【问题标题】:Drupal and Solr add custom field for indexingDrupal 和 Solr 为索引添加自定义字段
【发布时间】:2016-04-05 15:32:29
【问题描述】:

我正在尝试从 Drupal 环境中向 Solr 添加自定义字段。

我有 schema.xml 中的 Atm

<field name="custom_files" type="text" indexed="true" stored="true" termVectors="true"/>

虽然在 drupal 自定义模块中 hook_apachesolr_index_documents_alter() 是

foreach($documents as &$document){
    $document->addField('custom_files', 'some long string');

在 solr 查询和模式浏览器中,'custom_files' 字段存在并且可以读取,但是,在一般搜索中它不返回任何内容。根据“custom_files”字段返回内容的唯一方法是直接在字段中搜索。

如何在常规搜索中使用 solr search 'custom_files' 字段?

注意:我也尝试使用动态字段定义创建我的字段,但结果相同。

【问题讨论】:

    标签: apache drupal solr drupal-7 drupal-modules


    【解决方案1】:

    您没有提及 Drupal 的哪个版本(我假设是 D7?)或您正在使用哪个模块(apachesolr 或 search_api_solr),但要点是您需要将其添加到 fl 参数(@987654323 @ = 字段列表),以便在搜索结果中返回字段的内容。您已经为数据编制了索引,但您还必须告诉查询返回该数据。使用 apachesolr 模块,您可以使用 hook_apachesolr_query_prepare() 挂钩来添加该参数。

    function mymodule_apachesolr_query_prepare() {
      $query->addParam('fl', 'custom_files');
    )
    

    顺便说一句,为什么要在 schema.xml 中使用自定义字段? Solr 具有dynamic fields,它允许您即时创建自定义字段,而无需向架构定义添加任何内容。这些文本字段在 D7 apachesolr 模式中定义:

    <dynamicField name="ts_*"  type="text"    indexed="true"  stored="true" multiValued="false" termVectors="true"/>
    <dynamicField name="tm_*"  type="text"    indexed="true"  stored="true" multiValued="true" termVectors="true"/>
    

    sm 代表 singlemultiple,因此如果该字段仅存储每个文档的单个值,则使用 ts_,如果该字段有多个,则使用 tm_每个文档的值。

    因此,在您的情况下,您可以在索引挂钩中执行此操作:

    $document->addField('ts_custom_files', 'some long string');
    

    然后

    $query->addParam('fl', 'ts_custom_files');
    

    在您的 query_prepare 挂钩中。所有这一切都无需向您的架构添加任何内容。

    【讨论】:

    • 谢谢,hook_apachesolr_query_prepare() 实际上就是我所需要的
    • 有什么方法可以设置 stored="false", but indexed="true" 通过使用代码 $document->addField('ts_custom_files', 'some long string') ?
    【解决方案2】:

    如果您使用的是 search_api_solr (D7),以下是如何添加节点索引时未包含的额外信息(例如计算值)。

    在您的 .module 中,使用如下内容:

    function mymodule_alter_callback_info() {
        $callbacks['index_metadata'] = array(
            'name' => t('Index node metadata'),
            'description' => t('Add node metadata to solr index.'),
            'class' => 'IndexMetadata'
        );
    }
    

    IndexMetadata 类类似于:

    // IndexMetadata.inc
    class IndexMetadata extends SearchApiAbstractAlterCallback {
      public function alterItems(array &$items) {
        foreach ($items as $id => &$item) {
          $item->indexed_at = time(); // or other more useful metadata
        }
      }
      public function propertyInfo() {
        return array(
          'indexed_at' => array(
            'label' => t('Index timestamp'),
            'description' => t('Unixtime when node was indexed'),
            'type' => 'int'
          ),
        );
      }
    }
    

    在模块的 .info 文件中,添加:

    files[] = IndexMetadata.inc
    

    包括上面的类。

    最后,运行drush cc all,然后转到 Search API 配置页面,找到您要添加到的索引并单击“过滤器”(或 admin/config/search/search_api/index/[index_name]/workflow)。显示了新的处理器“索引时间戳”。勾选框让它在索引上运行。

    【讨论】:

      猜你喜欢
      • 2022-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      相关资源
      最近更新 更多