Searching content with RESTful

RESTful Drupal with Search API

Mateu Aguiló
3 min readDec 16, 2014

According to its module page, Search API aims to provide easy searches in Drupal.

Search API provides a framework for easily creating searches on any entity known to Drupal, using any kind of search engine.

Nowadays, you can safely forget about your search engine and start taking care of what you want to do with your search results. You can set up, for instance, an Apache Solr instance and hook it to Drupal to start indexing content in no time. On top of that you have a miriad of features, like great control on what you index, faceted search through Facetapi, building search pages with a couple of clicks, … In summary, it is really easy to set up your search functionality within Drupal.

The problem is when you go decoupled (or headless, as you please) and you don’t have the support of all these fantastic modules to build the feature for you. You need to expose all that information through your HTTP API, probably using your favorite API generation tool (RESTful, RESTWS or Services). There is no clear step-by-step tutorial or guidelines on how to build a decoupled search page using Drupal as your content provider.

Drupal-RESTful GitHub organization

That’s how I started my Saturday. It was rainy outside and I was not going to step off my office, since it was the only heated room at 7.30am. I figured that building this search integration would be a good test for the flexibility that we were looking for in the RESTful module.

It had been a while since I wrote the code that abstracted out the data providers, so I had to open up some of the existing examples to get started. After a couple of hours of getting myself acquainted with Search API’s code and writing the RESTful integration I was able to call it “good enough for the day” and started writing tests for it.

The resulting features are:

  • Perform a search and return the results in the requested format. For that you just need to do a GET request to https://www.example.org/api/my-fancy-search/Lorem+ipsum.
  • Sort your search results by any field that supports sorting in your search index. Hit your search resource with a sort query string like https://www.example.org/api/my-fancy-search/Lorem+ipsum?sort=comment_count,-created.
  • Filter the search results by any facet declared by Facet API. Just use the filtering options as you would use them in any other request to RESTful https://www.example.org/api/my-fancy-search/Lorem+ipsum?filter[comment_count][value]=2&filter[comment_count][operator]=“>=”&filter[author][value]=1. This request will return all the indexed content that contains Lorem Ipsum and has two or more comments authored by the administrator.
  • Browse the available facets, and its values along with the search results.
  • Support for limits and pagination.
  • And all the other benefits of RESTful: get the exact output that you need, render caching, authentication providers, …
Output example. See the facets and the custom output format.

All of these follow the same structure that you are used to when exposing your resources with the RESTful module. This way your client components won’t need to understand another set of special query parameters to interact with your search engine.

All in all, I was very satisfied with how easy it was to integrate this particular piece of functionality. To me that meant that anyone that needs to add support to weird things — things that aren’t entities or plain tables in your database— can do it without a big pain. Also, the fact that it can be done from the contrib space is very important. We have just created the Drupal-RESTful GitHub organization, and we want you to start writing small contribs to cover those edge cases. Everything else is already there.

--

--