Java – how to index date fields in Lucene

I'm new to Lucene I must index the date field

IndexWriter writer = new IndexWriter(FSDirectory.open(indexDir),new WhitespaceAnalyzer(),true,IndexWriter.MaxFieldLength.UNLIMITED)

My opinion is: why do I need an analyzer when there is no analysis date field, and I use field for index Index. NOT_ ANALYZED.

Solution

You can store date fields in this way

Document doc = new Document();
doc.add(new Field("modified",DateTools.timeToString(f.lastModified(),DateTools.Resolution.MINUTE),Field.Store.YES,Field.Index.NOT_ANALYZED));

Where f is the file object

Now use the above document as the index writer

The checkout sample code comes with Lucene... And the following link http://lucene.apache.org/java/2_2_0/api/org/apache/lucene/document/DateTools.html

UPDATE

Field. Index NOT_ ANALYZED

According to Lucene Javadoc, you do not need to use field Index NOT_ Analyzed analyzes fields, but I think, by design, indexwriter expects the analyzer to index exact copies of data, which is inefficient in storage and search

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>