Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Drupal Tip: Add your own Fields to Solr Search Index

DZone's Guide to

Drupal Tip: Add your own Fields to Solr Search Index

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

Drupal 7 is the PHP content management system that needs no introduction. But if you also want an excellent search engine to use with it then Apache Solr is the best choice. Solr builds on Lucene and adds some nice features on top of it.

There is a very good Drupal module to integrate the search engine with. It simplifies the Solr installation and adds most of the basic node fields into search index by default. But what about when you need to define your own fields?

Like any of the Solr tutorials will tell you, Solr's index (and fields) is defined in its schema.xml file, but you don't even need to touch that to add some basic fields. Luckily the Solr module provides some useful hooks that it uses during indexation.

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_index hook. This change actually confused me because I hadn't noticed it and my custom fields weren't being indexed anymore. 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 I've fetched the price field of a node and added it into the Solr document. Of course you have to replace the beginning of the method name with your own module's name.

One trick you have to notice here is that I've prefixed the field with "ss_". Solr's schema supports dynamic field definitions. For example all fields starting with "ss_" are defined as string types. You need to pay attention to the type you want. If you have a text field that you want to be searchable you should probably use text field type.

For Solr searching

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');
}

This tells Solr to add the new field into the set of returned fields. Simple as that!

End result

Now you've got the new field in Solr's search index. If you also want to be able to search its contents, pay attention to the type you define it as and make sure that Solr's QueryParser uses the field.

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Published at DZone with permission of Janne Anttila. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}