为什么must_not就不生效呢?
mapping:
"text_terms": {
"type": "nested",
"properties": {
"term": {
"type": "string",
"index": "not_analyzed"
},
"freq": {
"type": "integer"
}
}
}
数据
{ "text_terms" : [ { "term" : "aaa", "freq" : 1 }, { "term" : "bbb", "freq" : 1 }, { "term" : "ccc", "freq" : 1 } ] }
{ "text_terms" : [ { "term" : "aaa", "freq" : 1 }, { "term" : "西门子", "freq" : 1 }, { "term" : "ccc", "freq" : 1 } ] }
{ "text_terms" : [ { "term" : "ccc", "freq" : 1 }, { "term" : "西门子", "freq" : 1 }, { "term" : "ddd", "freq" : 1 } ] }
{ "text_terms" : [ { "term" : "ddd", "freq" : 1 }, { "term" : "eee", "freq" : 1 } ] }
查询包含西门子
的记录 没有问题 能查出包含西门子的两条记录
"query": { "nested": { "query": { "bool": { "must": [{ "term": { "text_terms.term": "西门子" } }] } }, "path": "text_terms" } }
但是查询不包含西门子的记录时 就不生效了呢?
"query": { "nested": { "query": { "bool": { "must_not": [{ "term": { "text_terms.term": "西门子" } }] } }, "path": "text_terms" } }
怎么此时四条记录都能查出来呢?
添加第五条记录
must_not 查不出此条记录来 于是知道原因
正确的查询方法
即
must_not
应该放在nested
外面补充:
must_not
在nested
内部must_not
在nested
外部