简体   繁体   中英

How to STORE numeric values in lucene documents?

I am writing a code in java and use lucene 3.4 to index the text documents. Each document has an id and some other numerical values as well as content and title.

I add each document to the index according to the following code:

Document doc = new Document();
doc.add(new NumericField("id").setIntValue(writer.numDocs()));
doc.add(new NumericField("year").setIntValue(1988));
doc.add(new Field("content", new FileReader(file)));
writer.addDocument(doc);
writer.close();

But when I search and want to get the results, it returns null for these fields. I know that whenever I add a field and set the Field.Store.NO, it returns null , but why it happens right now? What should I do to get the value of these fields?

doc.get("id");     //why it returns null? what should I do?

Numeric fields are by default not stored.

Use the NumericField(String, Field.Store, boolean) constructor to specify that it should be stored if you would like to retrieve it later.

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