简体   繁体   中英

Lucene.net Query Between Two Date?

I can use Lucene.net for string indexing and searching, but, i want to search documents by, between two date. can you share any code example for search between two date? i find one sample in stackoverflow, but it's too old (9 year ago) and can't run with my Lucene.net version. Thanks,

Lucene.NET does not have a field type of date or date range query. So there are typically two choices for how to store, index and search dates.

  1. You can handle them as string, for example in year month day format ie 210711 or 20210711 for July 11, 2021; or
  2. You can handle them as a long by storing the ticks or milliseconds that represent the date.

Then depending on which you chose to go with then you can use a TermRangeQuery or a NumericRangeQuery for doing the search. Both approaches are able to handle date only or date and time.

It's also worth knowing that Lucene.NET contains a DataTools static class that has utility methods for converting a DateTime to a string and back with different levels of resolution, ie

"yyyy";
"yyyyMM";
"yyyyMMdd";
"yyyyMMddHH";
"yyyyMMddHHmm";
"yyyyMMddHHmmss";
"yyyyMMddHHmmssfff";

Hopefully that's enough to get you going.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM