Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to index date field in lucene

Tags:

java

date

lucene

I am new to lucene. I have to index date field. i am using Following IndexWriter constructor in lucene 3.0.0.

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

my point is: Why it needs a analyzer when date fields are not analyzed,while indexing I used Field.Index.NOT_ANALYZED.

like image 785
Romi Avatar asked Dec 02 '10 09:12

Romi


1 Answers

You can store date field in this fashion..

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 a file object...

Now use the above document for indexwriter...

checkout the 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

Index the field's value without using an Analyzer, so it can be searched. As no analyzer is used the value will be stored as a single term. This is useful for unique Ids like product numbers.

As per lucene javadoc you don't need analyzer for fields using Field.Index NOT_ANALYZED but i think by design the IndexWriter expects an analyzer as indexing the exact replica of data is not efficient in terms of storage and searching.

like image 196
Favonius Avatar answered Oct 05 '22 12:10

Favonius