问题描述
大家好.这个问题涉及多方面的搜索.
Good day to all. The question concerns faceted search.
假设有2个过滤器:
2.1类别货运(1765)汽车(1566)其他(8675)
2.1 CategoriesFreight (1765)Cars (1566)Any other (8675)
2.2种颜色红色(5689)绿色(156)蓝色(3599)黄色(2562)
2.2 ColorsRed (5689)Green (156)Blue (3599)Yellow (2562)
正如我们在每个过滤器前面看到的那样,它指示了弹性中分别存储了多少元素.在运费"前面打勾.
As we see in front of each filter, it is indicated how many elements are individually stored in elastic.Put a tick in front of the "freight".
现在的行为:
2.1类别货运(1765)汽车(0)还有(0)
2.1 CategoriesFreight (1765)Cars (0)Any more (0)
2.2种颜色红色(红色货号)绿色(绿色货运数量)蓝色(蓝色货运数量)黄色(黄色货运数量)
2.2 ColorsRed (red freight number)Green (number of green freight)Blue (number of blue freight)Yellow (number of yellow freight)
您需要这种行为:
2.1类别货运(1765)汽车(1566)其他(8675)
2.1 CategoriesFreight (1765)Cars (1566)Any other (8675)
2.2种颜色红色(红色货号)绿色(绿色货运数量)蓝色(蓝色货运数量)黄色(黄色货运数量)
2.2 ColorsRed (red freight number)Green (number of green freight)Blue (number of blue freight)Yellow (number of yellow freight)
也就是说,特定字段上的过滤器不会影响其聚合,但会影响所有其他过滤器.如何实现优化呢?现在实现了x个弹性请求,并且x等于过滤器数量
That is, that the filter on a specific field does not affect its aggregation, but affects all others. How can this be implemented optimized? Now implemented for x requests to elastic, and x is equal to the number of filters
最美好的祝愿
推荐答案
假定初始查询为 match_all
,则查询
Assuming the initial query is match_all
, the query for
2.2颜色红色(5689)绿色(156)蓝色(3599)黄色(2562)
2.2 Colors Red (5689) Green (156) Blue (3599) Yellow (2562)
将是:
{
"query": {
"match_all": {}
},
"aggs": {
"CATEGORIES": {
"terms": {
"field": "category"
}
},
"COLORS": {
"terms": {
"field": "color"
}
}
}
}
当的 强>被选择的期望是什么由步骤说明步骤如下:
When Freight
is selected what is expected is explained step by step as below:
这可以通过在 category
字段上查询字词来实现.现在,如果在聚合之前应用此查询,则会导致问题中提到的问题. CATEGORIES
方面将与 Frieght
相对,而其他计数为零.尽管 COLORS
方面将具有预期的计数.为了解决这个问题,我们可以使用post_filter .这样可以确保在准备汇总后对记录进行过滤.
This can be achieved using terms query on category
field. Now if this query is applied before aggregation, it results into the problem mentioned in the question. The CATEGORIES
facet will have count against Frieght
and other counts will be zero. Though the COLORS
facet will have expected counts. To solve this we can make use of post_filter. This will make sure that filtering of records is done after preparing aggregations.
这是它的工作方式:
步骤1: match_all(原始查询)
Step 1: match_all(original query)
步骤2:准备汇总
第3步:应用过滤器(预期的搜索结果)
Step 3: apply the filter (the expected search result)
通过上述操作,我们将获得正确的过滤结果和预期的 CATEGORIES
构面,但是 COLORS
中的计数仍然相同,根据选择的数量,预期会减少 CATEGORIES
方面.下一步可以解决此问题.
By the above we will achieve correct filtered results and expected count CATEGORIES
facet, but the counts in COLORS
are still same which were expected to reduce according to the selection in the CATEGORIES
facet. The next step fixes this.
为解决这个问题,我们将过滤器聚合与实际聚合一起使用.我们将在应进行计数的其余每个聚合中应用post_filter,即除 CATEGORIES
以外的所有聚合,在我们的情况下,这些聚合都是 COLORS
.
To deal with this we will use filter aggregation along with the actual aggregation. We will apply the post_filter in each of the remaining aggregations where the counts should be effected i.e. all aggregations other than CATEGORIES
which in our case is only COLORS
.
结合以上两个步骤,查询将是:
Combining the above two steps the query will be:
{
"query": {
"match_all": {}
},
"aggs": {
"CATEGORIES": {
"terms": {
"field": "category"
}
},
"COLORS": {
"filter": {
"terms": {
"category": [
"Freight"
]
},
"aggs": {
"COLORS": {
"terms": {
"field": "color"
}
}
}
}
},
"post_filter": {
"terms": {
"category": [
"Freight"
]
}
}
}
}
这篇关于如何处理方面筛选和相应的聚合计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!