【问题标题】:how to customise search results of apachesolr (drupal 6)如何自定义apachesolr的搜索结果(drupal 6)
【发布时间】:2009-06-15 13:54:12
【问题描述】:

谁能帮助我如何自定义 apache solr 搜索的搜索结果。我只能访问这些变量 [comment_count] => [created] => [id] => [name] => [nid] => [title] => [type] => [uid] = > [url] => [score] => [body].

如何从索引中访问其他变量,例如 status, vote ....(我不想访问数据库来检索这些值,我想从索引中获取它本身)

我需要在结果 sn-p 中显示该特定节点的投票数

我需要了解 1.如何索引投票字段 2. 如何在结果sn-p中显示投票、状态...。

【问题讨论】:

  • 简短的回答是你必须通过请求所有你需要的字段,默认情况下只有这些字段将作为响应发送 d,nid,title,comment_count,type,created,changed,score,path ,url,uid,name 函数 formexample_apachesolr_modify_query(&$query, &$params) { $params['fl'] .= ',field_desc'; $params['fl'] .= ',field_url'; }

标签: drupal drupal-6 solr


【解决方案1】:

出于以下几个原因,投票是一个糟糕的索引选择:

  • 选票变化很快
  • 进行投票时,不会更新节点。因此,apachesolr 不会知道重新索引节点以获取更改。

如果“状态”是指节点->状态值,那么答案是它始终为 1。未发布的节点永远不会被索引。

现在,如果你想在索引中添加一些 else 内容,你需要hook_apachesolr_update_index(&$document, $node) - 这个钩子在每个节点被索引时被调用,你可以从 $ 向 $document 添加字段节点将值放入 solr 索引。但是,您想使用预定义的字段前缀 - 查看 schema.xml 以找到列表。

【讨论】:

  • +1 用于提及预定义的字段前缀 - 这正好回答了我的问题。
  • 很好的答案,但您也错过了能够在搜索结果中检索值的部分。
【解决方案2】:

以下是添加用于排序和输出的字段的代码示例。

/**
 * Implementation of hook_apachesolr_update_index()
 * Here we're adding custom fields to index, so that they available for sorting. To make this work, it's required to re-index content.
 */
function somemodule_apachesolr_update_index(&$document, $node) {
  if ($node->type == 'product') {
    $document->addField('sm_default_qty', $node->default_qty);
    $document->addField('sm_sell_price', $node->sell_price);
    $document->addField('sm_model', $node->model);
    foreach ($node->field_images AS $image) {
      //$imagecached_filepath = imagecache_create_path('product', $image['filepath']);
      $document->addField('sm_field_images', $image['filepath']);
    }
  }
}

/**
 * Implementation of hook_apachesolr_modify_query()
 * Here we point what additional fields we need to get from solr
 */
function somemodule_apachesolr_modify_query(&$query, &$params, $caller) {
  $params['fl'] .= ',sm_default_qty,sm_field_images,sm_sell_price,sm_model';
}

如果您想完全自定义输出,您应该执行以下操作: 1) 将 search-results.tpl.php 和 search-result.tpl.php 从 /modules/search 复制到您的主题文件夹。 2) 在 search-result.tpl.php 中根据需要使用 $result 对象 3) 不要忘记通过访问 admin/build/themes 清除主题注册表

或者如前所述,您可以使用预处理器挂钩覆盖。

问候,斯拉瓦

【讨论】:

    【解决方案3】:

    另一种选择是使用输入参数 nid 创建您喜欢的视图,然后在您的 template.php 文件中创建以下预处理:

    function MYTHEME_preprocess_search_result(&$vars) {
    
    $vars['myView'] = views_embed_view('myView', 'default', $vars['result']['node']->nid);
    
    }
    

    将视图名称“myView”与变量名称匹配对我来说很有意义。然后,您可以在 search-results.tpl.php 文件中使用变量 $myView。

    【讨论】:

      【解决方案4】:

      这是由 Solr 搜索集成模块的制造商制作的视频,概述了如何自定义索引哪些节点和字段,以及 Solr 作为搜索结果输出的内容...

      对于 Drupal 6: http://sf2010.drupal.org/conference/sessions/apache-solr-search-mastery.html

      和 Drupal 7: http://www.acquia.com/resources/acquia-tv/conference/apache-solr-search-mastery

      这一切看起来都非常可定制!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多