使用多个查询参数的ElasticSearch多重匹配

zbsbpyhn  于 2022-11-28  发布在  ElasticSearch
关注(0)|答案(1)|浏览(196)

我们在ElasticSearch中有以下多匹配查询

{
"query": {
    "bool": {
        "must": {
            "multi_match": {
                "query": "90803",
                "type": "cross_fields",
                "fields": [
                    "POSTAL_CODE^5",
                    "ADDRESS",
                    "CITY"
                ],
                "operator": "and"
            }
        }
    }
}}

我们如何传递多个查询参数。例如,我们希望在查询中传递多个ID,以与字段“邮政编码”匹配。

lmvvr0a8

lmvvr0a81#

首先,POSTAL_CODE是否为已分析字段?如果不是,则可以使用Terms Query

{
    "query": {
        "terms" : { 
            "POSTAL_CODE" : ["90803", "90809"]
        }
    }
}

如果出于某种原因要使用Match,而没有匹配多个值的Match Query,则必须使用Bool Queryshouldmust,具体取决于您的用例。
must示例:

{
    "query": {
        "bool": {
            "must": [{
                "match": { "POSTAL_CODE": "90803" }
            }, {
                "match": { "POSTAL_CODE": "90809" }
            }]
        }
    }
}

相关问题