Home > Enterprise >  how to do exact match in elastic search?
how to do exact match in elastic search?

Time:04-16

I want to find exact match .if there is no match .I don't want to null.

I am search this id 6a8c283f-1e75-ec11-8943-000d3a15f525. it is giving me this output

  • fcf0dd4e-abbc-ec11-983f-0022482588e9

But these it is not matched only ec11 matched

I tried this search query

 result = await client.search({
          index: 'products',
          "query": {
            "match": {
              "sysid":sysid
            }
          }
        })

CodePudding user response:

The problem is because your sysid field is of type text and is thus analyzed at indexing time and tokenized into fcf0dd4e, abbc, ec11, 8943 and 000d3a15f525.

Also when searching using a match query, the searched term is analyzed as well into the following tokens 6a8c283f, 1e75, ec11, 8943, 000d3a15f525.

As you saw, the match works because ec11 matches.

For an exact match, what you need to do is to run your query against a keyword field instead. Maybe you're lucky and your mapping already contains a sysid.keyword sub-field. If not, you need to change your mapping and reindex your data.

Then you'll be able to run your term query like this and get your exact match:

  "term": {
     "sysid.keyword":sysid
  }
  • Related