Apache Drupal和Solr为索引添加自定义字段

Apache Drupal和Solr为索引添加自定义字段,apache,drupal,solr,drupal-7,drupal-modules,Apache,Drupal,Solr,Drupal 7,Drupal Modules,我正试图从Drupal环境向Solr添加一个自定义字段 我有schema.xml中的Atm <field name="custom_files" type="text" indexed="true" stored="true" termVectors="true"/> 在solr查询和架构浏览器中,存在“custom_files”字段,可以读取该字段,但在常规搜索中,该字段不返回任何内容。根据“custom_files”字段返回内容的唯一方法是直接在字段中搜索 如何在常规搜索中搜索

我正试图从Drupal环境向Solr添加一个自定义字段

我有schema.xml中的Atm

<field name="custom_files" type="text" indexed="true" stored="true" termVectors="true"/>
在solr查询和架构浏览器中,存在“custom_files”字段,可以读取该字段,但在常规搜索中,该字段不返回任何内容。根据“custom_files”字段返回内容的唯一方法是直接在字段中搜索

如何在常规搜索中搜索solr“自定义文件”字段


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

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

function mymodule_apachesolr_query_prepare() {
  $query->addParam('fl', 'custom_files');
)
另一方面,为什么要在schema.xml中使用自定义字段?Solr提供了一个自定义字段,允许您动态创建自定义字段,而无需向模式定义添加任何内容。这些文本字段在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"/>
然后

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

在你的
查询中\u准备
钩子。所有这些都不需要向模式中添加任何内容。

您没有提到Drupal的哪个版本(我想是D7?)或您正在使用的模块(apachesolr或search\u api\u solr),但要点是您需要将其添加到
fl
参数(
fl
=字段列表)以便在搜索结果中返回字段的内容。您已经为数据编制了索引,但还必须告诉查询返回该数据。对于apachesolr模块,您可以使用
hook\u apachesolr\u query\u prepare()
hook添加该参数

function mymodule_apachesolr_query_prepare() {
  $query->addParam('fl', 'custom_files');
)
另一方面,为什么要在schema.xml中使用自定义字段?Solr提供了一个自定义字段,允许您动态创建自定义字段,而无需向模式定义添加任何内容。这些文本字段在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"/>
然后

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

在你的
查询中\u准备
钩子。所有这些都不需要向模式中添加任何内容。

如果您使用的是搜索api\u 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.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'
      ),
    );
  }
}
IndexMetadata类类似于:

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.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
,然后转到搜索API配置页面,找到要添加到的索引并单击“过滤器”(或admin/config/Search/Search\u API/index/[index\u name]/workflow)。显示新处理器“索引时间戳”。勾选该框,使其在索引上运行。

如果您使用的是搜索api\u 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.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'
      ),
    );
  }
}
IndexMetadata类类似于:

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.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
,然后转到搜索API配置页面,找到要添加到的索引并单击“过滤器”(或admin/config/Search/Search\u API/index/[index\u name]/workflow)。显示新处理器“索引时间戳”。勾选该框,让它在索引上运行。

谢谢,hook\u apachesolr\u query\u prepare()实际上是我所需要的全部。是否有任何方法可以通过使用代码$document->addField('ts\u custom\u files','some long string')设置stored=“false”,但index=“true”?谢谢,hook\u apachesolr\u query\u prepare()实际上是我所需要的全部?是否有任何方法可以设置stored=“false”,但是通过使用代码$document->addField('ts_custom_files','some long string'),index=“true”?