Apachesolr, Drupal, Php

Drupal 7 Apache solr add custom field to solr search index

Drupal 7 Apache solr search integration module is most useful to integrate Apache Solr with drupal. Apache solr search integration module will index all the content of your sites at solr which is full with advance search feature. You can exclude some of content types from being index under setting of this module. When you index your content at solr it will index most of the field of your content type but some of the custom field will not be indexed but Apache solr integration module provides number of hooks which you can use to add custom field to solr index, to alter the query before it sent to apache solr or alter the search result before being displayed.

For Solr Indexing:

You need to use hook_apachesolr_index_document_build. This hook is fairly new in the Solr module (since version 7.x-1.0-beta14) and it replaces the deprecated hook_apachesolr_update_indexhook. Here’s how to use it in your custom module:

function MY_MODULE_apachesolr_index_document_build(ApacheSolrDocument $document, $entity, $entity_type, $env_id) {
  $price = $entity->field_price;
  $document->addField('ss_product_price', $price / 100);

Here notice i added the field the name ss_product_price. This is to follow the apache solr naming convention for dynamic field. Here ss means string single field. The first ‘s’ means its string value field, this field will hold only string value, and second ‘s’ means this is single value field. When we will search for some terms then solr will expect to search for single string in this field. If we store text (containing more string) value to this field then search will not work as expected. If the field you are indexing can have more then one value then loop over each value and us sm_ prefix with the field name. If you adding text field then try ts_ and tm_ for text single and text multi value respectively. Drupal Apache solr integration module support to index dynamic fields. For detail read the Dynamic field section in schema.xml shipped with apache solr module.


This tells Solr to add the new field into the set of returned fields.

For Sorl Searching:

Now you’ve got the new field in Solr’s search index. By default this new field doesn’t get returned when you search from Solr and therefore you need to use the hook_apachesolr_query_alter hook.

function MY_MODULE_form_apachesolr_query_alter($query) {
  $query->addParam('fl', 'ss_product_price');

Here i added ‘fl’ which stands for field in addParam function to add parameter to query.

End result:

Using these two hooks you can add custom field to solr index and can query to solr to search in that custom field. These two hooks will only work for Drupal 7. For drupal 6 follow this post.


If adding custom field in solr index works but search within that field doesn’t work then there is chance that you indexed the field as String field and you are trying to search complete text within that field in this case search will not work. You need to index field as text field with ts_ prefix in custom field name and altering the query by adding ‘qf’ parameter in query like this:

  function MY_MODULE_form_apachesolr_query_alter($query) {
    $query->addParam('qf', 'ts_product_price');

Have a look at this post on drupal.org for more clarification.


8 thoughts on “Drupal 7 Apache solr add custom field to solr search index

  1. Pingback: Drupal 7 Apache solr add custom sort to solr query parameter | Let know this world

  2. Pingback: Drupal 7 apache solr search within attached document | Let know this world

  3. This didn’t not work for me
    addParam(‘fl’, ‘ss_product_price’);
    I had to use this
    addParams(array(‘fl’ => array(‘ss_product_price’)));

  4. Mohamed Zeid says:

    Thanks for your great post. I am newbie to the Drupal world and I have some custom fields in my content type. I tried to create a view to use apchesolr localhost server, but I keep getting this error whenever I try to add the custom fields

    “Configure field: Broken/missing handler
    The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.”

    So, I don’t know what is wrong here. Maybe because these custom fields were not added to solr. My other question is: where to add the code above? Do I add it to schemal.xml or the apche solr integration moudle and in which file? Hope you can help me and thanks in advance.

    • Hi ZEID,
      That Broken/missing handler error will be shown if the field is available for views but the handler for that field is missing, this could be case that earlier when you indexed that filed the module providing handler for that field was enabled but later you disabled that module, By re-enabling that module can resolve this error.

      For you another question, No don’t put that code in schemal.xml file. You have to create your custom module. There are bunch of tutorial over the internet to create the drupal 7 module. You just need to create .info and .module files for this code to work. Put the above code in .module file and replace “MY_MODULE” text with your module name.

  5. Yael says:

    Thank you for this post.
    I also to write it like Stefan mentioned in order to make it searchable.
    If you also experienced it, it might be good to update the procedure.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s