我们目前正在使用Elasticsearch为网站上的产品列表提供支持。过滤也由Elasticsearch管理,我们遇到了关键值聚合和过滤的问题。
TL; DR: 对键/值字段进行聚合"功能"应用过滤器:
{
"features": {
"global": {},
"aggregations": {
"features": {
"filter": {
"bool": {
"must": [
{
"nested": {
"path": "features",
"query": {
"bool": {
"minimum_should_match": "1",
"must": [
{
"term": {
"features.code": "brand"
}
}
],
"should": [
{
"term": {
"features.value.code": "CKJ"
}
}
]
}
}
}
}
]
}
},
"aggregations": {
"features": {
"nested": {
"path": "features"
},
"aggregations": {
"key": {
"terms": {
"field": "features.code",
"min_doc_count": 1,
"size": 50,
"order": {
"_term": "asc"
}
},
"aggregations": {
"value": {
"terms": {
"size": 50,
"order": {
"_term": "asc"
},
"min_doc_count": 1,
"field": "features.value.descriptions.description.NL"
},
"aggregations": {
"code": {
"terms": {
"field": "features.value.code"
}
}
}
}
}
}
}
}
}
}
}
}
}
问题:我们可以写这个,这样我们可以选择哪个键获取过滤器(而不是所有键)。
长版:
这就是我们的数据:
{
"price": 59.95,
"id": "123456789",
"colours": [
{
"id": 39672,
"description": {
"EN": "Multi colour",
"NL": "Multi kleur",
"FR": "Multi couleur",
"DE": "Multi colour"
},
"images": [ ],
"primaryimage": "4055157573965.jpg",
"swatch": "4055157573965.jpg"
}
,
{
"id": 40383,
"description": {
"EN": "Multi colour",
"NL": "Multi kleur",
"FR": "Multi couleur",
"DE": "Multi colour"
},
"images": [ ],
"primaryimage": "4055157573637.jpg",
"swatch": "4055157573637.jpg"
}
],
"size": [
"36"
],
"features": [
{
"id": null,
"code": "brand",
"key": {
"NL": "Merk",
"FR": "Marque",
"DE": "Marke",
"EN": "Brand"
},
"value": {
"descriptions": [
{
"description": {
"DE": "BRAND"
}
}
,
{
"description": {
"NL": "BRAND"
}
}
,
{
"description": {
"EN": "BRAND"
}
}
,
{
"description": {
"FR": "BRAND"
}
}
],
"code": "brand"
}
}
,
{
"id": null,
"code": "gender",
"key": {
"FR": "Genre",
"DE": "Geschlecht",
"EN": "Gender",
"NL": "Geslacht"
},
"value": {
"descriptions": [
{
"description": {
"FR": "Dames"
}
}
,
{
"description": {
"NL": "Dames"
}
}
,
{
"description": {
"EN": "Dames"
}
}
,
{
"description": {
"DE": "Dames"
}
}
],
"code": "1"
}
}
,
{
"id": null,
"code": "groupLevel1",
"key": {
"DE": "GroupLevel1",
"FR": "GroupLevel1",
"NL": "GroupLevel1",
"EN": "GroupLevel1"
},
"value": {
"descriptions": [
{
"description": {
"EN": "Level1"
}
}
,
{
"description": {
"FR": "Level1s"
}
}
,
{
"description": {
"NL": "Level1"
}
}
,
{
"description": {
"DE": "Level1"
}
}
],
"code": "119D"
}
}
]
}
这是我们在数据上运行的(简化的)查询:
{
"from": 0,
"size": "18",
"sort": [
"_score"
],
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"online": 1
}
},
{
"term": {
"visible": true
}
},
{
"term": {
"web_product.user": 111
}
},
{
"term": {
"id_navigation": "281"
}
},
{
"nested": {
"path": "features",
"query": {
"bool": {
"minimum_should_match": "1",
"must": [
{
"term": {
"features.code": "brand"
}
}
],
"should": [
{
"term": {
"features.value.code": "CKJ"
}
}
]
}
}
}
}
]
}
}
}
},
"_source": {
"include": [
"*.NL",
"id_product",
"product_code",
"colours.id",
"colours.primaryimage",
"colours.swatch",
"price"
]
},
"aggregations": {
"colours": {
"filter": {
"bool": {
"must": [
{
"term": {
"id_navigation": "281"
}
},
{
"term": {
"online": 1
}
},
{
"term": {
"visible": true
}
},
{
"term": {
"web_product.id_tenant": 115
}
},
{
"nested": {
"path": "features",
"query": {
"bool": {
"minimum_should_match": "1",
"must": [
{
"term": {
"features.code": "brand"
}
}
],
"should": [
{
"term": {
"features.value.code": "CKJ"
}
}
]
}
}
}
}
]
}
},
"aggregations": {
"colours": {
"terms": {
"field": "colours.description.NL.facet",
"min_doc_count": 1,
"size": 50,
"order": {
"_term": "asc"
}
}
}
}
},
"features": {
"global": {},
"aggregations": {
"features": {
"filter": {
"bool": {
"must": [
{
"term": {
"id_navigation": "281"
}
},
{
"term": {
"online": 1
}
},
{
"term": {
"visible": true
}
},
{
"term": {
"web_product.user": 111
}
},
{
"nested": {
"path": "features",
"query": {
"bool": {
"minimum_should_match": "1",
"must": [
{
"term": {
"features.code": "brand"
}
}
],
"should": [
{
"term": {
"features.value.code": "CKJ"
}
}
]
}
}
}
}
]
}
},
"aggregations": {
"features": {
"nested": {
"path": "features"
},
"aggregations": {
"key": {
"terms": {
"field": "features.code",
"min_doc_count": 1,
"size": 50,
"order": {
"_term": "asc"
}
},
"aggregations": {
"value": {
"terms": {
"size": 50,
"order": {
"_term": "asc"
},
"min_doc_count": 1,
"field": "features.value.descriptions.description.NL"
},
"aggregations": {
"code": {
"terms": {
"field": "features.value.code"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
此查询运行良好,可以很好地过滤数据和聚合。 在这个例子中重要的是2个聚合:"颜色"和"功能"。过滤这两个聚合以显示查询的相应方面。 我们正在使用多重选择,因此如果我们过滤颜色,我们将不会过滤聚合以显示所有其他可用颜色。除了功能聚合之外,这一切都适用于我们的所有聚合。
功能聚合是一个键值聚合,并将返回多个方面:
"features": {
"doc_count": 2327,
"features": {
"doc_count": 11,
"features": {
"doc_count": 66,
"key": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "brand",
"doc_count": 11,
"value": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Calvin Klein",
"doc_count": 11,
"code": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "CKJ",
"doc_count": 11
}
]
}
}
]
}
}
,
{
"key": "gender",
"doc_count": 11,
"value": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "WOMEN",
"doc_count": 11,
"code": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "WOMEN",
"doc_count": 11
}
]
}
}
]
}
}
,
{
"key": "groupLevel1",
"doc_count": 11,
"value": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "BLAZERS",
"doc_count": 1,
"code": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "520300",
"doc_count": 1
}
]
}
}
,
{
"key": "CARDIGANS",
"doc_count": 1,
"code": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "540200",
"doc_count": 1
}
]
}
}
,
{
"key": "HEMDEN KORTE MOUWEN",
"doc_count": 1,
"code": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "510000",
"doc_count": 1
}
]
}
}
,
{
"key": "HEMDEN LANGE MOUWEN",
"doc_count": 1,
"code": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "510100",
"doc_count": 1
}
]
}
}
,
{
"key": "JURKEN",
"doc_count": 1,
"code": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "560100",
"doc_count": 1
}
]
}
}
,
{
"key": "ROKKEN",
"doc_count": 1,
"code": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "560000",
"doc_count": 1
}
]
}
}
,
{
"key": "SJAALS",
"doc_count": 1,
"code": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "590700",
"doc_count": 1
}
]
}
}
,
{
"key": "SWEATERS",
"doc_count": 2,
"code": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "540000",
"doc_count": 2
}
]
}
}
,
{
"key": "T-SHIRTS LANGE MOUWEN",
"doc_count": 1,
"code": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "530100",
"doc_count": 1
}
]
}
}
,
{
"key": "TRUIEN",
"doc_count": 1,
"code": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "540100",
"doc_count": 1
}
]
}
}
]
}
}
]
}
}
}
},
"colours": {
"doc_count": 11,
"colours": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Blauw",
"doc_count": 1
}
,
{
"key": "Grijs",
"doc_count": 4
}
]
}
}
添加了颜色过滤器以进行比较。
现在,当我们在前端选择一个特征方面时,我们当然也希望有多重选择。但是,当我们将过滤器应用于要素聚合时,将过滤所有关键点,从而产生一个不可多选的方面。如果我们删除要素聚合上的过滤器,则不会过滤其他键,从而导致选择不正确的构面。
所以我的问题是:我们可以根据密钥过滤值/密钥聚合吗?逻辑将是这样的:如果键==品牌,那么不要过滤其他过滤器
如果需要更多示例,我可以添加它们。
提前致谢。