I am trying to build a facet navigation using elasticsearch for an ecommerce website.
The shop products can have multiple variants. The document structure I thought of looks like this:
PUT /products_test/product/1
{
"id": "1",
"manufacturer": "foobar",
"categories": [
"28554568",
"28554577",
"28554578"
],
"variants": [
{
"id": "1_a",
"color": "blue",
"size": "L"
"price": "67.99"
},
{
"id": "1_b",
"color": "red",
"size": "L"
"price": "69.99"
}
]
}
I defined variants as nested type. This way the following query will return all documents which contain a variant matching the filters:
POST /products_test/product/_search
{
"query": {
"filtered": {
"query": {"match_all": {}},
"filter": {
"and": [
{"term": {"categories": "28554568"}},
{"terms": {
"variants.color": [
"red"
]
}}
]
}
}
}
}
Now, i really would like to get the id of the variant that matched the filter in order to display that variant of the product on the category page. So in the case of the example I would like to have the id of the second variant (1_b) return. I only get the id of the document returned in the resultset. Is there any way to define a value from the nested object be returned also?