简体   繁体   English

Drupal和Solr添加用于索引的自定义字段

[英]Drupal and Solr add custom field for indexing

I'm attempting to add a custom field to Solr from a Drupal enviorenment. 我正在尝试从Drupal环境向Solr添加自定义字段。

Atm in schema.xml I have 我在schema.xml中有atm

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

While in drupal custom module in hook_apachesolr_index_documents_alter() is 在hook_apachesolr_index_documents_alter()中的drupal自定义模块中是

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

In solr query and schema browser 'custom_files' field is present and can be read, however, on general search it returns nothing. 在solr查询和模式浏览器中,“ custom_files”字段存在并且可以读取,但是,在常规搜索中,该字段不返回任何内容。 The only way to make something to return based on 'custom_files' field is to search directly in field. 使基于“ custom_files”字段返回内容的唯一方法是直接在字段中搜索。

How could I meake solr search 'custom_files' field on general search? 如何在常规搜索中让solr搜索'custom_files'字段?

Note: I tried also to make my field using dynamic field definition but ended with same result. 注意:我也尝试使用动态字段定义来创建字段,但结果相同。

You don't mention which version of Drupal (D7 I assume?) or which module you're using (apachesolr or search_api_solr) , but the gist is that you need to add it to the fl parameter ( fl = field list) so that the contents of the field are returned in the search results. 您没有提及哪个版本的Drupal(我假设是D7?)或使用的是哪个模块(apachesolr或search_api_solr),但是要点是您需要将其添加到fl参数( fl =字段列表)中,以便该字段的内容将在搜索结果中返回。 You've indexed the data, but you also have to tell the query to return that data. 您已为数据建立索引,但还必须告诉查询返回该数据。 With the apachesolr module, you would use the hook_apachesolr_query_prepare() hook to add that parameter. 对于apachesolr模块,您将使用hook_apachesolr_query_prepare()挂钩添加该参数。

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

On a side note, why are you using a custom field in schema.xml? 附带说明一下,为什么要在schema.xml中使用自定义字段? Solr has dynamic fields which allow you to create a custom field on the fly without having to add anything to the schema definition. Solr具有动态字段 ,您可以动态创建自定义字段,而不必在架构定义中添加任何内容。 These text fields are defined in the D7 apachesolr schema: 这些文本字段在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"/>

The s and m stand for single and multiple , so you would use ts_ if the field would only store a single value per document, and tm_ if the field would have multiple values per document. sm代表singlemultiple ,因此,如果该字段每个文档只存储一个值,则使用ts_如果该字段每个文档具有多个值,则使用tm_

So in your case you could do this in your indexing hook: 因此,在您的情况下,您可以在索引挂钩中执行此操作:

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

and then 接着

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

in your query_prepare hook. 在您的query_prepare挂钩中。 And all this without adding anything to your schema. 所有这一切都无需在架构中添加任何内容。

If you're using search_api_solr (D7) here's how to add extra information not contained in the nodes as they are indexed (eg computed values). 如果您使用的是search_api_solr (D7),则​​此处介绍了如何在索引节点时添加未包含在节点中的额外信息(例如,计算值)。

In your .module, use something like: 在您的.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'
    );
}

The IndexMetadata class would be something like: 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'
      ),
    );
  }
}

In your module's .info file, add: 在模块的.info文件中,添加:

files[] = IndexMetadata.inc

to include the above class. 包括以上课程。

Finally, run drush cc all then go to the Search API config page, find the index you're adding to and click 'Filters' (or admin/config/search/search_api/index/[index_name]/workflow). 最后,运行drush cc all然后转到Search API配置页面,找到要添加到的索引,然后单击“过滤器”(或admin / config / search / search_api / index / [index_name] / workflow)。 The new processor 'Index timestamp' is shown. 显示了新的处理器“索引时间戳记”。 Tick the box to have it run on index. 选中该框以使其在索引上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM