Elasticsearch AND 条件与术语查询

Elasticsearch AND condition with term query

这是我的搜索过滤器查询, 我想为条件提供 "and" 条件。 现在我在过滤条件中给出了 must

'query' => [
    "filtered" => [
        "query" => [
            "match_all" => []
        ],
        "filter" => [
            "bool" => [
                "must" => [
                    "terms" => [
                        "num" => [
                            1,2,3
                        ]
                    ],
                    "terms" => [
                        "sample" => [
                            "a", "b"
                        ],
                    ]
                ]
            ]
        ],
        'query' => [
            'multi_match' => [
                'query' => "Hello",
                'fields' => ['text'],
                'operator' => 'and'
            ],
        ]
    ]
],

但它不起作用。还有其他解决方案吗?

试试这个,当你想添加 'and' 条件时,只需将那部分添加到 bool,must 中。

{"query":{"bool":{"must":[{"terms":{"num":[1,2,3]}},{"terms":{"sample":["a","b"]}},{"match":{"text":"hello"}}]}},"from":0,"size":10}

您需要将 terms 过滤器包含在另一个数组中,否则您的 bool/must 会变成一个关联数组,而这不是您想要的(即 terms 过滤器之一得到丢弃)。

'query' => [
    "filtered" => [
        "query" => [
            "match_all" => []
        ],
        "filter" => [
            "bool" => [
                "must" => [
                   [                     <---
                      "terms" => [
                         "num" => [
                            1,2,3
                         ]
                      ]
                   ],                    <---
                   [                     <---
                      "terms" => [
                        "sample" => [
                            "a", "b"
                        ],
                      ]
                   ]                     <---
                ]
            ]
        ],
        'query' => [
            'multi_match' => [
                'query' => "Hello",
                'fields' => ['text'],
                'operator' => 'and'
            ],
        ]
    ]
],