4

我正在尝试对嵌套文档使用 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 在教,还有一个没有老师。

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

我正在使用的查询是

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是预期的,但不是class1,而且class3是缺失的。

如果我做同样的查询,must我会得到正确的结果(只有 class1)。

不知道我做错了什么?

4

1 回答 1

3

一个办法。

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

希望这可以帮助!!

于 2014-07-25T17:55:26.773 回答