Custom Drupal Search Built with Search API + Database Search + Views

| Drupal Development | 58 seen

The default Drupal search function is pretty much OK, but if you are looking for ways to control and extend search behavior  - you are stuck with the default out-of-the-box search. 

I'll show you how you can boost your search functionality, by using just two (OK, three modules)

Download the following modules:

Download and extract them into the sites/all/modules folder

Enable Database search,  Search API, and Search views

Now we must add a search server and search index.

Let's start with Search server.

  • Go to admin/config/search/search_api

  • Click on Add server link

  • Give it a name - I prefer to call it as DB Server, if you wish, you can enter the Server description. Under the dropdown from Service, the class chose Database service. Click Create Server.

Now go back to admin/config/search/search_api - you should see - your newly created server as enabled, and index as disabled.

  • Click on the Edit button for the Default node index.

  • You must click the check box next to the Enabled button, and under Server, you must choose our newly created Server which we called DB Search (if you called it another way - you should see that name under drop-down box). Click Save settings.

We are almost there - before indexing items, as it will appear in the next window after you will click Save settings, let's add some fields to index.

  • Go to admin/config/search/search_api/index/default_node_index/fields

If you have made some custom changes to your Drupal site, like added custom fields, taxonomies e.t.c. and you would like them to be indexed in your search server - this is the place where to add fields to the search index. If you are OK with the default (Title and body) leave the settings intact.

Now let's index our nodes. You should configure cron, so items are indexed automatically, but if you want manually index your search server, go to admin/config/search/search_api/index/default_node_index

  • Click on Index now button to index 50 items, and repeat it, so all your content is indexed, if you have too many nodes, use a cron job.

Now we are ready to build our Search page.

Create a new View

Admin > Structure > Views > Add new view

  1. View name: Search
  2. Show: [the name of the Search index] (in the case of Default node index the default is "Default node index")
  3. Create a page [tick]
  4. Page title: Search
  5. Path: Search
  6. Display format: Unformatted list of Fields
  7. Items to display: 10, use pager

Continue & edit (the new View)

Format: Show: Fields | Settings

Fields: Title, Body

Filter criteria

  • Fulltext search: Expose this filter, Required, Remember the last selection, Use as search keys
  • Indexed Node: Status: Operator: Is one of, Value: Published

Sort criteria

  • Search: Relevance, descending (if you don’t have an order with fuzzy search you will get a PDO exception)

Page settings

  • Access: Permission: view published content

Advanced

  • No results behavior: Global: Text area “No results matched your search.”

Exposed form

  • Exposed form in block: Yes
  • Exposed form style settings: Submit button text: Search

Save the View. Add the exposed form block to a region.
Note that you will only receive results for partial matches that are longer than the minimum word length specified in the Index configuration.

It's up to you now, how are you going to format the display of your views page. As extra, you could add Facets to be displayed on the sidebar as well.

Here is the final search results page  I made for this site: https://www.reinisfischer.com/do_search?keyword=yerevan

Consider hiring me if you need help with Drupal website development

Add new comment