Check out my visual testing tool https://diffy.website. It helps to save testing time during deployments and development.
Today we are going to talk about really awesome module Search API
One of the most popular tasks I have regarding customizing apachesolr search results is adding new fields to index and to have custom facet for them. So lets discuss how it is done if we are using Search API module.
First of all I expect that you have Search API and Search API Solr search modules installed. Solr node index is created and search page is in place to have search within our index.
As example our task is to create two additional properties of the node: week day of creation of the node (single value text) and random text multiple value property.
In order to have new properties of the node available we need to use hooks of Entity API module. In our case all we need is to use hook_entity_property_info_alter().
/** * Implements hook_entity_property_info_alter(). */ function example_search_api_property_entity_property_info_alter(&$info) { $info['node']['properties']['created_week_day'] = array( 'type' => 'text', 'label' => t('Week day of node creation'), 'sanitized' => TRUE, 'getter callback' => 'example_search_api_property_weekday_getter_callback', ); $info['node']['properties']['test_multiple_field'] = array( 'type' => 'list<text>', 'label' => t('Test multiple text'), 'sanitized' => TRUE, 'getter callback' => 'example_search_api_property_random_text_getter_callback', ); }
In code above we define two new properties of the node. First one is single value and the second is multiple value.
Implementation of getter functions can be following:
/** * Getter callback for created_week_day property. */ function example_search_api_property_weekday_getter_callback($item) { return format_date($item->created, 'custom', 'D'); } /** * Getter callback for multiple field. */ function example_search_api_property_random_text_getter_callback($item) { $strings = array('one', 'two', 'three', 'four', 'five'); $number = rand(1, 5); $values = array(); while ($number > 0) { $values[] = $strings[rand(0, 4)]; $number--; } return $values; }
After enabling our module we can go to Fields of our index and find two new text fields.
After creating facets of these fields and adding facets blocks to our search page we will see facets working.
Now you can see how it is easy to add new properties to entities in Search API. If we create custom field we should in similar way advise Entity API about them. As example you can see patch to add location field data.
You are welcome to download our example module below.
Comments
Cant repeat example
Please ensure you have added
Got a Solr Error
awesome
I believe it should be done same way
Another way to use field_info
Views integration