Search code examples
elasticsearchelasticsearch-java-apielasticsearch-query

How can I find exactly phrase with an array field in Elasticsearch?


I have index with field singers mapping like:

"singers": {
    "type": "string",
    "index_name": "singer",
    "analyzer": "unicode_analyzer"
}

Example data like:

"singers": [
    "Đàm Vĩnh Hưng",
    "Thanh Lam (NSƯT)"
]

Now I want to find if index has exactly singer name "Đàm Vĩnh Hưng", how can I do that?

Currently, I tried:

{
   "query": {
      "terms": {
         "singers": [
            "Đàm Vĩnh Hưng"
         ]
      }
   }
}

But the result is empty.

Any advice will be welcome. Thank you guys.

After tries, I resolved this when I use match_phrase. I will keep this for anyone who meet the same problem. Please close this. Thank you.


Solution

  • Use match_phrase.

    {
       "query": {
          "match_phrase": {
             "singers": "Đàm Vĩnh Hưng"
          }
       }
    }