1
votes

I am trying to write a query that returns all docs which don't have a particular field which is a nested property but it doesn't work. I am using ES 5.4

{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "shares"
          }
        }
      ]
    }
  }
}

What am I doing wrong?

This is my mapping

{
  "test": {
    "aliases": {},
    "mappings": {
      "vendor": {
        "properties": {
          "address": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "categories": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "city": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "displayTypes": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "emailId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "facebookAppId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "foursquareType": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "googleTypes": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "hours": {
            "properties": {
              "day": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "from": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "to": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "isOnBoarded": {
            "type": "boolean"
          },
          "kiaskCategories": {
            "type": "keyword"
          },
          "latitude": {
            "type": "float"
          },
          "location": {
            "type": "geo_point"
          },
          "longitude": {
            "type": "float"
          },
          "name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "phoneNumber": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "pictures": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "placeId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "price": {
            "type": "float"
          },
          "rating": {
            "type": "float"
          },
          "shares": {
            "type": "nested",
            "properties": {
              "_id": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "content": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "createdDate": {
                "type": "date"
              },
              "endDate": {
                "type": "date"
              },
              "facebookLikes": {
                "type": "long"
              },
              "images": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "price": {
                "type": "long"
              },
              "quantity": {
                "type": "long"
              },
              "shareType": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "startDate": {
                "type": "date"
              },
              "tags": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "source": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "state": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "timezone": {
            "type": "float"
          },
          "url": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "website": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "yelpTypes": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1496319860700",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "0S0A9YN-S-SrW4eeOHX06w",
        "version": {
          "created": "5040099"
        },
        "provided_name": "test"
      }
    }
  }
}
1

1 Answers

0
votes

this should work You have enforce nested data type mappings and use nested query.

{
  "query" : {
    "nested": {
      "path": "shares",
      "query": {
        "bool": {
          "must_not": [
            {
              "exists" : {
                "field" : "shares._id"
              }
            }
          ]
        }
      }
    }
  }
}

Note: The following setup works for me.

PUT test_index
{
  "mappings": {
    "document_type" : {
      "properties": {
        "name" : {
          "type": "text"
        },
        "shares" : {
          "type": "nested"
        }
      }
    }
  }
}

POST test_index/document_type
{
  "name" : "vicky"

}

POST test_index/_search
{
  "query": {
    "nested": {
      "path": "shares",
      "query": {
        "bool": {
          "must_not": [
            {
              "exists" : {
                "field" : "shares.city"
              }
            }
          ]
        }
      }
    }

  }
}