简体   繁体   English

如何在Lucene文档中存储数值?

[英]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. 我正在用Java编写代码,并使用lucene 3.4索引文本文档。 Each document has an id and some other numerical values as well as content and title. 每个文档都有一个id和一些其他数值以及内容和标题。

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. 但是,当我搜索并想要获取结果时,这些字段将返回null。 I know that whenever I add a field and set the Field.Store.NO, it returns null , but why it happens right now? 我知道每当我添加一个字段并设置Field.Store.NO时,它都将返回null ,但是为什么它现在会发生呢? 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. 使用NumericField(String,Field.Store,boolean)构造函数可以指定要存储的内容,以备日后检索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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