Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

ElasticSearch: Querying if today is between and series of start and end dates in list

If I have some data with a field with multiple sets of start/end dates.. for example:

{
     id: 1,
     title: "My Title",
     availability[{start: 01-01-2020, end: 01-05-2020},{start: 02-01-2020, end: 02-22-2020}]
}

Is it possible in elasticsearch to build a query to check if today (or any given date) falls within any of the start/end date combinations in the list?

Or would I need to structure my data differently to make this work?

Previously, I was dealing with just one start and one end date and could store them as their own fields and do a gte, lte combination to check.

Update:

if I add them as nested fields. e.g.:

"avails" : {
    "type" : "nested",
    "properties" : {
        "availStart" : { "type" : "date" },
        "availEnd" : { "type" : "date" }
    }
}

If I do my search like this:

{
    "query": {
        "nested" : {
            "path" : "avails",
            "query" : {
                "term" : {
                    { "range" : {"avails.start" : {"lte": "now"}}},
                    { "range" : {"avails.end" : {"gt" : "now"}}}
                }
            }
        }
    }
}

will it evaluate this for each nested record and return any parent record with a child record that matches?

question from:https://stackoverflow.com/questions/65888730/elasticsearch-querying-if-today-is-between-and-series-of-start-and-end-dates-in

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's good that you've chosen nested fields. Now you just need to make sure the mappings, field names, and the query are all consistent.

  1. The date mapping including the format:
PUT myindex
{
  "mappings": {
    "properties": {
      "avails": {
        "type": "nested",
        "properties": {
          "start": { "type": "date", "format": "MM-dd-yyyy" },
          "end": { "type": "date", "format": "MM-dd-yyyy" }
        }
      }
    }
  }
}
  1. Syncing your doc
POST myindex/_doc
{
  "id": 1,
  "title": "My Title",
  "avails": [
    {
      "start":"01-01-2020",
      "end": "01-05-2020"
    },
    {
      "start": "02-01-2020",
      "end": "02-22-2020"
    }
  ]
}
  1. And finally the query. Yours was malformed -- if you want a logical AND, you'll need to wrap the range queries in a bool + must:
POST myindex/_search
{
  "query": {
    "nested": {
      "path": "avails",
      "query": {
        "bool": {
          "must": [
            { "range" : {"avails.start" : {"lte": "now"}}},
            { "range" : {"avails.end" : {"gt" : "02-01-2020"}}}
          ]
        }
      }
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...