Elasticsearch logo with a search magnifier and result rows, illustrating useful Elasticsearch queries
Back to Blog
Backend Development
July 7, 2021
Arifur Rahman

5 Useful Elasticsearch Queries

Five Elasticsearch queries worth knowing, and how its query patterns differ from the SQL you are used to when you need to get data back out.

Elasticsearch logo with a search magnifier and result rows, illustrating useful Elasticsearch queries

Elasticsearch is NoSql Database. It stores data in an unstructured way as an Index and to retrieve data need to follow it's own query pattern rather regular SQL query.

There are several types of query pattern are used in Elasticsearch. We will talk 5 most useful queries are used in Elasticsearch.

1- Bool Query

The AND/OR/NOT operators can be used to fine tune our search queries in order to provide more relevant or specific results. This is implemented in the search API as a bool query. The bool query accepts a must parameter (equivalent to AND), a must_not parameter (equivalent to NOT), and a should parameter (equivalent to OR). For example, if you want to search for a book with the word "Elasticsearch" OR "Solr" in the title, AND is authored by "clinton gormley" but NOT authored by "radu gheorge"

POST /bookdb_index/book/_search
{
  "query": {
    "bool": {
      "must": {
        "bool" : { 
          "should": [
            { "match": { "title": "Elasticsearch" }},
            { "match": { "title": "Solr" }} 
          ],
          "must": { "match": { "authors": "clinton gormely" }} 
        }
      },
      "must_not": { "match": {"authors": "radu gheorge" }}
    }
  }
}

[Results]
"hits": [
  {
    "_index": "bookdb_index",
    "_type": "book",
    "_id": "1",
    "_score": 2.0749094,
    "_source": {
      "title": "Elasticsearch: The Definitive Guide",
      "authors": [
        "clinton gormley",
        "zachary tong"
      ],
      "summary": "A distibuted real-time search and analytics engine",
      "publish_date": "2015-02-07",
      "num_reviews": 20,
      "publisher": "oreilly"
    }
  }
]

2- Basic Match and Multi-match Query

The multi_match keyword is used in place of the match keyword as a convenient shorthand way of running the same query against multiple fields. The fields property specifies what fields to query against and, in this case, we want to query against all the fields in the document.

The multi_match keyword is used in place of the match keyword as a convenient shorthand way of running the same query against multiple fields. The fields property specifies what fields to query against and, in this case, we want to query against all the fields in the document.

//Match Query
{
 "query": {
        "match" : {
            "query" : "guide",
            "field" : "title
        }
    }
}

//For Multi-match Query
{
    "query": {
        "multi_match" : {
            "query" : "guide",
            "fields" : ["title", "authors", "summary", "publish_date", "num_reviews", "publisher"]
        }
    }
}

3-Match Phrase Prefix

Match phrase prefix queries provide search-as-you-type or a poor man's version of autocomplete at query time without needing to prepare your data in any way. It will check particular field prefix match against full document.

POST /bookdb_index/book/_search
{
    "query": {
        "match_phrase_prefix" : {
            "summary": {
                "query": "search en",
                "slop": 3,
                "max_expansions": 10
            }
        }
    },
    "_source": [ "title", "summary", "publish_date" ]
}

4- Term/Terms Query

Term query will search for exact match rather than match or match-prefix query. Term query looking does not allow partial match.

Multiple terms can be specified by using the terms keyword instead and passing in an array of search terms.

POST /bookdb_index/book/_search

//Term Query Eample
{
    "query": {
        "term" : {
            "publisher": "manning"
        }
    },
    "_source" : ["title","publish_date","publisher"]
}


//Terms Query Example

{
    "query": {
        "terms" : {
            "publisher": ["oreilly", "packt"]
        }
    }
} 

5-Range Query

Range queries work on date, number, and string type fields. It helps to fetch data from a specific range of data, number, or ids. On query need to define gather than as 'gte' and less than as 'lte'.

POST /bookdb_index/book/_search
{
    "query": {
        "range" : {
            "publish_date": {
                "gte": "2015-01-01",
                "lte": "2015-12-31"
            }
        }
    },
    "_source" : ["title","publish_date","publisher"]
}

Find the post on IG here

Building something bigger than a code snippet? ARITS does custom software development.

Need extra engineers, or a team to run the build?

Tell us the shape of the work and your deadline. You will get a straight read on what it takes and what it will cost, from a team that has shipped 400+ projects out of Dhaka and London.

Cookies

We use cookies to see how people find this site and to measure our ads. You can turn those off — everything here still works. Privacy policy

Necessary

Keeps the site working and remembers this choice.

Always on

Analytics

Anonymous stats on which pages get read, so we know what to write next.

Marketing

Lets us measure which ads brought someone here.