Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in AWS by (19.1k points)

How to find an exact match of multiple text values with IN operation? e.g.,

SELECT * WHERE name in ('charlie','alan') AND testID in (1,2)

When I try to run below query, it returns extra data for matching values e.g., "the charlie", not for the exact values only at "charlie"

{  

   "query":{  

      "bool":{  

         "filter":[  

            {  

               "query_string":{  

                  "fields":[  

                     "name"

                  ],

                  "query":"charlie or alan"

               }

            },

            {  

               "terms":{  

                  "testID":[1,2]

               }

            }

         ]

      }

   }

}

1 Answer

0 votes
by (44.4k points)

The reason behind this is because your field name is of type text.

Elasticsearch breaks the values when the data type is text. For instance, if you provide The Charlie or Charlie Puth, it will take the values like this [the, charlie] or [charlie, path] and it will be saved in the inverted index. So, right now if you query, it will return these documents which you consider to not match. 

You can solve this by using lowercase normalizer. The mapping looks like this:

PUT <your_index_name>

{  

   "settings":{  

      "analysis":{  

         "normalizer":{  

            "my_custom_normalizer":{  

               "type":"custom",

               "filter":[  

                  "lowercase"

               ]

            }

         }

      }

   },

   "mappings":{  

      "mydocs":{  

         "properties":{  

            "name":{  

               "type":"keyword",

               "normalizer":"my_custom_normalizer"

            }

         }

      }

   }

}

Now, the data entered like The Charlie will be stored as the charlie and you stored in an inverted index. So, now you can query and get data as you expect.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...