简体   繁体   English

Elasticsearch AND条件与词条查询

[英]Elasticsearch AND condition with term query

This is my search filter query , I want to give "and" condition for terms. 这是我的搜索过滤器查询,我想为条件提供“和”条件。 Now I have given must inside the filter condition 现在我已经给了必须在过滤条件内

'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'
            ],
        ]
    ]
],

but its not working. 但它不起作用。 Any other solution for this? 还有其他解决方案吗?

尝试一下,当您想添加“和”条件时,只需在bool中添加该部分即可。

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

You need to enclose your terms filters in one more array, otherwise your bool/must becomes an associative array and that's not what you want (ie one of the terms filter gets discarded). 您需要将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'
            ],
        ]
    ]
],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM