[英]ElasticSearch filtering on multiple fields (with aggregration)
I am building a faceted filtering function for a webshop, something like this: 我正在为网上商店构建多面过滤功能,如下所示:
Filter on Brand:
[ ] LG (10)
[ ] Apple (5)
[ ] HTC (3)
Filter on OS:
[ ] Android 4 (11)
[ ] Android 5 (2)
[ ] IOS (5)
I am using aggregation and filtering in elasticsearch, which is working out pretty well for me after a few days of learning ES (loving it!). 我在Elasticsearch中使用聚合和过滤,经过几天学习ES(喜欢它!),这对我来说效果很好。 But sadly I got stuck on the actual filtering now. 但是可悲的是,我现在停留在实际过滤上。
If i click on 'LG', the IOS filter will be disabled and (5) will change to (0) and the results on the right side will change to 13 android phones. 如果我单击“ LG”,则将禁用IOS筛选器,并且(5)将更改为(0),并且右侧的结果将更改为13个Android手机。 Great, so far so good. 很好,到目前为止很好。
Now if I click on 'Android 4', only 11 phones will show on the right side. 现在,如果我单击“ Android 4”,则右侧只会显示11个电话。 Awesome! 真棒! So far so good :) 到现在为止还挺好 :)
But now, if i click on 'Android 5', all results disappear. 但是现在,如果我单击“ Android 5”,所有结果都会消失。 I'm not sure what I'm doing wrong. 我不确定自己在做什么错。 I would expect that all LG phones with both Android 4 and 5 show up. 我希望所有配备Android 4和5的LG手机都能出现。
Below is a sample query of the last case. 下面是最后一种情况的示例查询。 Please note there are also some other fields included in the query which I am using to build the faceted filtering. 请注意,查询中还包含其他一些字段,这些字段用于构建分面过滤。
{
"size":100,
"query":{
"filtered":{
"query":{
"match_all":[
]
},
"filter":{
"bool":{
"must":[
{
"term":{
"brand.untouched":"LG"
}
},
{
"term":{
"operating_system.untouched":"Android 4"
}
},
{
"term":{
"operating_system.untouched":"Android 5"
}
}
],
"should":[
],
"must_not":{
"missing":{
"field":"model"
}
}
}
},
"strategy":"query_first"
}
},
"aggs":{
"brand.untouched":{
"terms":{
"field":"brand.untouched"
}
},
"operating_system.untouched":{
"terms":{
"field":"operating_system.untouched"
}
},
"camera1":{
"histogram":{
"field":"camera1",
"interval":5,
"min_doc_count":0
}
},
"price_seperate":{
"histogram":{
"field":"price_seperate",
"interval":125,
"min_doc_count":0
}
}
}
}
Does anyone know the solution? 有人知道解决方案吗? Thanks so much. 非常感谢。
Your query is searching for documents in which operating_system.untouched
is both "Android 4" and "Android 5" which will never be the case and hence you get zero results. 您的查询正在搜索的文档中, operating_system.untouched
既是“ Android 4”又是“ Android 5”,但事实并非如此,因此结果为零。 You can simply make use of Terms Filter
so that documents where the value of operating_system.untouched
is either "Android 4" or "Android 5" matches. 您可以简单地使用Terms Filter
以便operating_system.untouched
的值与“ Android 4”或“ Android 5”匹配的文档。 Below is the updated query you should be using: 以下是您应该使用的更新查询:
{
"size":100,
"query":{
"filtered":{
"filter":{
"bool":{
"must":[
{
"terms":{
"brand.untouched": [
"LG"
]
}
},
{
"terms":{
"operating_system.untouched": [
"Android 4",
"Android 5"
]
}
}
],
"must_not":{
"missing":{
"field":"model"
}
}
}
},
"strategy":"query_first"
}
},
"aggs":{
"brand.untouched":{
"terms":{
"field":"brand.untouched"
}
},
"operating_system.untouched":{
"terms":{
"field":"operating_system.untouched"
}
},
"camera1":{
"histogram":{
"field":"camera1",
"interval":5,
"min_doc_count":0
}
},
"price_seperate":{
"histogram":{
"field":"price_seperate",
"interval":125,
"min_doc_count":0
}
}
}
}
If you want to add another set of categories like price range, you just need to add a bool should
clause inside the bool must
clause. 如果要添加价格范围之类的另一组类别,只需在bool must
子句中添加bool should
子句。 See below for an example when you want to filter on a field price
on two ranges (0, 100]
and (100, 200]
. What this basically means is that you can have nested must
and should
filters to realize any boolean logic you want to implement for filtering in Elasticsearch. 当您想按两个范围(0, 100]
和(100, 200]
的现场price
进行过滤时,请参见下面的示例。这基本上意味着您可以嵌套must
和should
过滤器以实现所需的任何布尔逻辑在Elasticsearch中实现过滤。
...
"must":[
{
"terms":{
"brand.untouched": [
"LG"
]
}
},
{
"terms":{
"operating_system.untouched": [
"Android 4",
"Android 5"
]
}
},
"bool": {
"should": [
{
"range": {
"price": {
"gt": 0,
"lte": 100
}
}
},
{
"range": {
"price": {
"gt": 100,
"lte": 200
}
}
}
]
}
],
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.