Hi folks.
I have an index with documents that have a product_id
(long) and product_subtype
(text). I'm trying to search for documents using multiple product_id
s and product_subtype
s.
The data that I have is this:
[
{
"id": 1,
"product_id": 123,
"product_subtype": "subtype abc"
},
{
"id": 2,
"product_id": 123,
"product_subtype": "subtype def"
},
{
"id": 3,
"product_id": 456,
"product_subtype": "subtype abc"
},
{
"id": 4,
"product_id": 789,
"product_subtype": "subtype abc"
},
{
"id": 5,
"product_id": 789,
"product_subtype": "subtype def"
}
]
I want to search for products with product_subtype = "subtype abc" OR product_subtype = "subtype def"
, and that belong to product_id 123 OR 456
.
The result I expect is ids 1, 2 and 3 from the data above.
My initial approach was to do this query, but this also returns ids 4 and 5:
{
"query": {
"bool": {
"should": [
{ "match_phrase": { "product_subtype": "subtype abc" } },
{ "match_phrase": { "product_subtype": "subtype def" } },
{ "terms": { "product_id": [123, 456] } }
]
}
}
}
I then tried to move the product_id
part to must
instead of should
. This works if I query for both product_subtype
s (abc
and def
), but if I query for just abc
, it still returns results for def
(although now respecting the product_id
s):
{
"query": {
"bool": {
"should": [
{ "match_phrase": { "product_subtype": "subtype abc" } },
{ "match_phrase": { "product_subtype": "subtype def" } }
],
"must": [
{ "terms": { "product_id": [123, 456] } }
]
}
}
}
The last thing I tried was moving everything to must
, but then it doesn't return anything.
Is there a way that I could accomplish this query with ES?
Thanks in advance