ElasticSearch: count product attributes

孤街醉人 提交于 2019-12-24 02:18:48

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!