Elasticsearch query not giving exact match
Asked Answered
S

4

13

Am searching elasticsearch with the below match query, which is not giving me the exact match instead its giving some more irrevalant match also.

am using elasticsearch 6.2.3

Please find my query below

get items/_search
{
   "query" : {
      "match" : {
         "code" : "7000-8900"
      }
   }
}

Please find the response am getting it from match query

7000-8900
7000-8002-WK
7000-8002-W
Schweinfurt answered 12/6, 2018 at 13:26 Comment(3)
Try term instead of matchUniflorous
Am not getting any results in term instead of matchSchweinfurt
this is exactly the goal of elasticsearch ^^ search against tokens, keep in mind that your document fields are tokenized into words (tokens), search operation is made against these words (document and query).Semiporcelain
G
21

Instead of match you have to use term query, as the documentation describe:

The term query finds documents that contain the exact term specified in the inverted index

So you have to change your query as follow:

get items/_search
{
   "query" : {
      "term" : {
         "code.keyword" : "7000-8900"
      }
   }
}

If you don't get any result there are two possibilities:

  • The term searched is not what you think it really is (for example is not trimmed)
  • The index has no explicit mapping and the automatic mapping didn't recognize the field code as a string.

Note: if the mapping is correct and code is a term field it is possible to use "code". If the mapping was automatic and the mapping recognize it as text you need to use "code.keyword"

Gilliam answered 12/6, 2018 at 13:28 Comment(11)
No results with termSchweinfurt
Check if there is some space or special char around codeGilliam
You might need to use code.keyword with the term query and not just codeUniflorous
its working properly for other fields like "name" : "product7265"Schweinfurt
yes it might be The index has no explicit mapping and the automatic mapping didn't recognize the field code as a string. ., what shall i do for this?Schweinfurt
Probably name was mapped as keyword, while code as textGilliam
@Schweinfurt check the updated answer (thx @Val) and use code.keyword not codeGilliam
Its works., but in some case i need to search like "code.keyword" : "7000-8002-*" this is not working. i just want to search anything ( * )Schweinfurt
This is not a term (exact) search. In this case you need to use a wildcard query elastic.co/guide/en/elasticsearch/reference/current/…Gilliam
For elastic search 8.4 above query returns exact match only, How can i return results like mentioned in the questionRecalcitrate
The question was asking for exact matching. If you need a like or similar search this is not the question / answer for you @RahulKumarGilliam
L
11

I had the same issue with the match, so I tried to use term. But it is a bad practice. ES says, we should not use term for string matching.

If you specify the field as keyword, the match will do the exact match anyway.

If you haven't defined the field as keyword, you can still do the query like this:

get items/_search
{
   "query" : {
      "match" : {
         "code.keyword" : "7000-8900"
      }
   }
}
Lifework answered 14/12, 2019 at 0:9 Comment(1)
It worked. I checked my column mapping on which i was applying search. it was "text" and type was "keyword". { "query": { "bool": { "filter": [ { "term": { "node_title.keyword": "5-Verify-Offhours" } } ] } } }Society
S
4

You can try this method.This query return exact match record.

import json
from elasticsearch import Elasticsearch

es = Elasticsearch('http://localhost:9200')
res = es.search(index="test_index", doc_type="test_doc", body=json.dumps({"query": {"match_phrase": {"name": "Jhon"}}})))
Sedimentation answered 20/7, 2018 at 6:44 Comment(0)
H
0
You can also search exact with .raw:
get items/_search
{
   "query" : {
      "match" : {
         "code.raw" : "7000-8900"
      }
   }
}
Hebrew answered 24/10, 2022 at 21:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.