Add custom field to Search API Solr index

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)];
  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.


It article is interested for me. Thanks. But I have next problem. I change one from function so: function example_search_api_property_weekday_getter_callback($item) { return 'rrrr'; } but when I search 'rrrr' I have not found any items. I use profile2 instead nodes. With cck fields search_api works fine.

Please ensure you have added your "new" field to index. Can you also please check with Solr whether this field indexed properly?

multiple values encountered for non multiValued field

It works like a charm. I've replaced 'node' with my own entity name and the multiple value facet allready works. Awesome.. Now comes the next struggle: My predecessor used the Dynamic properties module for adding multiple fields to an entity. Any suggestions about how to get them into the index and create facets?? cheers Willem

I think it should be still possible using hook_entity_property_info_alter but in getter callback you should do get needed property of the entity.

A great example found in link module

Very nice, but if i want to use this custom property to sort a view, it's displaying in the views UI but the callbacks are never called. Is there any workaround for this use case?