Search code examples
elasticsearchelastic-stackelasticsearch-5elk

how to write Elastic search query for exact match for a string


I am using kibanna I am trying to put filter on a field container_name = "armenian" but I have other container names with following names

  1. armenian_alpha
  2. armenian_beta
  3. armenian_gama
  4. armenian1
  5. armenian2

after putting the filter , search query in kibanna becomes

{
  "query": {
    "match": {
      "container_name": {
        "query": "armenian",
        "type": "phrase"
      }
    }
  }
}

But the output searches logs for all containers , as I can see the Elastic search query is using a pattern matching

How can I put an exact match with the string provided and avoid the rest ?


Solution

  • You can try out with term query. Do note that it is case sensitive by default unless you specify with case_insensitive equals to true. Also, if your container_name is a text field type instead of keyword field type, do add the .keyword after the field name. Otherwise, ignore the .keyword.

    Example:

    GET /_search
    {
      "query": {
        "term": {
          "container_name.keyword": {
            "value": "armenian"
          }
        }
      }
    }
    

    Link here: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html