1
votes

I have documents that I want to index/search with ElasticSearch. These documents may contain multiple dates, and in some cases, the dates are actually date ranges. I'm wondering if someone can help me figure out how to write a query that does the right thing (or how to properly index my document so I can query it).

An example is worth a thousand words. Suppose the document contains two marriage date ranges: 2005-05-05 to 2007-07-07 and 2012-12-012 to 2014-03-03.

If I index each date range in start and end date fields, and write a typical range query, then a search for 2008-01-01 will return this record because one marriage will satisfy one of the inequalities and the other will satisfy the other. I don't know how to get ES to keep the two date ranges separate. Obviously, having marriage1 and marriage2 fields would resolve this particular problem, but in my actual data set I have an unbounded number of dates.

I know that ES 5.2 supports the date_range data type, which I believe would resolve this issue, but I'm stuck with 5.1 because I'm using AWS's managed ES.

Thanks in advance.

1
Probably a nested field that has two fields: start_date and end_date. Did you explore this option already?Andrei Stefan
No. I'm really new to ES, and am unfamiliar with nested fields. I was wondering if there was such a construct, but hadn't run across anything in the docs I had read. I'll dig into that. Thanks for pointing me in that direction!Robert Wille

1 Answers

0
votes

You can use nested objects for this purpose.

PUT /records
{
  "mappings": {
    "record": {
      "properties": {
        "marriage": {
          "type": "nested", 
          "properties": {
            "start":    { "type": "date"  },  
            "end": { "type": "date"  },  
            "person1":     { "type": "string"   },  
            "person2":   { "type": "string"   }   
          }   
        }   
      }   
    }   
  }
}

PUT /records/record/1

{
  "marriage": [ { "start" : "2005-05-05","end" :"2007-07-07" , "person1" : "","person2" :"" },{"start": "2012-12-12","end": "2014-03-03","person1" : "","person2" :"" }]
}

POST /records/record/_search
{
  "query": {

          "nested": {
            "path": "marriage", 
            "query": {

                    "range": {
                      "marriage.start":  { "gte": "2008-01-01", "lte": "2015-02-03"}
                    }   

            }   
          }   

 }