-1

I have solr docs with start date and end date, what I want is to find all available docs that current date is between start date and end date? I wonder how to specify that query?

Some example is like this:

{
    uniqueId: "1jhk23-123424h-12312l3j1lj23lj",
    start: "20190101",
    end: "20191010"
}

Today is 20190125, then this doc should be found

Thanks,

Kuan
  • 11,149
  • 23
  • 93
  • 201
  • Can you confirm the schema definition of 'start' and 'end'? – kellyfj Jan 25 '19 at 20:03
  • It is just like in my question – Kuan Jan 25 '19 at 20:04
  • Fields like that could be "string", "text_general" but also Solr supports three different date types - the answer will depend on how you've specified the type of these index fields https://lucene.apache.org/solr/guide/6_6/working-with-dates.html – kellyfj Jan 25 '19 at 20:08
  • Thanks, could you give a sample query for this question? – Kuan Jan 25 '19 at 20:09
  • I can't because you have not told me the schema definition of the fields you are using :-) – kellyfj Jan 25 '19 at 20:10
  • Let us say it is string(cos I do not have access to schema too) – Kuan Jan 25 '19 at 20:11
  • If you later confirm those are date range fields I'd suggest looking at https://stackoverflow.com/questions/796753/solr-fetching-date-ranges – kellyfj Jan 25 '19 at 20:23
  • Thanks, it is not that format – Kuan Jan 25 '19 at 20:24
  • 2
    If you are indeed using string maybe you can do q=+start:[* to "20190125"] +end:["20190125" to *] – kellyfj Jan 25 '19 at 20:27
  • Thanks, I will test this. – Kuan Jan 25 '19 at 20:28
  • 1
    Isn't this the same underlying problem [you asked about before](https://stackoverflow.com/questions/54101425/how-to-search-documents-in-within-date-range-but-based-on-text-format-info-in-so)? – MatsLindh Jan 26 '19 at 09:58
  • @MatsLindh Not exactly same, one is asking how to search that field, this one is how to search docs with current date in range. – Kuan Jan 28 '19 at 17:17
  • @Kuan Sure, but the underlying issue is the same. – MatsLindh Jan 28 '19 at 18:35
  • @MatsLindh Not really, this one is more about logic(which after I have already understood that how solr treat numeric format field). That one is more about usage( which I try to figure out how Solr work with numeric field ). Do not let the question context mislead you – Kuan Jan 28 '19 at 19:35

1 Answers1

1

As long as your dates are in YYYYMMDD format (and not YYYYDDMM), the lexical sort order works fine. Using a regular range search will work in that case:

start:[* TO 20190125] AND end:[20190125 TO *]

Under regular circumstances this will fetch all tokens that has their sort order between those values.

MatsLindh
  • 49,529
  • 4
  • 53
  • 84