filter range date elasticsearch filter range date elasticsearch elasticsearch elasticsearch

filter range date elasticsearch


First create your mapping with the openingTimes object at the top level.

/PUT http://localhost:9200/demo/test/_mapping{  "test": {    "properties": {      "openingTimes": {        "type": "object",        "properties": {          "monday": {            "type": "nested",            "properties": {              "start": {                "type": "date",                "format": "hour_minute"              },              "end": {                "type": "date",                "format": "hour_minute"              }            }          }        }      }    }  }}

Index your document

/POST http://localhost:9200/demo/test/1{  "name": "thename",  "openingTimes": {    "monday": [      {        "start": "10:00",        "end": "14:00"      },      {        "start": "19:00",        "end": "02:30"      }    ]  }}

With a nested filter query you can search for the document with the start and end fields within boolean range queries:

/POST http://localhost:9200/demo/test/_search{  "query": {    "filtered": {      "query": {        "match_all": {}      },      "filter": {        "nested": {          "path": "openingTimes.monday",          "filter": {            "bool": {              "must": [                {                  "range": {                    "openingTimes.monday.start": {                      "lte": "13:00"                    }                  }                },                {                  "range": {                    "openingTimes.monday.end": {                      "gte": "14:00"                    }                  }                }              ]            }          }        }      }    }  }}