如何在 elasticsearch 中使用多个字段和值进行过滤?

How to filter with multiple fields and values in elasticsearch?

我一直在通读文档并尝试实现通过多个字段和列过滤结果的解决方案,但是我不断收到错误; 格式错误的查询

我想用完全相等的值过滤结果,例如:

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 中按照描述过滤多个字段和值?

如果您需要我提供回答问题所需的额外信息,请发表评论。如果您在文档中添加 link,请同时提供应该如何解决我当前或类似情况的示例(带有查询 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]}}
        ]
    }
}