Apply "doesn't equal" with nested query

Hi there!

Please help to suggest queries to get data. How to get documents that don't have FilterFields. ContentFieldUseID = 20 and FilterFields.ContentFieldValues = "aaaa@gmail.com".

Expected result: I want to get two documents that have IDProperty : "15-118", "15-119".

Thanks for your help

{
          "IDProperty" : "15-117",
          "ContentTitle" : "Anh Le",
          "ContentSummary" : "",
          "FilterFields" : [
            {
              "ContentFieldUseID" : 20,
              "ContentFieldValues" : [
                "aaaa@gmail.com"
              ]
            },
            {
              "ContentFieldUseID" : 21,
              "ContentFieldValues" : [
                "anh le"
              ]
            }
},
{
          "IDProperty" : "15-118",
          "ContentTitle" : "Anh Le 2",
          "ContentSummary" : "",
          "FilterFields" : [
            {
              "ContentFieldUseID" : 20,
              "ContentFieldValues" : [
                "yyyy@gmail.com"
              ]
            },
            {
              "ContentFieldUseID" : 21,
              "ContentFieldValues" : [
                "anh le 2"
              ]
            }
},
{
          "IDProperty" : "15-119",
          "ContentTitle" : "Anh Le 3",
          "ContentSummary" : "",
          "FilterFields" : [
            {
              "ContentFieldUseID" : 21,
              "ContentFieldValues" : [
                "anh le 3"
              ]
           }
}

Hi @Anh_Le

Try this query:

{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "FilterFields",
            "query": {
              "bool": {
                "must": [
                  {
                    "term": {
                      "FilterFields.ContentFieldUseID": {
                        "value": 20
                      }
                    }
                  },
                  {
                    "term": {
                      "FilterFields.ContentFieldValues": {
                        "value": "aaaa@gmail.com"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ]
    }
  }
}

I used this mapping:

PUT test
{
  "mappings": {
    "properties": {
      "FilterFields": {
        "type": "nested",
        "properties": {
          "ContentFieldValues": {
            "type": "keyword"
          },
          "ContentFieldUseID": {
            "type": "integer"
          }
        }
      }
    }
  }
}
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.