【问题标题】:Nested documents and boolean query with Elasticsearch使用 Elasticsearch 的嵌套文档和布尔查询
【发布时间】:2016-12-21 16:41:42
【问题描述】:

我正在尝试对嵌套文档使用 must_not 布尔查询,但我不断收到奇怪的结果。

这里有一个例子来说明我的问题。

curl -X DELETE "http://localhost:9200/must_again/"
curl -X POST "http://localhost:9200/must_again/" -d '{
  "mappings": {
    "class": {
      "properties": {
        "title": {
          "type": "string"
        },
        "teachers": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}'

curl -XPUT 'http://localhost:9200/must_again/class/1' -d '{
  "title": "class1",
  "teachers": [
    {
      "name": "alex"
    },
    {
      "name": "steve"
    }
  ]
}'

curl -XPUT 'http://localhost:9200/must_again/class/2' -d '{
  "title": "class2",
  "teachers": [
    {
      "name": "alex"
    }
  ]
}'

curl -XPUT 'http://localhost:9200/must_again/class/3' -d '{
  "title": "class3",
  "teachers": []
}'

此时,我有 3 个班级,只有 steve 在教,还有一个没有老师。

我的目标是在史蒂夫没有教的每一节课上获得最后 2 名。

我使用的查询是

curl -XGET 'http://localhost:9200/must_again/class/_search' -d '{
  "query": {
    "nested": {
      "path": "teachers",
      "query": {
        "bool": {
          "must_not": [
            {
              "match": {
                "teachers.name": "steve"
              }
            }
          ]
        }
      }
    }
  }
}'

返回

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.0,
    "hits": [
      {
        "_index": "must_again",
        "_type": "class",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "title": "class2",
          "teachers": [
            {
              "name": "alex"
            }
          ]
        }
      },
      {
        "_index": "must_again",
        "_type": "class",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "title": "class1",
          "teachers": [
            {
              "name": "alex"
            },
            {
              "name": "steve"
            }
          ]
        }
      }
    ]
  }
}

所以 class2 是预期的,但不是 class1class3 丢失。

如果我对 must 执行相同的查询,我会得到正确的结果(仅 class1)。

不确定我做错了什么?

【问题讨论】:

  • 问题是,您正在尝试获取没有 teacher.name = steve 的文档,对于 class1,它与(其中 teachername= alex )嵌套文档匹配,因此返回 class1。明白了吗?
  • 好的,我已经为 class1 找到了,但是 class3 呢?
  • 对于class3,没有嵌套的doc,所以,查询,(teacher name= steve)一定不能是假的,因为没有文件可以证明它是真的。 (例如,在 class1 和 class2 中,有一个文档可以证明 must_not = true )

标签: elasticsearch booleanquery


【解决方案1】:

一种解决方法。

curl -XPOST "http://localhost:9200/must_again/class/_search" -d'
{
   "query": {
      "bool": {
         "must_not": [
            {
               "nested": {
                  "path": "teachers",
                  "query": {
                     "bool": {
                        "must": [
                           {
                              "match": {
                                 "teachers.name": "steve"
                              }
                           }
                        ]
                     }
                  }
               }
            }
         ]
      }
   }
}'

希望这会有所帮助!

【讨论】:

  • 那行得通。它变得令人困惑,但我想没有更好的方法。谢谢。
猜你喜欢
  • 2018-04-28
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
  • 2018-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多