elasticsearch 6.0 query component examples

Elasticsearch 6.0 queries example using curl with simple steps.


In our example we will learn query component and its variation.For demo purpose we will use cURL.

cURL is a command line tool that can be used to transfer data over HTTP.In our example we will execute cURL command from cygwin.

For searching ,i am first indexing more data so that searching can be understood properly

I am indexing document like as follows.

  • curl -XPUT 'localhost:9200/preparationforinterview/elasticsearch/5?pretty' -H 'Content-Type: application/json' -d '{ "name" : "sam", "profession" : "engineer"}'
  • curl -XPUT 'localhost:9200/preparationforinterview/elasticsearch/6?pretty' -H 'Content-Type: application/json' -d '{ "name" : "amit", "profession" : "doctor"}'
  • curl -XPUT 'localhost:9200/preparationforinterview/elasticsearch/7?pretty' -H 'Content-Type: application/json' -d '{ "name" : "joe", "profession" : "teacher"}'
  • Explain

    syntax of cURL command is

    http://localhost:9200/index-name/type-name/document-name

    As we know in Elasticsearch every index have type and every type have document.In our example preparationforinterview is index name and elasticsearch is a type name while 5,6,7 is document name. For REST API Elasticsearch uses 9200 port as default port.

    Elasticsearch automatically adds index , type and mapping in that type.

    Elasticsearch automatically detects that name and profession is text and if new documents is added with more fields then Elasticsearch finds their type and adds it into mapping

  • After indexing document we will execute following command to check all documents.

  • curl "localhost:9200/preparationforinterview/elasticsearch/_search?pretty"



  • Now we will learn query component and its variation one by one.
  • Elasticsearch match_all Example


  • It will display all fields without any filter.
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query": {
    "match_all": {}
    }
    }'
    


    Elasticsearch match Example

  • It wil return all documents that matches this field value.In our case we are trying to get all document who has profession as engineer.Note that it will analyze input request and construct more basic query.In our exampple either we search engineer or Engineer,in both case we will get same output.
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query": {
    "match": {
       "profession" : "engineer"
    }
    }
    }'
    


    Elasticsearch query_string Example

  • By default it will return all fields if any any field is matched.For example in our case two document has same name sam ,so if we search using query_string query then it will return both documents with all fields.
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query": {
    "query_string" : {
    "query": "sam"
    }
    }
    }'
    


    Note :

    By default query_string query searches in all fields but if we want to search in particular field then syntax is as follows.In our case if we want to check in name field.

    curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query": {
    "query_string" : {
    "query": "name:sam"
    }
    }
    }'
    

    We can also mention default_field if nothing is mentioned as field

    curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query": {
    "query_string" : {
    "default_field" : "name",
    "query": "sam"
    }
    }
    }'
    

    Elasticsearch term Example

  • It wil return all documents that exact matches this field value.In our case we are trying to get all document who has profession as engineer.
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query": {
    "term": {
       "profession" : "engineer"
    }}
    }'
    


    Elasticsearch term filter Example

  • term fiter is same as term query but term filter does not calculate score.So same result we can get as below as well.In our case we are trying to get all document who has profession as engineer.
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
        "query": {
            "bool": {
                "must": {
                   "match_all": {}
                },
                "filter": {
                    "term": {
                       "profession": "engineer"
                    }
                }
            }
        }
    }'
    


    Elasticsearch terms query Example

  • It wil return all documents that exact matches this field value.In terms multiple values is searched in a documents fields.In our case we are trying to get all document who has profession as engineer or doctor.
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query": {
    "terms": {
       "profession" : ["engineer","doctor"]
    }}
    }'
    


    Elasticsearch multi_match Example

  • It will return all documents that's search value matches across multiple fields value. In terms multiple values is searched in a documents against field while in multi_match values are searched against multiple fields.In our case we are trying to get all documents whose name and profession fields match with search value.
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
      "query": {
          "multi_match": {
            "query": "sam amit",
            "fields" : ["name","profession"]
          }
      }
    }'
    


    Elasticsearch Compound Query Example

    In compound query different queries are merged using boolean operaton.We will understand compound query using simple example.

    Elasticsearch bool Example

  • In bool query we can combine multiple queries in single query by specifying query clause. In query clause we can specify which part should , must , must_not match with documents. To understand we take one example.I have added one field degree in our documents using query. For example
  • curl -XPOST 'localhost:9200/preparationforinterview/elasticsearch/7/_update?pretty' -H 'Content-Type: application/json' -d '{
    "doc": {
    "degree": ["A","C","D"]
    }
    }'
    

    Before executing bool query we see what do we have in our index now.

    curl "localhost:9200/preparationforinterview/elasticsearch/_search?pretty"



  • In bool query we can specify following parts as query
      1. must : It will returned all documents if must query part matches.It is equivalent to boolean AND.For example (q1 AND q2).
        should : It will returned all documents if should query part matches or not.It is equivalent to boolean OR.For example (q1 OR q2) but using minimum_should_match we can specify how many parametr should match.Default is 1 if must is no there and 0 if must is there.
        must_not : It will returned all documents if must_not query parameter is not part of document..It is equivalent to boolean NOT.For example (NOT q1 AND NOT q2).
  • Now we will make our query.We want to search all documents which must have degree D and should have degree either B or C but must not have profession engineer
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
      "query": {
        "bool": {
          "must": [
          {
            "match": {"degree":"D"}
          }
          ],
          "should": [
             {"match":{"degree":"B"}},
             {"match":{"degree":"C"}}
          ],
          "must_not": [
          {
             "term":{"profession":"engineer"}
          }
          ],
          "minimum_should_match": 1
        }
        }
    }'
    


    Elasticsearch range Query Example

  • To understand range query we are adding one field rank in our elasticsearch type using following query
  • curl -XPOST 'localhost:9200/preparationforinterview/elasticsearch/5/_update?pretty' -H 'Content-Type: application/json' -d '{
    "doc": {
    "rank": "2"
    }
    }'
    

    WE have added a 1, 2 and 4 rank , now we will search document who has rank between 1 and 4 so we get all document who have rank 2.

    curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query": {
    "range": {
       "rank" : {
         "gt" : 1,
         "lt" : 4
       }
    }
    }
    }'
    


    Elasticsearch range Query Example with filter

  • Same operation we can do by filter as well.
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
        "query": {
            "bool": {
                "must": {
                   "match_all": {}
                },
                "filter": {
                     "range" : {
                       "rank" : {
                         "gt" : 1,
                          "lt" : 4
                        }
                      }
                }
            }
        }	
    }'
    


    Elasticsearch prefix query Example

  • In prefix query it will return all document that's field matches with this prefix.In our case it will return all document whose field name 's value start with sa.prefix query is not analyzed so sa and Sa is different and exact prefix should match.
  • curl 'localhost:9200/preparationforinterview/elasticsearch/_search?pretty' -H 'Content-Type: application/json' -d '{
    "query": {
      "prefix": {
        "name": "sa"
      }
    }
    }'
    





Visit Others Links

Elasticsearch Introduction
Elasticsearch 6.0 Setup
Elasticsearch Indexing And Searching
Elasticsearch 6.0 indexing example using curl
Elasticsearch 6.0 searching example using curl
Elasticsearch 6.0 updating example using curl
Elasticsearch 6.0 updating example using curl when document does not exist
Elasticsearch 6.0 deleting example using curl
Elasticsearch 6.0 request body based searching
Elasticsearch 6.0 query component example
Elasticsearch 6.0 Analyze API example
Top Elasticsearch Interview Question
© 2019 PreparationForInterview.com