我有产品,有很多功能。架构看起来像这样
[{
name: "Test product 1",
features: [
...
{name: "color", value: "Blue"},
{name: "size", value: "M"},
...
]
},{
name: "Test product 2",
features: [
...
{name: "color", value: "Blue"},
{name: "size", value: "XL"}
...
]
},{
name: "Test product 3",
features: [
...
{name: "color", value: "White"},
{name: "size", value: "XL"}
...
]
}]
我希望获得每个功能的每个不同值的方面。像这样:
{
color: {
Blue: 2,
White: 1
},
size: {
M: 1,
XL: 2
}
}
如何设计正确的索引架构以及如何查询此方面?我应该创建两个不同的数组,一个用于特征名称,另一个用于类型&#34;对象&#34; ...然后进行多个查询,首先是特征名称,然后是每个嵌套方面?... < / p>
抱歉,我很困惑,并且不了解如何通过方面获得此结果
答案 0 :(得分:3)
为什么要尝试使用facet? Facets弃用,使用聚合..
以下是使用构面和聚合进行搜索的方法。 1.创建索引并放置映射
PUT x3
POST x3/x2/_mapping
{
"x2":{
"properties": {
"features" :{
"type": "nested"
}
}
}
}
POST x3/x2/1
{
"name" :"Test product 1",
"features": [
{"name": "color", "value": "Blue"},
{"name": "size", "value": "M"}
]
}
POST x3/x2/2
{
"name": "Test product 2",
"features": [
{"name": "color", "value": "Blue"},
{"name": "size", "value": "XL"}
]
}
使用构面搜索
POST x3/x2/_search
{
"size": 0,
"facets": {
"sizeFacet": {
"terms": {
"field": "features.value",
"size": 10000
},
"facet_filter": {
"term": {
"features.name": "color"
}
},
"nested": "features"
},
"colorFacet": {
"terms": {
"field": "features.value",
"size": 10000
},
"facet_filter": {
"term": {
"features.name": "size"
}
},
"nested": "features"
}
}
}
使用聚合搜索..
POST x3/x2/_search
{
"size": 0,
"aggs": {
"features": {
"nested": {
"path": "features"
},
"aggs": {
"names": {
"terms": {
"field": "features.name",
"size": 0
},
"aggs": {
"value": {
"terms": {
"field": "features.value",
"size": 0
}
}
}
}
}
}
}
}