Dru Sellers

Leverage Search Templates in Elasticsearch

Leverage Search Templates in Elasticsearch

Tools like Heroku or Amazon Lambda make it easy to ship changes to your code, but what about fine-tuning your Elasticsearch search queries? Perhaps you want to quickly adjust the boost, or the fields that are queried and segmented. You might consider changing the application code, and then test and deploy those changes. That’s a lot of hassle and risk for a few experimental tweaks to a search box though.

Fortunately, Elasticsearch has a handy feature that separates the query definition from the query execution called search templates. With search templates, you can register a query and replace parts of it using the mustache templating language. Your development team can then execute the query using just the name and the required parameters. Here’s a quick how-to:

Register a Template

POST _scripts/<templatename>
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match": {
          "title": "{{query_string}}"
        }
      }
    }
  }
}

Execute a Template

GET _search/template
{
  "id": "<templateName>",
  "params": {
    "query_string": "search for these words"
  }
}

Using search templates allows you to separate the definition from the execution. You can test and tweak the query as much as you want. As long as you don’t change the output or the params, the application code won’t have to change. Now you can now start to play with those templates, so you can provide a better search experience for your customers with a more rapid feedback cycle.

Imagine you have a search bar on the home page. This is the main search used by your customers. In the beginning of a project’s lifecycle, you might be happy just to have search functionality. As your users perform more searches though, you decide to work on your search relevance. So you register a search template that looks roughly like this:

POST _scripts/home_page_search
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "match": {
          "_all": "{{query}}"
        }
      }
    }
  }
}

Then later your developer can simply call

GET _search/template
{
  "id": "home_page_search",
  "params": {
    "query": "gloves"
  }
}

Nice and easy for everyone.

Let’s fast forward a few months, and your search results are not yielding clicks. You’ve learned through trial and error that you should really be boosting your title field over your other fields. This would have been a big hassle at previous projects. But you, ever the forward thinker, implemented this feature using search templates. All that you need to do now is simply change the template, maybe to something that looks like this:

POST _scripts/home_page_search
{
  "script": {
    "lang": "mustache",
    "source": {
      "query": {
        "function_score": {
          "query": {
            "match": {
              "_all": {
                "query": "{{query}}"
              }
            }
          },
          "field_value_factor": {
            "field": "title",
            "modifier": "log1p",
            "factor": 1.01
          },
          "boost_mode": "sum"
        }
      }
    }
  }
}

This change will boost results that have matches in the title field over the other fields, and then sort them to the top. You may find that you need to do a variety of boosts and buries, or drive results down, in order to achieve the best results for your customers. Like any other change to your search structure, you will want to test these changes to make sure that the results behave the way you expect. Fortunately, none of these tweaks will impact other members of your development team.

Do you have any other questions about search templates? Drop us a comment. Then click here to get started with Bonsai Elasticsearch on Manifold.

Special thanks to Dru Sellers and the rest of the One More Cloud team for submitting this post for us to publish on their behalf as part of Manifold’s 12 Days of Services.

StratusUpdate

Keep your head in the cloud with Stratus Update

With our new weekly newsletter we’ll keep you up to date with a curated selection of the the latest cloud services, projects and best practices.
Click here to read the latest issue.