0

I have an index like this:

{
    "rentals": {
    "aliases": {},
    "mappings": {
      "rental": {
        "properties": {

         "address": {
            "type": "text"
         },

         "availability": {
            "type": "nested",
            "properties": {
                "chargeBasis": {
                   "type": "text"
                 },
                "date": {
                   "type": "date"
                },
               "isAvailable": {
                  "type": "boolean"
                },
               "rate": {
                  "type": "double"
                }
           }
       }
  }

And this is my use case:

  1. I need to search for all the "rentals" that have a given address.

    • This is easy and done
  2. I need to get "availability" data for all those "rentals" searched; only for today's date.

    • This is the part where I'm stuck at, how do I query the nested documents of all the "rentals"?
Pratish Shrestha
  • 1,712
  • 4
  • 17
  • 26

1 Answers1

0

You need to use the nested query:

Because nested objects are indexed as separate hidden documents, we can’t query them directly. Instead, we have to use the nested query to access them.

Try something like:

{
  "query": {
    "nested": {
      "path": "availability", 
      "query": {
        "term": {
          "availability.date": "2015-01-01"
        }
      }
    }
  }
}
Antonio Val
  • 3,200
  • 1
  • 14
  • 27
  • But this will affect the first search results. Documents which do not have the availabillity date will not show up – Pratish Shrestha Jan 15 '17 at 06:36
  • What do you mean with the first search results? Yes, if you are looking for a particular availability date(today), documents without availabilty date are not going to show up. If you want as well documents missing that property you could use a boolean query. – Antonio Val Jan 15 '17 at 08:02