Etan Grundstein
Etan Grundstein

Reputation: 405

Item variants in ElasticSearch

What is the best way to use item variants in elasticsearch and retrieving only 1 item of the variant group?

For example, let's say I have the following items:

[{
    "sku": "abc-123",
    "group": "abc",
    "color": "red",
    "price": 10
  },
  {
    "sku": "def-123",
    "group": "def",
    "color": "red",
    "price": 10
  },
  {
    "sku": "abc-456",
    "group": "abc",
    "color": "black",
    "price": 20
  }
]

The first item and the last one are in the same group, so I want only to return one of them if I query for items below the price of 20 (for example), but with the best hit score.

Feel free to suggest documents design and queries accordingly.

Upvotes: 1

Views: 435

Answers (1)

Hatim Stovewala
Hatim Stovewala

Reputation: 1251

If your mapping is of Nested datatype, then you can use this to retrieve them.

GET index/type/_search
{
  "size": 2000, 
  "_source": false,
  "query": {
    "bool": {
      "filter": {
        "nested": {
          "path": "childs",
          "query": {
            "bool": {
              "filter": {
                "term": {
                  "childs.group.keyword": "abc"
                }
              }
            }
          },
          "inner_hits": {} 
        }
      }
    }
  }
}

Upvotes: 1

Related Questions