Intro To Search API (Part 1) – How To Create Search Pages

Want to learn more about search in Drupal 7? Then check out our online video course called “Build Powerful Search Pages with Views and Search API in Drupal 7“.

The core Search module in Drupal 7 is great for simple search pages however, the configuration options are fairly limited. If you want to change the look and feel of the search results, you could use the Display Suite Search sub-module that ships with Display Suite.

You can go one step further and create a custom search page using just Views. All you need to do is create a page display and expose the Search: Search Terms filter, and you’re done. But the filter still relies on the index data that the Search module creates.

If you want flexibility and control over your search pages, then you should take a serious look at the Search API module. The biggest benefit of using the module is that it supports a number of search backends like Apache Solr, Xapian and MongoDB or you can store the index directly into a database.

In this tutorial, we’ll create a custom search page using Views and Search API for the results. For simplicity we’ll use the Search API Database Search module as the backend.

Getting Started

Before we can begin, download Search API, Search API Database Search, Entity API and Views.

If you use Drush, run the following command:

$ drush dl search_api search_api_db entity views ctools

Once you have downloaded the modules, enable the following:

  • Search API
  • Search views
  • Database search
  • Entity API
  • Views
  • Views UI
  • Ctools

Create a Search API Server

The first step required for our search page, is to create a Search API server. This will let Search API know how and where the index will be stored. For this tutorial, we’ll use the Search API Database Search module to store our index in the database. If we were to use Apache Solr, then we would use the Search API Solr module.

Steps

1. Go to Configuration -> Search API and click on the “Add server” link.

2. Enter “Database server” into the “Server name” field and select “Database service” from the “Service class” drop-down list.

Fig 1.0

3. Save the form by clicking on “Save settings”.

Create a Search API Index

In the last section, we created a Search API server. Now, we need to create a Search API index and define which fields should be stored in the index.

Steps

1. Go to Configuration -> Search API and click on the “Add index” link.

2. Enter “Article index” into the “Index name” and select Node from the “Item type” drop-down list.

Fig 1.1

3. Further down the page, select “Database server” from the Server drop-down list and click on “Create index”.

Fig 1.2

Define Index Fields

On the Fields page, we have to define which fields will be indexed. When you’re building a large index, it takes a bit of trial and error to get it right.

For now, we’ll just index the title, body and tags field.

1. Check the Title checkbox and click on “Save changes”.

2. Click on the “Add related fields” field-set and select “The main body text” and click on “Add fields”.

Fig 1.3

3. After adding the related field, check the “The main body text » Text” checkbox and click on “Save changes”.

4. Again, click on the “Add related fields” field-set and this time select Tags.

Fig 1.4

5. After adding the related tags field, check the “Tags » Name” and “Tags” field and click on “Save changes”.

Define Workflow

The Workflow page allows you to run specific data alterations and processors on the index data. But for this tutorial, we’ll keep it simple and leave this page empty.

Index Content

Now that we have setup our Search API index, the next step is to index some data.

Click on the Status tab and click on “Index now”.

Fig 1.5

Create Search Page

At this point, we have created a Search API server, index and we indexed some content. The final piece of the puzzle, is to create the actual search page.

You can create search pages by simply using Views or by using the Search API pages module. But in this tutorial we’ll use Views because this gives you the most flexibility.

1. Go to Structure -> Views (admin/structure/views) and click on “Add new view”.

2. Select “Article index” from the Show drop-down.

Fig 1.6

If you can’t see “Article index”, then make sure you enabled the “Search views” module.

Fill out the rest of the page with the values defined in Table 1.0.

Table 1-0. Create a new view

Option Value
View name Search page
Machine name search_page
Show Article index
Create a page Checked
Page title Search page
Path search-page
Display format Unformatted list (default)

Once the form has been filled out, click on “Continue & edit”.

3. Click on Add within the “Filter criteria” area and select “Search: Fulltext search” and click on “Apply (all displays)”.

Fig 1.7

4. Check the “Expose this filter to visitors, to allow them to change it” checkbox and click on “Apply (all displays)”.

5. Click on Fields within the Format area and select “Rendered entity” and click on “Apply (all displays)”.

6. Select Teaser from the View mode drop-down list and click on “Apply (all displays)”.

7. Save the view and go to /search-page and test the page.

Fig 1.7

As you have seen, Search API gives you a lot of flexibility. However, you have to spend time setting up the server, index and views display. In part two, we’ll look at faceted search using the Facet API.

If you have any questions, please leave a comment.

About The Author

38 thoughts on “Intro To Search API (Part 1) – How To Create Search Pages”

  1. I was hardly lookin for that. Thx again. You saved me a lot of headache. Because the Standard-Search-Function in Drupal is not good.

  2. also want to say thanks… one of the few well documented and easy to understand tutorials on how to integrate search into drupal. for how much time drupal devs spend on maintaining modules, i think they’d save a lot of time for themselves if they provided some upfront instructions like this!

  3. Nice tutorial really. Just wondering after the search. The searched words are not highlighted. Is there any setting to make it work?

  4. Alexander Sibert

    Thank you for the tutorial but what i search is a solution to add a views block additional to the views page to integrate it with panels on my frontpage. Do you know a solution?

    1. If I understand you correctly, you want to add a views block to a Panels page?

      You should be able to simply add the block using Panels.

      1. Alexander Sibert

        Thank you for your reply. I mean i have created successfully a search API Views page. Now i want add a search API Views “Block”. This created block i want to use to show it on front page and if i search with it then it should go over the created Views search page.

        1. On your search API Views page, add an exposed filter. Then, under the Advanced section, set the “Exposed form in block:” to Yes.

          This will allow you to place the exposed filters anywhere within Panels.

  5. Fabio Gameleira

    I’m new on Drupal 7 with Slor Search and this tutorial is very, very, very good.

    Now I wil see part 2.

    Thanks from Brazil.

  6. Hello I’m glad you took your time to make such an detailed tutorial. I have one question: is there an ability to change the position of the Fulltext Search?

  7. When I create an index View the Use Aggregation option is not available. Is there a way to aggregate (group) indexed fields that have the same value to eliminate duplicate (or multiple) records?

    Thanks.

    1. The “Use Aggregation” option is not available when you use a Search API index as a backend in Views.

      I’m not sure how it can be done, I would ask in the Search API issue queue.

  8. Hello Ivan.

    thank you very much for your tutorial. I found it pretty helpful for my project.
    One question, do you know if there is a way to index both users and nodes in one search page?

    Thanks,

    Janeth

  9. I have a content type with a field type of node reference displaying multiple values. When uploading content, the field works fine showing multiple values. But I want to use that indexed field as one of my filters for my search in my view, but for some reason it does not pick up that field as a node reference with multiple values anymore. I checked Search API index from the configuration and the field is there as a type node. Is there something that I might be missing.

    NB: All my other taxonomy type indexed fields work 100% as filters.

    Thanking you in advance.

      1. No Ivan, in my content type, the field type node reference to get multiple values, just like I have field type term reference for multiple values, or field type date.

        My term reference indexed fields works fine in my view as filters for my search, but the one that is of node reference does not work. I will email you screenshots of what am trying to explain.

        1. Thanks for the screenshot. I don’t really know what the problem could be. In Drupal 7 I’ve always used entity reference instead of node reference. Try entity reference if possible. I’ve had great success with the module and Search API.

  10. Ajay Kumar Gudivada

    Thanks alot for providing such a detailed information. Could you please let me know the way to add custom menu items created through hook_menu in the search_api_db

    1. Search API can’t index menus, not without custom code. More importantly, why do you want to index them? Could you not index the page that they point to?

  11. Ajay Kumar Gudivada

    Let’s consider a View/Custom Menu Page where we display a list of node teasers. Now, we need to index the View Page/Custom Menu Page and doesn’t need to index the node pages. Please do guide me to index such pages.

    1. You can change the ranking in many ways. Your question is hard to answer because it’s very generic.

      I would google “drupal search api ranking” or “drupal search api biased”

  12. Hi ,
    Is there any search field for the indexed view in which we can filter out the results based on the Letters ,instead of providing the entire word as a input to the Fulltext text search Field .

    1. Hi Abdul,

      Not sure how to do this.

      To index content in general, words need to be a minimum amount of characters.

      Cheers,
      Ivan

  13. I am using a search api module for search, however while i search, On the search result, it gives some records left empty and some record are displayed. when i keeps on clicking the search button those empty record become displayed. In view setting i have already tick the ” hide empty filed ” . Please i need your help to get this thing fix. I have attached herewith the image shows the problem. Is it because of Search API indexing issues ?

  14. I want partial search as well
    How can we achieve this thing with above configuration?

    Also, I have one more exposed filter texonomy with full text,
    I want to have OR condition between this two filter how can we achieve this?

    In views it’s already “OR”, But somehow its always use “And” when there are full-text search and taxonomy available.

    1. Hi Jayesh,

      I’ve never set up partial search so I’m not sure how to do it.

      Regarding, AND/OR, look in the Search API issue queue if there’s a bug about it.

      Cheers,
      Ivan

  15. Andre Marcanth

    Hi, I have reproduced this tutorial, but I would like to know what kind of boolean operators I can use with this. Like AND, OR, and “An exact sentence”.
    Thank you,

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top