0
votes

I need to do a query in solr that returns the document closest to a given timestamp. Let's say these are my documents:

[{
  myDateField_dt: "2012-12-01T18:37:35.282Z"
},
{
  myDateField_dt: "2012-12-01T18:37:38.282Z"
},
{
  myDateField_dt: "2012-12-01T18:37:40.282Z"
}]

and this is my search date: 2012-12-01T18:37:36.282Z

then the query should return the first date from my example docs because it's closest to my search date. How would I do this with solr / lucene?

1

1 Answers

1
votes

you use function queries to do some date math, and sort the query by the formula you build. One possible (untested) candidate:

&sort=ms(2012-12-01T18:37:36.282Z, myDateField_dt) asc

Specially useful would be ms(), if() etc functions. Check the date math here

Beware to handle missing values in the date field too.