Xenofexs
Xenofexs

Reputation: 511

Match exact string with only one letter in array

I've this json from elastic :

"_source":{
  "secteurs":[
     "TEST-A",
     "B",
  ]
}

And this request :

{"query":{"match":{"secteurs":{"query":"A"}}}}

I don't understand why one object with "TEST-A" is returned. I want only the "A" secteurs, not the "TEST-A"

I've try with "query_string", it's the same result.

Mapping :

"secteurs":{
   "type":"text",
   "fields":{
      "keyword":{
         "type":"keyword",
         "ignore_above":256
      }
   }
}

Upvotes: 0

Views: 58

Answers (1)

Gibbs
Gibbs

Reputation: 22964

Yes, As I suspected. It is the behaviour of standard analyser.

When you index TEST-A, it will be stored as

 {
    "tokens": [
        {
            "token": "test",
            "start_offset": 0,
            "end_offset": 4,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "a",
            "start_offset": 5,
            "end_offset": 6,
            "type": "<ALPHANUM>",
            "position": 1
        }
    ]
}

Hence it matches a.

So to solve your problem, you can use the below

{"query":{"match":{"secteurs.keyword":{"query":"A"}}}}

Note: keyword is not_analyzed version of the data. So case sensitive too.

Upvotes: 1

Related Questions