简体   繁体   中英

exact match with IN Operator in elasticsearch

How to find exact match of multiple text values with IN operation? eg,

SELECT * WHERE name in ('george','michael') AND testID in (1,2)

When I try to run below query, it returns extra data for matching values eg, " the george ", not for the exact values only as " george "

{  
   "query":{  
      "bool":{  
         "filter":[  
            {  
               "query_string":{  
                  "fields":[  
                     "name"
                  ],
                  "query":"george or michael"
               }
            },
            {  
               "terms":{  
                  "testID":[1,2]
               }
            }
         ]
      }
   }
}

That is because your field name is of type text .

When datatype if of text , Elasticsearch breaks the values(for eg The George or George Washington into tokens [the, george] or [george, washington] and saves these tokens in inverted index. Hence when you search on this field using the query you have, it would also return these documents iewhat you say as not exact match.

For exact match I suggest you make use of keyword type using lowercase normalizer as mentioned in below mapping:

PUT <your_index_name>
{  
   "settings":{  
      "analysis":{  
         "normalizer":{  
            "my_custom_normalizer":{  
               "type":"custom",
               "filter":[  
                  "lowercase"
               ]
            }
         }
      }
   },
   "mappings":{  
      "mydocs":{  
         "properties":{  
            "name":{  
               "type":"keyword",
               "normalizer":"my_custom_normalizer"
            }
         }
      }
   }
}

By doing so your values would be saved as is for eg based on above mapping, it would convert The George or George Washington into the george or george washtingon and then store them as is in the inverted index.

You can probably have the query you've mentioned then working as expected.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM