【问题标题】:Elasticsearch must_not inside nested queryElasticsearch must_not 在嵌套查询中
【发布时间】:2018-10-09 18:00:17
【问题描述】:

我的弹性搜索索引具有嵌套字段。我想使用 must,其中包含一个 must_not 查询和一个嵌套查询。我已经通过以下方式单独尝试了 must_not 查询:

{
    "bool": {
        "must_not": [{
            "nested": {
                "path": "fields",
                "query": {
                    "terms": {
                        "fields.value.raw": [
                            "200"
                        ]
                    }
                }
            }
        }]
    }
}

上面的查询给了我一个有效的结果,但是当我用必须查询尝试这个时,它不会给我任何结果。我正在使用以下查询:

{
        "bool": {
            "must": [{
                "nested": {
                    "path": "fields",
                    "query": {
                        "bool": {
                            "must": [{
                                "match": {
                                    "fields.uid": "number"
                                }
                            }, {
                                "bool": {
                                    "must_not": [{
                                        "nested": {
                                            "path": "fields",
                                            "query": {
                                                "terms": {
                                                    "fields.value.raw": [
                                                        "200"
                                                    ]
                                                }
                                            }
                                        }
                                    }]
                                }
                            }]
                        }
                    }
                }
            }]
        }
    }

上面的查询没有给我一个有效的结果。上面的查询有什么问题? 如何在嵌套查询中使用 must_not?

【问题讨论】:

  • 如果您写一两句话来描述您希望查询执行的操作会有所帮助。例如,我想要所有价格等于 10 的文档。
  • 我的嵌套模式如下: { "fields": { "type": "nested", "properties": { "uid": { "type": "keyword" }, "value" : { "type": "text", "fields": { "raw": { "type": "keyword" } } } } } } 所以我想要具有字段的文档。uid 是数字字段的数字和值(fields.value) 不包含“200”所以需要在must中添加must_not,如何才能得到有效结果?

标签: elasticsearch


【解决方案1】:

您应该在同一个 bool 查询中使用 must 和 must_not。

{
    "bool": {
        "must_not": [{
            "nested": {
                "path": "fields",
                "query": {
                    "terms": {
                        "fields.value.raw": [
                            "200"
                        ]
                    }
                }
            }
        }],
        "must": [{
            "match": {
                "fields.uid": "number"
            }
        }]
    }
}

【讨论】:

  • 上面的查询没有给我一个有效的响应我想检查和查询嵌套对象的以下条件:具有fields.uid的文档是数字并且数字不包含“200”所以需要添加must_not in must,我怎样才能得到有效的结果?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-08
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多