Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Compound Queries in Elasticsearch

Introduction

Elasticsearch's Query DSL (Domain Specific Language) provides a rich and flexible mechanism to query data. Compound queries allow you to combine multiple queries in a single request, offering powerful ways to filter, score, and combine results based on various conditions.

Bool Query

The bool query is one of the most commonly used compound queries. It allows you to combine multiple queries using boolean logic.

The bool query supports four types of clauses:

  • must: All queries must match.
  • filter: All queries must match, but these clauses do not contribute to scoring.
  • should: At least one of these queries must match.
  • must_not: None of these queries must match.

Example

{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "Elasticsearch" } }
      ],
      "filter": [
        { "term": { "status": "published" } }
      ],
      "must_not": [
        { "range": { "publish_date": { "lte": "2020-01-01" } } }
      ],
      "should": [
        { "match": { "tags": "search" } },
        { "match": { "tags": "analytics" } }
      ]
    }
  }
}
                
This query searches for documents with the title "Elasticsearch", are published, and not published before 2020. Additionally, it boosts documents with tags "search" or "analytics".

Constant Score Query

The constant_score query wraps another query and assigns a constant score to all documents matching the query, ignoring the relevance scores.

Example

{
  "query": {
    "constant_score": {
      "filter": {
        "term": { "status": "published" }
      },
      "boost": 1.5
    }
  }
}
                
This query filters documents with the status "published" and assigns a constant score of 1.5 to all matching documents.

Dis Max Query

The dis_max query is used to combine multiple queries and return the document score from the query with the highest score.

Example

{
  "query": {
    "dis_max": {
      "queries": [
        { "match": { "title": "Elasticsearch" } },
        { "match": { "content": "Elasticsearch" } }
      ],
      "tie_breaker": 0.7
    }
  }
}
                
This query searches for documents that match either the title or content with "Elasticsearch" and uses a tie breaker to adjust the scores.

Function Score Query

The function_score query allows you to modify the score of documents that are retrieved by a query. This is useful for customizing relevance scoring.

Example

{
  "query": {
    "function_score": {
      "query": { "match": { "title": "Elasticsearch" } },
      "boost": "5",
      "functions": [
        {
          "filter": { "match": { "tags": "important" } },
          "weight": 2
        },
        {
          "filter": { "range": { "publish_date": { "gte": "2021-01-01" } } },
          "weight": 3
        }
      ],
      "score_mode": "sum",
      "boost_mode": "multiply"
    }
  }
}
                
This query boosts documents with the title "Elasticsearch", further boosting documents tagged as "important" or published after 2021. The scores are combined by summing up and then multiplying by the boost.

Wrapping Up

Compound queries in Elasticsearch provide a powerful way to create complex search queries by combining multiple queries and controlling how they interact. Understanding and utilizing these queries allows for more precise and effective search solutions.