简体   繁体   中英

store documents based on sort order in lucene index

I have two field (name, modifiedDate) in my index. i want to store new document based on modifiedDate and keep index sorted on modifiedDate
doc #1 is the oldest document and (modifiedDate) is oldest too
doc #n is most recent document and (modifiedDate) is near to now

1) how can i create this index structure that documents physically stored base on (modifiedDate) and keep the structure even after any change happened in the index (optimize, delete, update)

2) the following structure let me search for documents in specific date range. but i don't want to search the entire index and then filter. i want to use the following structure to skip all other documents if it goes beyond the date range

for (1 to docCount)
if (modifiedDate is in date range filter)
calculate the score based on query

for (1 to docCount)
if (modifiedDate is greater than upper bound of date range)
break
else
calculate the score based on query

if i have 3,000,000 document and my date range only meets 20 top document, in current lucene behavior i need to check all of the documents, but in accepted behavior I am only scoring top 20 document, and you can guess the huge performance gain

The existing answers are fine but Lucene 4.3.0 came out this year with a new "SortingMergePolicy" that allows advanced Lucene users to use the algorithm suggested in the original poster to cancel a search early. See the javadocs

Lucene will index and query efficiently on numeric fields, see NumericRangeQuery . The javadoc I linked to above have notes about the TrieRangeQuery implementation.

You can store modifiedDate as a NumericField which contains the modified date as a long in ms. Then use a QueryWrapperFilter around a NumericRangeFilter to limit your search to the appropriate date range.

This should be very efficient.

  1. You could sort the results with modifiedDate, see this answer: How do I sort Lucene results by field value using a HitCollector?
  2. if you are really adventurous, you could do some scoring customization. http://lucene.apache.org/java/3_3_0/scoring.html

HTH

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