How to search nested docs with field condition all satisfied with range condition


(jdgiauzn) #1

http://sense.qbox.io/gist/7c4abcc3fb48882c36c9014c625c8bda20e517c7

DELETE /test_index

PUT /test_index
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
      "listing": {
        "dynamic": "false",
        "properties": {
          "id": {
            "type": "integer"
          },
          "types": {
            "type": "nested",
            "properties": {
              "id": {
                "type": "integer"
              },
              "date": {
                "type": "date"
              },
              "status": {
                "type": "integer"
              }
            }
          }
        }
      }
    }
}

POST /test_index/listing/_bulk
{"index":{"_id":1}}
{"id":1,"types":[{"id":1, "date":"2016-09-07", "status": 1}, {"id":2, "date":"2016-09-08", "status": 1}, {"id":3, "date":"2016-09-09", "status": 1}]}
{"index":{"_id":2}}
{"id":2,"types":[{"id":4, "date":"2016-09-07", "status": 1}, {"id":5, "date":"2016-09-08", "status": 0}, {"id":6, "date":"2016-09-09", "status": 1}]}
{"index":{"_id":3}}
{"id":3,"types":[{"id":7, "date":"2016-09-07", "status": 0}, {"id":8, "date":"2016-09-08", "status": 0}, {"id":9, "date":"2016-09-09", "status": 0}]}

POST /test_index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "types",
            "filter": {
              "and": [
                {
                  "terms": {
                    "types.status": [
                      1
                    ]
                  }
                },
                {
                  "range": {
                    "types.date": {
                      "gte": "2016-09-07",
                      "lt": "2016-09-09"
                    }
                  }
                }
              ]
            }
          }
        }
      ]
    }
  },
  "_source": false
}

I want listing with id "1" only.

Thanks


(jdgiauzn) #2

Any body knows?


(Adrien Grand) #3

Your current query looks good, what is the issue?


(jdgiauzn) #4

current query result will return listings of id 1 and 2.

I just want the listing id of 1, because all status is 1, but listing id of 2 is not

thanks


(system) #5