Query to check if nestedobject contains values or not exist

Hello everyone, I am interested if something like this is possible in elastic search...
So, I want to search for documents, which manufacturer.Id is in 1225 or manufacturer object doesn't exist. Manufacturer is a nested object and query in which I want to add that logic, looks like this.

GET index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "manufacturers",
            "query": {
              "terms": {
                "manufacturers.id": [
                  2452
                ]
              }
            }
          }
        }
      ]
    }
  }
}
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "manufacturers",
            "query": {
              "terms": {
                "manufacturers.id": [
                  2452
                ]
              }
            }
          }
        }
      ]
      ,
      "must_not": [
        {
          "exists": {
            "field": "manufacturers"
          }
        }
      ]
    }
  }
}

Tried this query, but only get data with manufacturerIds of 2452

As you want to run a "or" you need to wrap the 2 conditions within a bool -> should.

how can I have must not in should query

GET onoff-live/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "manufacturers",
            "query": {
              "terms": {
                "manufacturers.id": [
                  2452
                ]
              }
            }
          }
        }
      ],
      "should": [
        {
          "exists": {
            "field": "manufacturers"
          }
        }
      ]
    }
  }
}

You can add another bool query with a must not clause within the should clause of the first bool query.

GET onoff-live/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "manufacturers",
            "query": {
              "terms": {
                "manufacturers.id": [
                  2452
                ]
              }
            }
          }
        }
      ],
      "should": [
        {
         "bool": {
           "must_not": [
             {
               "exists": {
                 "field": "manufacturers"
               }
             }
           ]
         }
        }
      ]
    }
  }
}

There is 3 hit where manufacturer nested object is empty and there is 184 hit where manufacturer id is in 2452.. and as i expected it should give me 187 hit but it gives me 184

I think it should be something like

GET onoff-live/_search
{
  "query": {
    "bool": {
      "should": [

        {
          "nested": {
            "path": "manufacturers",
            "query": {
              "terms": {
                "manufacturers.id": [
                  2452
                ]
              }
            }
          }
        },

        {
         "bool": {
           "must_not": [
             {
               "exists": {
                 "field": "manufacturers"
               }
             }
           ]
         }
        }
      ]
    }
  }
}

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