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

How to deploy Elasticsearch on an Azure Virtual Machine

October 20, 2025

How to deploy Elasticsearch on an Azure Virtual Machine

Learn how to deploy Elasticsearch on Azure VM with Kibana for full control over your Elasticsearch setup configuration.

How to use the Synonyms UI to upload and manage Elasticsearch synonyms

October 14, 2025

How to use the Synonyms UI to upload and manage Elasticsearch synonyms

Learn how to use the Synonyms UI in Kibana to create synonym sets and assign them to indices.

How to reduce the number of shards in an Elasticsearch Cluster

October 8, 2025

How to reduce the number of shards in an Elasticsearch Cluster

Learn how Elasticsearch shards affect cluster performance in this comprehensive guide, including how to get the shard count, change it from default, and reduce it if needed.

How to deploy Elasticsearch on AWS Marketplace

October 3, 2025

How to deploy Elasticsearch on AWS Marketplace

Learn how to set up and run Elasticsearch using Elastic Cloud Service on AWS Marketplace in this step-by-step guide.

HNSW graph: How to improve Elasticsearch performance

September 29, 2025

HNSW graph: How to improve Elasticsearch performance

Learn how to use the HNSW graph M and ef_construction parameters to improve search performance.

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