Hey there! If you are interested in Drupal web development, tips by our devs could do you a world of good. Having once given you a review of Web developer tools for Drupal 7, today we'll focus our attention at such issue as Drupal 7 & Apache Solr. 100% profit! ;)
I’ve heard a lot of Apache Solr, but never had an opportunity to try it. Therefore, I decided to create a test platform to see how Drupal search works and configure it using ApacheSolr.
Some benefits of ApacheSolr search over conventional search in Drupal:
- ApacheSolr search is much faster and does not overload the database server;
- allows flexible search settings;
- the site is easier to scale, you can easily install ApacheSolr on a nearby server;
- use of facets (refined/faceted search).
1. Installing Apache Solr
First, you need to install java, because ApacheSolr is written in java.
sudo apt-get -y install openjdk-7-jdk
ln -s /usr/lib/jvm/java-7-openjdk-amd64 /usr/java/default
In this blog post, we will install solr-tomcat, so run the following command:
sudo apt-get -y install solr-tomcat
Now ApacheSolr should be available at http://your_ip:8080/solr.
2. Connecting Apache Solr to Drupal
First, install several modules: search_api and search_api solr.
I installed search_api-7.x-1.16 and search_api_solr-7.x-1.9.
Now go to the module search_api_solr and copy two files from there - schema.xml and solrconfig.xml to the folder with the server settings (the path to the folder can be found with the "whereis solr" command - in my case it’s “/etc/solr/conf/”). These files are necessary for Drupal to be able to interact with Apache Solr. Once you have copied these files, you should restart the server.
3. Configuring Drupal Search
If you have not yet enabled search_api and search_api_solr modules, then enable them and clear the cache.
On the admin/config/search/search_api page, you can create two entities - server and index.
Server is a kind of bridge between Drupal and Apache Solr. It stores the settings to access the Apache Solr server.
Index allows you to configure the search in a certain way. You can create multiple indexes for a single server. For instance, different indexes for different types of nodes.
So create admin/config/search/search_api/add_server.
As a service class, select «Solr service». If you can not select a class, make sure that the SolrPHPClient library is installed in sites/all/libraries/SolrPHPClient and the search_api_solr module is enabled.
If everything is configured correctly, you will see the following message.
Now create the index - admin/config/search/search_api/add_index. Call it Node index, select Solr server created by us. 'Index items immediately' is a useful option, that allows indexing nodes at the time of creation (if you do not select it, the nodes will be indexed by cron).
The next step is choosing the fields by which to search. So far, we will only choose the title and save the form. Now create a view to display the search results - admin/structure/views/add. Before this, we should enable the search_api_views, views, views_ui and ctools modules.
For search results, there is a special type of views for the views module. Choose your index - Node index and create a view with a page display and configure it.
For that, add the Title and Body fields for display. Then add the «Search: Fulltext search» filter and select the Title field in the filter settings. So the search will be performed by the Title field. Also add exposed filter in order to be able to enter search arguments through the form.
Save the views. Run the indexing of nodes if they are not indexed on this page admin/config/search/search_api/index/node_index/status. I previously generated a couple of nodes - articles.
Go to the /search-results page - that’s the address I specified for display with the page type.
Right now all nodes are displayed because keywords are not specified.
Here’s how the search results by keywords look.
4. Faceted filters
Why do you need a facet, and what is it? A facet is a filter by specific field. This filter works as a specification.
Let’s consider a little example. You have an entity of a car, and it is characterized by color and by manufacturer. First, you can choose several colors in the filter.
But, having selected the manufacturer, you refine the search and now only the colors that match the selected manufacturer are available to you.
If you click on Ford again, the filter will return to its original state.
This functionality is commonly used to find information in different databases and directories.
So let's see how to configure facets for search with Drupal through Apache Solr.
Let’s first perform some more actions for configuring the website:
1. You must first install the facetapi module and Search facets (included in facetapi).
2. Now create a new content type Car and add fields for the color and the manufacturer there.
3. Create a new index that should index the nodes with the car type.
4. Then go to the Fields tab and include the color and type fields into the index.
So we have configured the index to search by nodes with the car type. Now let's move on to the facets. If you have already enabled the required modules and cleaned the cache, in the index settings you should see a new tab with Settings for Facets. In this tab, you can enable a facet filter for fields that are added to the index. You can specify the settings for each facet, but we’ll discuss it a little later. Now let’s just enable the facets and index our nodes with the car type.
Now you need to configure the output. For each facet filter, a standard block is created, and you can output it as you wish, for example through regions or panels. Facets have one peculiarity - you should first place the views with the results on the page, and, one level lower (and, accordingly, displayed later) the blocks of facets. After I placed the blocks of facets to the region, I got this kind of page:
Now let's talk about the settings of facets.
In the screenshot, you can see the form of settings.
Let’s consider the most important of them:
Display widget - allows you to select a widget for facet. By default, widgets in the form of links and checkboxes are available.
Sorting - defines the criteria by which the values in the facet block will be sorted when displayed.
Operator - AND or OR. In case with AND, you can select only one value in every facet, and in case with OR you can select a number of them.
Display for searches - you can specify for which displays in the views facet will work.
This can be useful if you have multiple displays with search results and do not want to filter only one of them.
To generate realistic content, I used the Realistic Dummy Content modules.
To have good links when using facet filters, I used the Facet API Pretty Paths module.
Hope these tips have been useful. Good luck with your coding!