0

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?

1 Answer 1

1

There is currently no way to get the nested documents that matched the query. It's a known issue and work/thought has been given to fixing it. The current state of the issue is that the fetch-phase of Elasticsearch needs to be refactored to support this feature.

It's important and will happen, because the refactoring will also benefit other parts of elasticsearch too, but it is a major refactor. There is no current ETA on when it will be available.

I would consider denormalizing your data so that each variant contains the "root object" data, and then doing grouping inside your client.

Alternatively, you could investigate parent/child mappings but similar restrictions apply (cannot determine which child matched a has_child query), but it does give a bit more flexibility since children are independent documents which can be searched

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.