亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么查詢不包含某個詞的記錄時 must_not不生效呢?

為什么查詢不包含某個詞的記錄時 must_not不生效呢?

慕斯王 2018-07-23 21:04:03
為什么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" } }怎么此時四條記錄都能查出來呢?
查看完整描述

1 回答

?
慕少森

TA貢獻2019條經驗 獲得超9個贊


添加第五條記錄

"text_terms" : [ { "term" : "西門子", "freq" : 1 } ]

must_not 查不出此條記錄來 于是知道原因

只要text_term中存在term不等于西門子的記錄 都能查出來 即使其中也有term等于西門子

正確的查詢方法

"query": { "bool":{ "must_not":{ "nested": {"path": "text_terms", "query": { "term": { "text_terms.term": "西門子" } } } } } }

must_not應該放在nested外面

補充:

  • must_not 在 nested 內部

curl 'http://localhost:9200/test/_validate/query?explain=true&pretty' -H 'Content-Type: application/json' -d'
{"query": { "nested": {  "path": "text_terms", "query": { "bool": { "must_not": [{ "term": { "text_terms.term": "西門子" } }] } } } }
}
'  "explanations" : [
    {      "index" : "test",
      "valid" : true,
      "explanation" : "ToParentBlockJoinQuery (+(-text_terms.term:西門子 +*:*) #_type:__text_terms)"
    }
  ]
  • must_notnested外部

curl 'http://localhost:9200/test/_validate/query?explain=true&pretty' -H 'Content-Type: application/json' -d'
{"query": { "bool":{ "must_not":{ "nested": {"path": "text_terms", "query": { "term": { "text_terms.term": "西門子" } } } } } }
}
'  "explanations" : [
    {      "index" : "test",
      "valid" : true,
      "explanation" : "+(-ToParentBlockJoinQuery (text_terms.term:西門子) +*:*) #DocValuesFieldExistsQuery [field=_primary_term]"
    }
  ]


查看完整回答
反對 回復 2018-07-29
  • 1 回答
  • 0 關注
  • 1559 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號