PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 25, 2022

[FIXED] how to write Elastic search query for exact match for a string

 October 25, 2022     elastic-stack, elasticsearch, elasticsearch-5, elk     No comments   

Issue

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



Answered By - ShengHow95
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing