Elasticsearch search by two fields

Exploring techniques for searching by two fields, including multi-match queries, bool queries, and query-time field boosting.

Searching across multiple fields in Elasticsearch is a common requirement in many applications. In this article, we will explore advanced techniques to perform searches by two fields, including multi-match queries, bool queries, and query-time field boosting. These techniques will help you to create more accurate and relevant search results for your users.

Advanced techniques to perform searches by two fields

1. Multi-match query

A multi-match query allows you to search for a single query string across multiple fields. This is useful when you want to find documents that contain the given query string in either of the two fields. Here’s an example of a multi-match query searching for the term “example” in the fields “title” or “description”:

{
  "query": {
    "multi_match": {
      "query": "example",
      "fields": ["title", "description"]
    }
  }
}

2. Bool query

A bool query allows you to combine multiple queries using boolean logic. You can use the “should” clause to search for documents that match the query in either of the two fields. Here’s an example of a bool query searching for the term “example” in the fields “title” and “description”:

{
  "query": {
    "bool": {
      "should": [
        {"match": {"title": "example"}},
        {"match": {"description": "example"}}
      ]
    }
  }
}

3. Query-time field boosting

Sometimes, you may want to give more importance to one field over another during the search. You can achieve this by applying a boost factor to the field at query time. A higher boost value gives more weight to the field, making it more likely to influence the final search score. Here’s an example of a multi-match query with a boost factor applied to the “title” field:

{
  "query": {
    "multi_match": {
      "query": "example",
      "fields": ["title^3", "description"]
    }
  }
}

In this example, the “title” field has a boost factor of 3, making it three times more important than the “description” field in determining the search score.

4. Combining queries with different boost factors

You can also combine multiple queries with different boost factors using a bool query. This allows you to fine-tune the importance of each field in the search results. Here’s an example of a bool query with different boost factors applied to the “title” and “description” fields:

{
  "query": {
    "bool": {
      "should": [
        {"match": {"title": {"query": "example", "boost": 3}}},
        {"match": {"description": {"query": "example", "boost": 1}}}
      ]
    }
  }
}

In this example, the “title” field has a boost factor of 3, while the “description” field has a boost factor of 1.

Conclusion

Searching by two fields in Elasticsearch can be achieved using advanced techniques such as multi-match queries, bool queries, and query-time field boosting. By combining these techniques, you can create more accurate and relevant search results for your users. Experiment with different query combinations and boost factors to find the optimal search configuration for your specific use case.

Ready to try this out on your own? Start a free trial.

Want to get Elastic certified? Find out when the next Elasticsearch Engineer training is running!

Related content

Understanding Elasticsearch scoring and the Explain API

May 7, 2025

Understanding Elasticsearch scoring and the Explain API

Diving into the scoring mechanism of Elasticsearch and exploring the Explain API.

Removing a node from an Elasticsearch cluster

May 5, 2025

Removing a node from an Elasticsearch cluster

Explaining how to remove a node from an Elasticsearch cluster.

Joining two indexes in Elasticsearch

May 9, 2025

Joining two indexes in Elasticsearch

Explaining how to use the terms query and the enrich processor for joining two indexes in Elasticsearch.

RAG and the value of grounding

May 1, 2025

RAG and the value of grounding

Learn about RAG, grounding and how to reduce hallucinations by connecting an LLM to your documents.

RAG without “AG”?

April 29, 2025

RAG without “AG”?

Learn how to leverage semantic search and ELSER to build a powerful and visually appealing Q&A experience, without using LLMs.

Ready to build state of the art search experiences?

Sufficiently advanced search isn’t achieved with the efforts of one. Elasticsearch is powered by data scientists, ML ops, engineers, and many more who are just as passionate about search as your are. Let’s connect and work together to build the magical search experience that will get you the results you want.

Try it yourself