【问题标题】:Elastic search combining queries and nested queries together将查询和嵌套查询结合在一起的弹性搜索
【发布时间】:2017-08-23 14:29:29
【问题描述】:

我已经阅读 elasticsearch 查询有一段时间了,文档似乎对我的某些查询没有帮助。 我正在尝试获取

的文档
((Field1=Keyword1 OR Field2=Keyword1) AND (Field1=Keyword2 OR Field2=Keyword2)) AND (Field3=Keyword3 ) AND (Field4!=Keyword4)).

我参考的文件是 https://dzone.com/articles/23-useful-elasticsearch-example-queries 并且当尝试在 Chrome 中的 sense API 扩展中执行他们的一些查询时,它有语法错误。 例如:

{
    "query": {
        "bool": {
            "must": {
                "bool" : { "should": [
                      { "match": { "title": "Elasticsearch" }},
                      { "match": { "title": "Solr" }} ] }
            },
            "must": { "match": { "authors": "clinton gormely" }},
            "must_not": { "match": {"authors": "radu gheorge" }}
        }
    }
}

它给出“重复的必须。语法错误”。

您能帮忙形成我的查询吗?

谢谢。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    你必须两次在同一级别。试试这个

       {
            "query": {
                "bool": {
                    "must": [{
                        "bool" : { "should": [
                              { "match": { "title": "Elasticsearch" }},
                              { "match": { "title": "Solr" }} ] }
                    }, { "match": { "authors": "clinton gormely" }}],
    
                    "must_not": { "match": {"authors": "radu gheorge" }}
                }
            }
        }
    

    【讨论】:

    • 谢谢,但我问的问题是关于为此((Field1=Keyword1 OR Field2=Keyword1) AND (Field1=Keyword2 OR Field2=Keyword2)) AND (Field3=Keyword3 ) AND (Field4!=Keyword4)).生成新查询
    • 您是否尝试过答案中的查询?在我看来,它会给你匹配((title=Elasticsearch OR title=Solr) AND (authors=clinton gormley) AND (authors!=radu gheorge)) 的结果——就像你问的那样。如果它不起作用,你应该解释如何。
    • @dshockley 你在问我吗?:)
    • @VolodymyrBilyachat -- 对不起,不!我的意思是你的答案看起来不错,我在问 OP 他是否在评论之前尝试过你的解决方案。 :)
    • @dshockley 这是个玩笑 :) 有时 OP 不想尝试他们想要准备好的代码复制粘贴......
    【解决方案2】:

    正如 Volodymyr 的回答中提到的,您的查询包含 2 个必须在同一级别,因此出现错误。查看this 文档以满足您的要求。

    必须:所有这些子句都必须匹配。相当于 AND。

    must_not:所有这些子句都不能匹配。等价于 NOT。

    应该至少有一个子句必须匹配。相当于 OR。

    filter:必须匹配的子句,但在非评分过滤模式下运行。

    【讨论】:

      猜你喜欢
      • 2017-06-11
      • 2021-09-01
      • 2015-09-24
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      相关资源
      最近更新 更多