【发布时间】: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 是预期的,但不是 class1 和 class3 丢失。
如果我对 must 执行相同的查询,我会得到正确的结果(仅 class1)。
不确定我做错了什么?
【问题讨论】:
-
问题是,您正在尝试获取没有 teacher.name = steve 的文档,对于 class1,它与(其中 teachername= alex )嵌套文档匹配,因此返回 class1。明白了吗?
-
好的,我已经为 class1 找到了,但是 class3 呢?
-
对于class3,没有嵌套的doc,所以,查询,(teacher name= steve)一定不能是假的,因为没有文件可以证明它是真的。 (例如,在 class1 和 class2 中,有一个文档可以证明 must_not = true )
标签: elasticsearch booleanquery