Name: kennyW

Drupal Digest

Improving Drupal's Core Search (with Apache SOLR)

kennyW's picture
by Kenny Wood
30 December, 2015Comments
Drupal's core search is great for websites with a small amount of content to search through and provides more than adequate results for the vast majority of use cases. But what about if your site has a substantial amount of content, or if you need a bit more control over how your search operates? Then using Apache SOLR might be the perfect option for you.

I'm not going to get into too much detail here around when to use SOLR and when using core search is sufficient. Instead i'm going to walk you through how a web designer can set up and configure Drupal to use Apache SOLR and improve on Drupal's core search functionality.

This article will cover;

  • How to connect your Drupal site to your Apache SOLR server
  • How to sort your results based on content priority
  • How to enable autocomplete
  • How to enable spellcheck
  • How to set up and use facets with SOLR 
  • How to enable partial word searching (fuzzy search)

You will need:

  • An Apache SOLR server
  • A web designer / web developer
  • Module: Search API
  • Module: Search API SOLR
  • Module: Search API Spellcheck
  • Module: Facet API
  • Module: AJAX Facets

Step 1: Installation and Set up

We won’t cover how to set up a SOLR server here as there are plenty of tutorials around this already, so no need to reinvent the wheel.

Once you have your apache SOLR server all set up and running smoothly, we need to connect it to your Drupal site.

To do this, you must first install and enable the Search API and Search API SOLR modules. If your web developer uses drush, they will be able to install the module using the following command

drush en search_api search_api_solr_ -y’

Next, we need to configure our search server. So head on over to Configuration > Search and Metadata > Search API. Then click to add a new server. From here you will need to specify a name for your search server, then select the “Solr Service” service class.

Create SOLR Server in Drupal

This will expand the pane and display new settings that must be configured for the SOLR server. Enter your servers IP address under ‘Solr host’, and then specify the path of your Solr instance under ‘Solr path’ (this usually follows the following pattern /solr/serverinstance (replace ‘serverinstance’ with whatever you called your server instance when creating the solr server. The rest can usually be left as their default settings. Next, click “create server”.

If all goes well you should be greeted with a screen and a couple of nice green notices stating that “The Solr server could be reached.”.

Our servers are now connected, now let’s configure our search index.

Go back to the Search API page where you should now see our recently created server. Click the “Add Index” link.

Add SOLR Index

Here we are going to specify the data we would like to index on the server. In this example, we are going to index all content of the ‘Expert’ content type. So we click “content” in the ‘item type’ drop down, and then select our content type. We then select our newly created search server in the “Server” drop down and click “Create index”.

There we have it, our basic search server and index have been created.

Step 2: Sort Results Based on Content Priority

After you click to create your index, you will then be taken to a page where we are able to specify which fields we want to be indexed. So go ahead and tick all of the fields you want to make searchable. Once you select to make a field indexable, you will be given an option of a “type”. If you want to make the field part of the ‘content ranking’ (i.e. the title is more important than the body), then you will need to set the type as ‘full text’.

In order to specify your ranking order, you will utilise the ‘boost’ option. Simply put, the higher the boost number, the more important that content is deemed.

Once you have selected your fields and priority. You will need to re-index the server before your changes are reflected on the server.

Step 3: Enable Autocomplete

Autocomplete can really aid the user experience of using your search, which is something you should be considering when building your website. In order to enable drupal autocomplete your web designer will need to first enable the search_api_autocomplete module. Next, navigate to your server index in your configuration, click on edit and you will see a new tab tabled “Autocomplete”. Clicking this will show a list of all of your search views that are using the index. Tick all of the views that you would like to enable autocomplete for.

Next head over to the permissions area and enable the appropriate permissions for viewing the autocomplete results.

And thats it. You should now see your autocomplete results in a drop down as you type

SOLR Autocomplete

Step 4: Enable spellcheck

Adding a spellcheck to your drupal website’s search is a great way to enhance the user experience and you absolutely should aim to build one into your site.

Let me walk you through a nice and simple way to add this to your SOLR search. First, you must install and enable the search API Spellcheck module (search_api_spellcheck for drush).

Once the module is installed, head over to your search view and enable the spellcheck block in the header of your view

And there we have it. Drupal will take care of the rest for you. Just search and you will see search suggestions.

Step 5: Facets

Every enhanced search needs facets. For the uninitiated, facets are ‘filters’ that allow you to filter your search results. To add facets to your SOLR search, you will need to install the facet api module (facet_api).

Next you will need to navigate to the search index you created earlier and then hit the newly created ‘facet’ tab. Here you will select all of the fields that you want to make into filters.

Clicking ‘save’ will create separate blocks for each facet. Simply add the blocks to your search page, to start using them.

There is one issue you will notice right away if you have ajax enabled on your search page. By default, facets don’t work with ajax and trigger a page refresh. This isn’t really ideal for on-the-fly updating of information for your user. We need to fix this. Luckily the fix is nice and simple. Head over and install the ‘ajax facets’ module (ajax_facets).

Then head on over to your search index, and click the ‘facets’ tab. Click ‘configure display’ on the facets you wish to enable ajax for. Now just select one of the ajax defaults in the ‘display widget’ dropdown and choose “Display ajax_facets wrapper” in the Empty facet behavior dropdown

Faceted Ajax

Your facets should now update the results without forcing a page refresh.

Step 6: Partial Word Searching

The single biggest problem with the default Drupal search, is the ‘partial word’ matching issue. For example if i search for the term “tech”, i will not receive any results for the term “technology” even though ‘tech’ is part of the term. This results in a lot of missed search results and your developer needs to fix this if you hope to provide a feature rich search functionality.

Using SOLR, we are able to remedy this problem by using Ngram’s. This works by breaking up the words into smaller subsections so “technology” becomes “te” “ec” “ch” “hn” and so on. This way, when we search for tech, it will realise that the term forms part of the word “technology” and we will then see the results for ‘technology’ also.

To make this work, we need to head over to SOLR server. Once you have SSH’d into the server, you will need to navigate to the conf folder using the following command

cd /var/solr/data/solrinstance/conf

replacing solrinstance for the name of your server.

Next we issue the vim command to edit the schema.xml file (vim, could be nano or any number of alternatives depending upon your system)

vi schema.xml

We need to add the following line into this document.

<filter class="solr.EdgeNGramFilterFactory" minGramSize="2" maxGramSize="25" />

We need to add the line under the following block of code

<fieldType name="text" class="solr.TextField" indexed="true" stored="true" multiValued="true" positionIncrementGap="100">       <analyzer type="index">         <charFilter class="solr.MappingCharFilterFactory" mapping="mapping-ISOLatin1Accent.txt"/>         <tokenizer class="solr.WhitespaceTokenizerFactory"/> </tokenizer>

To make the segment now read like so;

 <fieldType name="text" class="solr.TextField" indexed="true" stored="true" multiValued="true" positionIncrementGap="100">       <analyzer type="index">         <charFilter class="solr.MappingCharFilterFactory" mapping="mapping-ISOLatin1Accent.txt"/>         <tokenizer class="solr.WhitespaceTokenizerFactory"/> </tokenizer> <filter class="solr.EdgeNGramFilterFactory" minGramSize="2" maxGramSize="25" />

Save and exit the file and restart the server;

service solr restart

Re-index the content on the server and your site should start providing partial matched results right away!

Wrap up

So there we have it, a fully functioning SOLR search with all the modern features required to create a truly enhanced user experience. If your web developer has any issues with implementing this solution, just drop us a comment below and we will help you out where possible!

Other Posts

Free Christmas Graphic Templates

To celebrate the launch of our new blog, we are giving away a pack of 6 christmas graphic...

Welcome to Our New Blog

Now, we don't want to give too much away too soon, but we are working on some very exciting...