方面,并根据方面进行一些过滤 [英] facets and doing some filtering based on facets
问题描述
我有一个关于构面的问题,并根据构面进行了一些过滤.我知道这是一个重复的问题,但我找不到答案.
I have a question about facets and doing some filtering based on facets. i know this is a repeated question but i am unable find the answer.
我想知道如何在弹性搜索中实现相同的功能.
i would like to know how can i implement the same functionality in elastic search.
假设我有关于汽车和某些方面的索引-例如.模型和颜色.
lets asume that I have an index about cars and some facets -- eg. model and color.
颜色
[]红色(10)
[]蓝色(5)
[]绿色(2)
模型
[]宝马(4)
[]大众(5)
[]福特(8)
如果我选择一个模型,则只想获取该模型的颜色面,但我仍然想了解所有型号的产品.例如:
if I select a model I would like to get only color facets for that model, but I still would like to get facets for all models. eg:
颜色
[]红色(2)
[]蓝色(2)
[]绿色(1)
模型
[]宝马(4)
[x]大众(5)
[]福特(8)
我搜索了关于该用例的示例,但没有找到示例.这是可能,如果可以,如何过滤查询以获得这些结果?
I have searched I did not find an example about this use-case. Is this possible and if yes, how do I filter a query to get these results?
亲切的问候
推荐答案
我确定这里已经多次回答了,但让我们举一个具体的例子.
I'm sure this has been answered multiple here but let's take your concrete example.
创建索引
PUT lalit
{
"mappings": {
"properties": {
"model": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
},
"color": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
摄取一些文档
POST lalit/_doc
{"color":"red","model":"bmw"}
POST lalit/_doc
{"color":"blue","model":"bmw"}
POST lalit/_doc
{"color":"red","model":"vw"}
POST lalit/_doc
{"color":"green","model":"vw"}
POST lalit/_doc
{"color":"blue","model":"ford"}
应用过滤条件agg
GET lalit/_search
{
"size": 0,
"aggs": {
"all_models": {
"terms": {
"field": "model.keyword"
}
},
"all_colors": {
"terms": {
"field": "color.keyword"
}
},
"model_filtered_colors": {
"filter": {
"term": {
"model.keyword": "vw"
}
},
"aggs": {
"actual_aggs": {
"terms": {
"field": "color.keyword"
}
}
}
}
}
}
屈服
"aggregations" : {
"model_filtered_colors" : {
"doc_count" : 2,
"actual_aggs" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "green",
"doc_count" : 1
},
{
"key" : "red",
"doc_count" : 1
}
]
}
},
"all_models" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "bmw",
"doc_count" : 2
},
{
"key" : "vw",
"doc_count" : 2
},
{
"key" : "ford",
"doc_count" : 1
}
]
},
"all_colors" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "blue",
"doc_count" : 2
},
{
"key" : "red",
"doc_count" : 2
},
{
"key" : "green",
"doc_count" : 1
}
]
}
}
model_filtered_colors
通过颜色为您提供所有 vw
,而其他2种汇总则为您提供全部总计(不包括 vw
过滤器).
model_filtered_colors
gives you all vw
s by color while the other 2 aggregations give you the totals across the board (w/o the vw
filter).
这篇关于方面,并根据方面进行一些过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!