S
S
Sergey2017-08-03 19:15:12
elasticsearch
Sergey, 2017-08-03 19:15:12

How to filter nested object in ES?

Hello.
How to filter nested object in ES?
There are for example 3 products with options.

{
  name: "Product 1",
  price: 10.00
  options: [
      {
        value: "red",
        option: {
          name: "color"
        }
      },
      {
        value: "square",
        option: {
          name: "figure"
        }
      },
      {
        value: 100,
        option: {
          name: "height"
        }
      }
    ]
},
{
  name: "Product 2",
  price: 20.00
  options: [
      {
        value: "black",
        option: {
          name: "color"
        }
      },
      {
        value: "circle",
        option: {
          name: "figure"
        }
      },
      {
        value: 200,
        option: {
          name: "height"
        }
      }
    ]
},
{
  name: "Product 3",
  price: 30.00
  options: [
      {
        value: "blue",
        option: {
          name: "color"
        }
      },
      {
        value: "triangle",
        option: {
          name: "figure"
        }
      },
      {
        value: 300,
        option: {
          name: "height"
        }
      }
    ]
}

I need to get a list of colors. Those. array with ["red", "black", "blue"]
I make a request
{
  "query": {
    "bool": {
      "filter": [
        /** Тут дополнительные фильтры **/
      ]
    }
  },
  "aggs": {
    "list": {
      "terms": {
        "field": "options.value"
      }
    }
  }
}

Naturally, I get an array ["red", "square", 100, "black", ... 300]
How can I get only the values ​​I need? Already tried everything!
I have not slept for 3 days, I swallow valerian and sing coffee :)
Explain please.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2017-08-03
@bujanov

https://www.elastic.co/guide/en/elasticsearch/refe... so about.
The only thing is that it would be nice to make your options nested, otherwise the filter may not work correctly, since after the filter ALL options values ​​will be taken.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question