首页 > 解决方案 > 当它们是“AND-OR”条件时如何使用弹性搜索获取数据(查询生成器)

问题描述

例如,如果我必须从所有文件“(userId OR currentUserId) AND (customeromerId OR customercode OR customerbacth) AND (customerph or customer addrss) AND customercity”的文本匹配中获取结果。

如果我使用下面的代码,我将得到根据关键字匹配的结果,而不管AND OR条件如何。

 $queryMsg = ['query_string' => ['query' => $query]];

但是我想获取结果,例如,应该获取与 userId 或 currentUserId 和 customeromerId 匹配的记录。

请帮助我如何在弹性搜索中编写查询,以便我可以获取结果。

提前非常感谢。

标签: elasticsearchelasticsearch-6

解决方案


{
"query" : {
    "must" : [
        {
            "bool" : {
                "should" : [
                    {
                        "match" : {'userId' : id}

                    },
                    {
                        "match" : {'currentUserId' : id}
                    }
                ],
                "minimum_should_match" : 1
            }
        },
        {
            "bool": {
                "should": [
                    {
                        "match": {'customeromerId': code}

                    },
                    {
                        "match": {'customercode': code}
                    },
                    {
                        "match": {'customerbacth': code}
                    }
                ],
            "minimum_should_match" : 1
            },
        },
        {
            "bool": {
                "should": [
                    {
                        "match": {'customerph': address}

                    },
                    {
                        "match": {'customer addrss': address}
                    }
                ],
                "minimum_should_match" : 1
            }
        },
        {
            "bool" : {
                "must" : {
                    "customercity" : city
                }
            }
        }
    ]
}

}


推荐阅读