Elasticsearch Query DSL

KATEGORİ

,

Elasticsearch query DSL (Domain Specific Language) is a JSON based query language that defines queries in elasticsearch. When your data is indexed, you can create criteria for filtering and retrieving data.

We can send http requests to elasticsearch using a REST client such as postman.

Run a post command to insert some data to elasticsearch.

Then you can retieve this document with a search query.

In a post request, we can write complex queries by defining a query in request body.

{
    "query":{
        "term":{
            "id":3
        }
    }
}

or 

{
    "query":{
        "term":{
            "text":"tasimacilik"
        }
    }
}

In Elasticsearch Query DSL, the “match” query is used for full-text search.

{
  "query": {
    "match": {
      "text": "tasimacilik tren"
    }
  }
}

In this example, Elasticsearch would search for documents where the text field contains any of the terms “tasimacilik” or “tren”.

If we use a “term” query, it’ll look for an exact match. In below query elasticsearch would search for documents that text field contains an exact match with term “tasimacilik tren”. It will return an empty result because term query is not suitable for searching multiple words. We should use a single word.

{
    "query":{
        "term":{
            "text":"tasimacilik tren"
        }
    }
}

How can we search whole sentence with term query? We’ll use keyword.

{
    "query":{
        "term":{
            "text.keyword":"tcdd tasimacilik a.s elastic 1"
        }
    }
}

This query will search whole sentence. if we use a single word or multiple words, result will be empty again because keyword search only whole sentence. This query will also work with “match” but term is a bit faster.

The wildcard query supports two wildcard characters.

*(asteriks) – match zero or more character

?(question mark) – match exactly one character

For example “tc*” will search for documents containing terms start with “tc”.

Similar to wildcard we can use query_string for a full-text search combining more than one criteria.

{
  "query": {
    "query_string": {
      "query": "tc* OR lo*"
    }
  }
}