我一直在通读文档并尝试实现通过多个字段和列过滤结果的解决方案,但是我不断收到错误; 格式错误的查询。
我想用完全相等的值过滤结果,例如:
is_active: true
category_id: [1,2,3,4]
brand: "addidas"
gender: "male"
为了更清楚地说明我打算做什么,如果用 SQL 编写的话,我希望它以这种方式运行:
SELECT .... WHERE
is_active= 1 AND category_id IN(1,2,3,4)
AND brand='addidas' AND gender='male'
我在 DSL 中的查询如下:
{
"body": {
"query": {
"nested": {
"query": {
"bool": {
"must": {
"terms": {
"category_id": [
1,
2,
3
]
},
"term": {
"is_active": true
},
"term": {
"brand": "addidas"
}
}
}
}
}
}
}
}
如何在 elasticsearch 中过滤多个字段和值?
如果您需要我提供回答问题所需的额外信息,请发表评论。如果您添加指向文档的链接,请同时提供一个示例(带有查询 dsl)说明如何我目前或类似的情况应该得到解决。
最佳答案
使用以下代码:
子句(查询)必须出现在匹配的文档中,并将有助于得分。
"query": {
"bool": {
"must" : [
{"term" : { "is_active" : true}},
{"term" : { "gender" : "female"}},
{"term" : { "brand" : "addidas"}},
{"terms": { "categoryId": [1,2,3,4]}}
]
}
}
在过滤器元素下指定的查询对评分没有影响
"query": {
"bool": {
"filter" : [
{"term" : { "is_active" : true}},
{"term" : { "gender" : "female"}},
{"term" : { "brand" : "addidas"}},
{"terms": { "categoryId": [1,2,3,4]}}
]
}
}
关于elasticsearch - 如何在 elasticsearch 中使用多个字段和值进行过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48614104/