Elasticsearch Console Query Example in Elasticsearch

By Opster Team

Updated: Jun 22, 2023

| 2 min read

Introduction

Elasticsearch, a powerful search and analytics engine, provides a rich query language that allows users to perform complex searches and aggregations on their data. In this article, we will dive into some advanced Elasticsearch console query examples, focusing on techniques and tips that can help you optimize your search queries and extract valuable insights from your data.

1. Multi-Match Query with Boosting

A multi-match query allows you to search for a term across multiple fields. You can also apply boosting to give more importance to specific fields. Here’s an example:

GET /_search
{
  "query": {
    "multi_match": {
      "query": "apple",
      "fields": ["title^3", "description", "tags"],
      "type": "best_fields"
    }
  }
}

In this example, we search for the term “apple” across the fields “title”, “description”, and “tags”. The “title” field has a boost factor of 3, making it more important than the other fields.

2. Filtering with Bool Query

The bool query allows you to combine multiple queries and filters to narrow down your search results. Here’s an example:

GET /_search
{
  "query": {
    "bool": {
      "must": [
        {"match": {"title": "apple"}}
      ],
      "filter": [
        {"term": {"category": "electronics"}},
        {"range": {"price": {"gte": 100, "lte": 500}}}
      ]
    }
  }
}

In this example, we search for documents with the term “apple” in the title field, and apply two filters: the category must be “electronics” and the price must be between 100 and 500.

3. Aggregations for Data Analysis

Aggregations help you analyze and summarize your data. Here’s an example of a terms aggregation combined with a date histogram aggregation:

GET /_search
{
  "size": 0,
  "aggs": {
    "sales_by_category": {
      "terms": {"field": "category"},
      "aggs": {
        "sales_over_time": {
          "date_histogram": {
            "field": "sale_date",
            "calendar_interval": "month"
          }
        }
      }
    }
  }

In this example, we group the documents by category and then create a date histogram for each category, showing the number of sales per month.

4. Pagination and Sorting

When dealing with large result sets, you can use pagination and sorting to control the order and number of documents returned. Here’s an example:

GET /_search
{
  "from": 10,
  "size": 10,
  "query": {
    "match": {"title": "apple"}
  },
  "sort": [
    {"price": {"order": "asc"}}
  ]
}

In this example, we search for documents with the term “apple” in the title field, sort the results by price in ascending order, and return the second page of results (documents 11-20).

5. Highlighting Search Results

Highlighting allows you to emphasize the matched terms in the search results. Here’s an example:

GET /_search
{
  "query": {
    "match": {"description": "apple"}
  },
  "highlight": {
    "fields": {
      "description": {}
    },
    "pre_tags": ["<strong>"],
    "post_tags": ["</strong>"]
  }
}

In this example, we search for the term “apple” in the description field and highlight the matched terms using the <strong> HTML tag.

Conclusion

These advanced Elasticsearch console query examples demonstrate the power and flexibility of Elasticsearch’s query language. By mastering these techniques, you can optimize your search queries, extract valuable insights from your data, and improve the overall user experience.

How helpful was this guide?

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?