I am using Elastic Search 8.2
I have a field named product_name
(type - text, keyword) that contains names of products.
The product names have spaces. Ex: Orbital Keys, XPress Bottle Opener, InstaPress Coffee Maker, Uno Comfort Wear etc.
I want only documents for Orbital Keys or InstaPress Coffee Maker.
I tried using, terms
(using must
because I have other conditions to add):
{
"query":{
"bool":{
"must":[
{
"terms":{
"product_name": ["Orbital Keys", "InstaPress Coffee Maker"]
}
}
]
}
}
}
This didn't return any results.
I tried should
and match_phrase
and it works:
{
"query":{
"bool":{
"should":[
{
"match_phrase":{
"product_name": "Orbital Keys"
}
},
{
"match_phrase":{
"product_name": "InstaPress Coffee Maker"
}
}
]
}
}
}
Is should
the best way to find multiple phrases within a field or is there any other way to approach this problem?
Appreciate your help
CodePudding user response:
You made use of the Term query, in this case you have to use the keyword field in the query because the match is exact.
I imagine your mapping looks like this:
{
"mappings": {
"properties": {
"product_name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
Your query with Terms should be this:
GET test/_search
{
"query":{
"bool":{
"must":[
{
"terms":{
"product_name.keyword": ["Orbital Keys", "InstaPress Coffee Maker"]
}
}
]
}
}
}