Ivan Zugec

Drupal Consultant

How to setup Search API with Apache Solr

The Search API is a Drupal 7 search framework module. It allows you to create custom search pages on any URL and integrates with a few search backends.

In this article I’ll show you how to setup Search API with Apache Solr on a mac (10.6).

For more details check out the Search API project page.

If you already have solr setup skip to the Search API section.

Setup Apache Solr

The first thing we’ll have to do is setup Solr locally. Make sure you have Java installed, if your using OSX 10.6 and keep it up to date you should be fine.

To double check run java -version in terminal to see which version is installed.

sapi_image1.png

Next go to the download page and download version 1.4.1.

sapi_image2.png

Extract the files anywhere open up terminal and navigate to the extracted directory.

sapi_image3.png

Solr ships with a built-in web server called Jetty. We’ll use this for our local version. To start up the server go into the example directory and execute start.jar.

cd example
java -jar start.jar

Go to http://127.0.0.1:8983/solr/admin to see if solr is working.

sapi_image4.png

Setup Search API

To get Search API working with solr there are a few thing we’ll have to configure with solr.
First go and download the following modules from drupal.org and place them into sites/all/modules:

  1. Entity
  2. Search API
  3. Search API Solr search

drush dl entity search_api search_api_solr

Configure solr

Go to your solr root directory and copy/paste the example directory, rename it to drupal (you can call the directory anything).

Copy the schema.xml and solrconfig.xml that ships in search_api_solr and paste it into /drupal/solr/conf. It’s fine to override the existing xml files just make sure you create a backup.

Now go ahead and test solr with the new xml files.

cd drupal
java -jar start.jar

Go to http://127.0.0.1:8983/solr/admin to see if solr is working with the new xml files.

Instead of “(Example)” you should see “(search-api-1.0)”.

sapi_image5.png

Download SolrPhpClient

Go to http://code.google.com/p/solr-php-client/downloads/list and download (Only works with r22 Solr PHP Client (Built 11/09/09 from r22) ) the php solr library and place it in the seach_api_solr directory.

sapi_image6.png

sapi_image7.png

Modules

Go to the modules page and enable the Search API, Search pages and Solr search.

sapi_image8.png

Configure Search API

Go to Configuration >> Search and metadata and click on Search API.

sapi_image9.png

There are three steps involved with setting up a solr search page. First we setup the server, then index and finally the search page.

Setup server

From the Search API configuration page click on the Add server link.

sapi_image10.png

Enter in the Server name and Server description then select Solr service from the Service class select box.

sapi_image11.png

Adjust the Solr service details below if you have any specific requirements.

Once you have finished filling out the form click on Save settings.

sapi_image12.png

Setup index

Once the server has been setup, the next thing we have to do is configure the index. Click on the Add index from the Search API configuration page.

sapi_image13.png

In the Add index form fill out the Index name and select which entity type you want to index and select Solr from the Server select box.

sapi_image14.png

Once you have created the index you’ll have to select which fields will get indexed. This could take some trail and error depending on your data. In the example that I’m using I have add the "main body text" and "Tags" from the Add related fields.

sapi_image15.png

After you have selected which fields get to be indexed, you’ll be directed to the Workflow page. On this page you can specify extra data alterations and processors to the index data. For this example we’ll keep that page empty.

The final item for the index is to actually add data into the solr index. Click on the Status tab and click on the Index now button.

sapi_image16.png

Search pages

Now that we have the server and the index all sorted we have to create a Search Page.

Search API module gives you the ability to create custom search pages on any URL. You are no longer stuck with a single search page on search/node or search/apachesolr_node.

Go to the Search API page and click on the Search pages tab, then click on the Add search page link.

sapi_image17.png

On the Add search page form enter in the Search name, Index and Path.

sapi_image18.png

On the next page you have a few more options, if you want leave the default options and click on the Create page button.

sapi_image19.png

Go to your newly created search page and test it out.

sapi_image20.png

If your having trouble you can start debugging by watching what appears in the terminal.

sapi_image21.png

Faceted search

Another great feature that the Search API gives you is faceted search. However this only seems to work with a solr server. UPDATE: Facets do work with database servers. Comment

To get started go and enable the Search facets module.

sapi_image22.png

Once the module has been enabled go back and edit the index that you created. Then select Facets from the dropdown.

sapi_image23.png

For this example I enabled the Content type and Tags > Name block. But you can choose any facet blocks.

sapi_image24.png

Once the blocks are enabled don’t forget to assign them to a region.

sapi_image25.png

Go back to your search page and test out the facets.

sapi_image26.png

If you have any problems or questions please leave a comment.

About the author: Ivan Zugec
Ivan Zugec's picture

Ivan is a Sydney Australia based Drupal Developer with over 5 years experience.

Follow:
Version: 

Comments

drunken monkey's picture

Submitted by drunken monkey on

If you don't object, I'll link to this article on the project page. I'll probably create a handbook page detailling this soon – can I use your screen shots for those? Looks really great!

One correction, though: Facets already also work with database servers, and facets support for Xapian servers is (as far as I know) planned, too.

ivan's picture

Submitted by ivan on

Absolutely link to this page :)

Also thanks for the correction.

Johnny Gamba's picture

Submitted by Johnny Gamba on

Hi, i wanted to say thank you so much, what a great tutorial, this is one of the most helpful and interesting post i have ever read, thanks again.

highermath's picture

Submitted by highermath on

This was an excellent walkthrough. One item of note: This only works with r22 of the SolrPhpClient. r60 generates errors.

ivan's picture

Submitted by ivan on

Post has been updated.

commonsense360's picture

Submitted by commonsense360 on

Thanks very much for this. I was dreading trying to figure it out on my own but your instructions are easy to follow and simplified to make it a much easier journey. VERY GOOD STUFF!

marco71's picture

Submitted by marco71 on

You directed me to the search API module in Drupal. You didn't tell me about this guide :-)
I messed around with all kinds of things, I now read how too, in this guide.

I just set up Solr 3.3.0 on my local tomcat windows machine and finished setting up the simple search form. And YES YES It's finding "Jobs" Cool man I couldn't be happier.

camry96's picture

Submitted by camry96 on

Just wanted to say that your tutorial is a godsend!! I have been googling this forever and trying various tutorials -- they've all worked up to a point, but I could never get the facets working. With yours, I finally can! Much gratitude from a complete drupal newbie. THANK YOU.

Alan S's picture

Submitted by Alan S on

This is a brilliant tutorial - easily the best I've seen for this.

There's only one thing missing (from this and pretty much every Solr tutorial, especially the flimsy Drupal docs), which is the basics of how to secure Solr, in particular how to set up HTTP Authentification.

This depends on whether you're using a Tomcat or Jetty version of Solr (I believe Jetty is the default). It's Jetty if you have a file jetty.xml (usually .../[solr directory]/example/etc/jetty.xml ), and it's tomcat if you have a file WEB-INF/web.xml (could be various places on your system, try running find / -name "web.xml" )

Here's a good, well written guide on how to set up basic HTTP authentification for apache solr in Jetty, and here's a guide on how to set up basic HTTP authentification for Tomcat apache solr.

marco's picture

Submitted by marco on

it would be great if someone could post a way to autostart jetty on system boot/startup. i found a way but thats suboptimal.
i created a shellscript containing the manuel command and call that on startup (update-rc MYSCRIPT defaults)

Ryan Johnston's picture

Nicely written! I love full-fledged how-tos like this. The apache solr and search apis are SUPER powerful. Love it!

Henning's picture

Submitted by Henning on

Hi,
and thank you for this how-to.
Do you (or anybody else) know how to get the "start-page" of search to view all data? What I mean is that when a user first get to the search page I want to show all data to the user.

ivan's picture

Submitted by ivan on

If you create a search pages using views (search_api_views). You can set it up to display default results when they land on the page.

thierry geufroi's picture

Submitted by thierry geufroi on

thanks for this great tutorial.
I am using the regular mysql database service which now should provide faceted searching....
Even though I think I make everything ok, I end with facets with no items...would you have a clue on what I could have missed?

ivan's picture

Submitted by ivan on

I haven't used Search API with the database backend. Try using solr and see what happens.

Jordi Serra's picture

I need to use Acquia search with my Search API module, but I can't figure out how. I tried all combinations but with no luck. If somebody succeeded, could please explain me how??

THANKS!

ivan's picture

Submitted by ivan on

I don't think Acquia search supports Search API, I could be wrong. :) Contact Acquia and find out.

BarisW's picture

Submitted by BarisW on

Please poke them to support it. I've asked them as well, and the more people ask for it, the earlier they will adopt it. The problem is that they need to support a specific schema.xml.

Jack's picture

Submitted by Jack on

I followed your tutorial. Great now I have my own solr drupal on my mac :D
Only when I place the Facets in blocks they don't show up untill I enter a keyword that is in the title of the a node and press search. any idea how this is posible?

Do I need to change something so that opening the page I created is showing all results? and if so how?

Jack's picture

Submitted by Jack on

found a sollution for my problem :)
Using search views and creating a new view I now have nodes with Facets in blocks on the side of the page

shivani's picture

Submitted by shivani on

Hi,
I am not able to index my content…
in logs,,error is
Indexing failed on one of the following entity ids: node/1, node/2
“400″ Status: Bad Request: Bad RequestApache Tomcat/6.0.33 – Error report HTTP Status 400 – ERROR:unknown field ‘site’type Status reportmessage ERROR:unknown field ‘site’description The request sent by the client was syntactically incorrect (ERROR:unknown field ‘site’).Apache Tomcat/6.0.33

pls help…

ivan's picture

Submitted by ivan on

It looks like Search API can't connect to Solr. Make sure you can access the Solr admin page first.

Brian's picture

Submitted by Brian on

Hi,
I installed SolrSearch and it worked fine. After I installed Search API (according to this page), when I replace 2 XML files, I get "400" Status: [doc=search_index-25] missing required field: entity_type. Please help. I really apreciate it.

ivan's picture

Submitted by ivan on

Are you running the latest version of Search API module? Also, where are you getting the 400 error? In Drupal or Apache Solr.

Add new comment