在弹性搜索中传递多个组合查询

时间:2018-07-18 15:15:00

标签: elasticsearch elasticsearch-5

`"query": {
        "function_score": {
            "query": {
                "bool": {
                    "must": [],
                    "should": [],
                    "filter": [
                        {
                            "terms": {
                                "category": "type-1",
                                "product": "product-A"
                            },
                            "terms": {
                                "category": "type-2",
                                "product": "product-B"
                            }
                        }
                    ]
                }
            },
            "functions": []
        }
    },`

我想像上面那样传递多重组合查询,应该是正确的查询格式

在sql中,我的查询将是

从以下产品中选择*,其中(类别='type1'和产品=产品-A)或(类别='type2'和产品=产品-B)或(类别='type3'和产品=产品-C)< / p>

我想复制以上查询

1 个答案:

答案 0 :(得分:0)

如果要在布尔查询中创建OR语句,则应该是嵌套的bool查询,其中包含多个should子句。

所以尝试:

{
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "must": [],
                    "should": [],
                    "filter": [
                        {
                            "bool": {
                                "should": [
                                    {
                                        "bool": {
                                            "must": [
                                                {
                                                    "term": {
                                                        "category": "type-1"
                                                    }
                                                },
                                                {
                                                    "term": {
                                                        "product": "product-A"
                                                    }
                                                }
                                            ]
                                        }
                                    },
                                    {
                                        "bool": {
                                            "must": [
                                                {
                                                    "term": {
                                                        "category": "type-2"
                                                    }
                                                },
                                                {
                                                    "term": {
                                                        "product": "product-B"
                                                    }
                                                }
                                            ]
                                        }
                                    }
                                ]
                            }
                        }
                    ]
                }
            }
        }
    },
    "functions": []
}

,如果没有must子句,则可以将filter子句移到主should中,因为只有匹配至少一个子句的文档才匹配。

相关问题