elasticsearch version: 6.4

Here is my current data:

enter image description here

I want to search for products which has Xbox in name. I am using the match keyword but that is not working.

Below is my elastic search query:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "name": {
                            "query": "xbox"
                        }
                    }
                },
                {
                    "terms": {
                        "deep_sub": [
                            "Konsol Oyunları",
                            "Konsol Aksesuarları"
                        ]
                    }
                }
            ]
        }
    },
    "from": 0,
    "size": 50
}

1 Answers

0
Kamal On

Whenever you face such kind of issues, try breaking down the query. You have Match Query and Term Query. Run both of them individually and see what's not working.

From what I understand, looks like your field deep_sub is of text type and this would mean Term Query is not returning results.

You would need to create its sibling equivalent using keyword type and then run Term Query on it for exact matches.

From the above link we have the below info:

Keyword fields are only searchable by their exact value.

If you do not control the mapping, meaning if your mapping if of dynamic type, then you must have its sibling equivalent keyword field available which would be deep_sub.keyword

You can check by running GET <your_index_name>/_mapping

Your query would then be as follows:

POST <your_index_name>/_search
{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "match":{  
                  "name":{  
                     "query":"xbox"
                  }
               }
            },
            {  
               "terms":{  
                  "deep_sub.keyword":[         <----- Change This
                     "Konsol Oyunları",
                     "Konsol Aksesuarları"
                  ]
               }
            }
         ]
      }
   },
   "from":0,
   "size":50
}

Let me know if this helps!