【问题标题】:passing multiple combination query in elastic search在弹性搜索中传递多个组合查询
【发布时间】:2018-07-19 22:34:33
【问题描述】:
`"query": {
        "function_score": {
            "query": {
                "bool": {
                    "must": [],
                    "should": [],
                    "filter": [
                        {
                            "terms": {
                                "category": "type-1",
                                "product": "product-A"
                            },
                            "terms": {
                                "category": "type-2",
                                "product": "product-B"
                            }
                        }
                    ]
                }
            },
            "functions": []
        }
    },`

我想像上面这样传递多个组合查询是否可能,正确的查询格式应该是什么

在 sql 中我的查询是

select * from product where (category='type1' and product=product-A) or (category='type2' and product=product-B) or (category='type3' and product=product-C)

我想复制上面的查询

【问题讨论】:

  • 您对这个结果有何期待?您是否正在过滤过滤器的结果? ..不清楚..
  • 我想要一个等效于 (category=type-1 and product=product-A) 或 (category=type-2 and product=product-B) 的查询,因为类型中可能有 product-A -2 类别或类型 1 类别中的产品 B 我想要特定类别的产品
  • @Pogrindis 我不确定我的查询我仍在尝试是否必须、应该或过滤任何对我有用的东西
  • @NikhilBhivgade,你能更新映射和查询吗?

标签: elasticsearch elasticsearch-5


【解决方案1】:

如果您想在 bool 查询中创建 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 子句,您可以将您的过滤器子句移动到主should,因为只有匹配至少一个子句的文档才会匹配。

【讨论】:

  • 出现错误:"reason":"[term] 查询不支持多个字段
  • 很抱歉,我重写了查询以使用嵌套布尔值来处理 AND 逻辑
猜你喜欢
  • 1970-01-01
  • 2019-11-25
  • 1970-01-01
  • 2015-09-11
  • 2021-06-20
  • 2020-04-14
  • 1970-01-01
  • 1970-01-01
  • 2020-06-12
相关资源
最近更新 更多