Why doesn't must_not take effect when querying records that don't contain a word?

Why doesn"t must_not take effect?

mapping:

"text_terms": {
    "type": "nested",
    "properties": {
        "term": {
            "type": "string",
            "index": "not_analyzed"
        },
        "freq": {
            "type": "integer"
        }
    }
}

data

{ "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 records containing Siemens have no problem finding two records containing Siemens

"query": { "nested": { "query": { "bool": { "must": [{ "term": { "text_terms.term": "" } }] } }, "path": "text_terms" } }

but does the query not take effect when it does not contain Siemens records?

"query": { "nested": { "query": { "bool": { "must_not": [{ "term": { "text_terms.term": "" } }] } }, "path": "text_terms" } }

how can all four records be found at this time?

Mar.30,2021

add the fifth record

"text_terms" : [ { "term" : "", "freq" : 1 } ]

must_not can't find this record, so it knows why

as long as there are records in text_term that term is not equal to Siemens, even if there are term equals Siemens

correct query method

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

that is, must_not should be placed outside nested

add:

  • must_not within 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: +*:*) -sharp_type:__text_terms)"
    }
  ]
  • must_not on nested outside
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:) +*:*) -sharpDocValuesFieldExistsQuery [field=_primary_term]"
    }
  ]
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3eb2c-2c43a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3eb2c-2c43a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?