问题
I have a datatype 'product' which has a field 'attributes' (dynamic object). For example, a product can contain:
attributes: {
"color": "White",
"size": "L",
}
another product can contain
attributes: {
"color": "Black",
"weight": "12 kg",
}
So the set of attributes is not fixed.
My goal is to make faceted search over all attributes of products which were found. I found a way to manually define each attribute in 'aggregations' section:
"aggregations": {
"attribute_color": {
"terms": {
"field": "attributes.color"
}
},
"attribute_size": {
"terms": {
"field": "attributes.size"
}
)
}
But I don't know in advance which attributes will be found so I want to make this list dynamically. I assume this can be done in 2 steps: find all attribute keys (color, size, weight, etc) and then make aggregation query for these fields. So the question is how I can get all attributes keys from the search result?
If I'm wrong and it can be done in 1 step, please point me to the right solution. Thanks!
来源:https://stackoverflow.com/questions/26255310/elasticsearch-count-product-attributes