DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Drupal Tip: Add your own Fields to Solr Search Index

Drupal Tip: Add your own Fields to Solr Search Index

Janne Anttila user avatar by
Janne Anttila
·
Apr. 01, 12 · Web Dev Zone · Interview
Like (0)
Save
Tweet
10.28K Views

Join the DZone community and get the full member experience.

Join For Free
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.

Drupal Content management system

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • On Architects, Architecture, and Failures
  • Spring, IoC Containers, and Static Code: Design Principles
  • How to Build Microservices With Node.js
  • Anatomy of a Webhook HTTP Request

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo