将搜索结果缩小到多个类别 [英] Narrowing search result to multiple categories
问题描述
我正在尝试将搜索结果缩小到任何给定的类别。
我的搜索body,在elasticsearch上工作2.2.0
I am trying to narrow down search results to be within any given categories. My search body that works on elasticsearch 2.2.0
{
"filter":{
"bool":{
"should":[
{
"match":{
"categories":"16310211"
}
},
{
"match":{
"categories":"493964"
}
}
]
}
},
"size":25,
"from":0
}
然而,现在我们使用的是AWS托管服务,它们只支持1.5.6。当我运行相同的代码时,我会得到一个http 400的不良请求。如何在1.5.6上实现多个条件(一个或多个)过滤结果的相同效果?
However now we are using AWS hosted services and they only provide support for 1.5.6. When I run the same code I get back a http 400 bad request. How can I achieve the same effect of filtering results by multiple criteria (one or more) on 1.5.6?
推荐答案
如果你确认使用多个匹配
查询,那么您需要将它们与 bool
查询即可。尝试这样:
If you're committed to using multiple match
queries, then you will need to combine them with a bool
query. Try this:
{
"query":{ <--- use query here instead of filter
"bool":{
"should":[
{
"match":{
"categories":"16310211"
}
},
{
"match":{
"categories":"493964"
}
}
]
}
},
"size":25,
"from":0
}
但是,通常使用匹配
查询进行全文搜索。它看起来好像你的类别是确切的值。我会为这种类型的数据推荐一个术语
过滤器。您可能有以下更好的体验:
However, generally the match
query is used for doing full text searching. It looks as if your "categories" are exact values. I would recommend a term
filter for this type of data instead. You may have a better experience with the following:
{
"query":{
"filtered":{
"filter":{
"bool":{
"should":[
{ "term":{"categories":"16310211"} },
{ "term":{"categories":"493964"} }
]
}
}
}
},
"size":25,
"from":0
}
这篇关于将搜索结果缩小到多个类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!