簡體   English   中英

Drupal和Solr添加用於索引的自定義字段

[英]Drupal and Solr add custom field for indexing

我正在嘗試從Drupal環境向Solr添加自定義字段。

我在schema.xml中有atm

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

在hook_apachesolr_index_documents_alter()中的drupal自定義模塊中是

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

在solr查詢和模式瀏覽器中,“ custom_files”字段存在並且可以讀取,但是,在常規搜索中,該字段不返回任何內容。 使基於“ custom_files”字段返回內容的唯一方法是直接在字段中搜索。

如何在常規搜索中讓solr搜索'custom_files'字段?

注意:我也嘗試使用動態字段定義來創建字段,但結果相同。

您沒有提及哪個版本的Drupal(我假設是D7?)或使用的是哪個模塊(apachesolr或search_api_solr),但是要點是您需要將其添加到fl參數( fl =字段列表)中,以便該字段的內容將在搜索結果中返回。 您已為數據建立索引,但還必須告訴查詢返回該數據。 對於apachesolr模塊,您將使用hook_apachesolr_query_prepare()掛鈎添加該參數。

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"/>

sm代表singlemultiple ,因此,如果該字段每個文檔只存儲一個值,則使用ts_如果該字段每個文檔具有多個值,則使用tm_

因此,在您的情況下,您可以在索引掛鈎中執行此操作:

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

接着

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

在您的query_prepare掛鈎中。 所有這一切都無需在架構中添加任何內容。

如果您使用的是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)。 顯示了新的處理器“索引時間戳記”。 選中該框以使其在索引上運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM