0

I am in the process of building a new site that will have some content in the form of videos and PDFs. To make those more searchable I added the metatag module. We are also using the Search API to hook into Solr and do our search operations.

I have found the hooks I need to insert, update, and delete metatag info from the metatag fields. That part is fairly straight forward.

However, I am stuck on the part where I tell the Search API about the fields so that they can be indexed. I think I will be able to figure out how to push the data to the index with the hooks. I just don't know how to tell the Search API that the fields are there and that they contain strings.

I was looking, originally, at the hook_search_api_item_type_info(). However, after reading the description in the API documentation (in the code) I realize that it is only for new entity types. These metatag fields are mainly attached to nodes. And the documentation specifically says that it should not be used for existing entity types (i.e. nodes) because this hook simply tells about the data controller classes.

Should I use hook_search_api_alter_callback_info() to do this? That seems like a good candidate, but I am just not quite following how this works in the SearchAPI.

How do I tell the Search API that the metatag fields exist and I want to be able to index them?

This is in Drupal 7.

1

I do it like this: First

  function HOOK_search_api_alter_callback_info() {
  $callbacks['search_api_alter_add_keywords_field'] = array(
    'name' => t('Keywords field from metatag module'),
    'description' => t("Adds Keyword field from metatag module to indexed data."),
    'class' => 'SearchApiAddKeywordField',
  );
  return $callbacks;
}

Second

class SearchApiAddKeywordField extends SearchApiAbstractAlterCallback {      
  public function alterItems(array &$items) {
  foreach ($items as $id => &$item) {
      if (!isset($item->metatags[$item->language]['keywords']['value'])) {

        $item->search_api_keyword = NULL;
        continue;
      }
      $item->search_api_keyword = strtolower ($item->metatags[$item->language]['keywords']['value']);
    }
  }

  public function propertyInfo() {
    return array(
      'search_api_keyword' => array(
        'label' => t('Keyword field'),
        'description' => t('Add keyword field to index.'),
        'type' => 'text',
      ),
    );
  }

}
  • After Clear cache go to you index filters and check: Keywords field from metatag module
  • If Boost settings need to be changed (to give more importance to the field) do it now.
  • Index Again
  • Go to the search results view - the view you created to list indexed content.
  • Assuming that you have fulltext search filter in "Searched fields" select the new "field" Keyword field
  • Test the things out.
0

The "Meta tag" module has the integration with "Search API" ready to be used for indexing your metadata.

From README.txt

Search API integration

Entity meta tag values can be made searchable using the Search API module (https://www.drupal.org/project/search_api).

  1. Select "Meta tags" under "Data alterations" in the filters for the index: admin/config/search/search_api/index/INDEX NAME/workflow
  2. Meta tag fields will now appear under "Fields" and can be enabled there: admin/config/search/search_api/index/INDEX NAME/fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.