The Search API Field Token contributed module allows site builders to provide a token field option on the Search API index to set the data to be sent to the index using tokens. In simple language, it allows us to index data into search index using tokens like content type label, image style of an image.
The requirement
At Morpht, we have the requirement to index the content type label, image style of an image with absolute URL into the search API index.
Currently, in the Search API configuration it is possible to select a image (Media) but not define the Image Style. The end result is that the original image path is indexed. This image is large and un-cropped. Generally this is not a problem because rendered entities (Node) are used when displaying results, however, for systems such as Sajari and Recombee this is not possible as we use JSON Template to render the results client side. We need a way to get the proper image style into the index.
Also, it is not possible to select the Content Type label, just the machine name.
When using results from Search API it is usual to use rendered entities to show the results. A result of this pattern is that it is not easy to index images into the search index. However, in some cases this is desired, such as when adding content to indexes in external services such as Recombee (recommbnedations) and Seach.io (search).
The Solution
In order to make this work, we could not rely on the single solution which solves the problem only for image styles. We came up to the leverage the power of token system to deal with other upcoming use cases.
- We provided a new SearchApiProcessor property token_field_property
- We provided a SearchApiProcessor token_field which accepts the token with token_field_property
Implementing the Search API Field Token Drupal module
Using the Search API Field Token Drupal module is very simple as other contributed and core Drupal modules. The Search API Field Token module can be installed by following steps:
- Installation: Install the Drupal Search API Field Token module from here and install the module in the same way as you would install any contributed Drupal module.
- Enabling:You will find that Search API Field Token along with Search API modules. Let's enable it.
- Fields section in the Search Index: Navigate to Administration > Configuration > Search and Metadata > Search API > [Search Index to edit] > Fields.
- Adding field in the Search Index: Add the token fields and save
How it looks?
You can see this in action on "Recommended" section on the homepage our site.
Give this module a try and let us know what you think in the issue queue.