public void index() {
IndexWriter indexWriter = null;
int[] attachFiles = {1, 2, 3, 4, 5, 6};
try {
indexWriter = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35)));
Document document = null;
for (int i = 0; i < ids.length; i++) {
document = new Document();
// 对数字进行索引使用NumericField,第三个参数是true/false,表示是否索引
document.add(new NumericField("attachFiles",Field.Store.YES,false).setIntValue(attachFiles[i]));
indexWriter.addDocument(document);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (indexWriter != null) {
try {
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
如果是setIntValue还可以根据需要换成setDoubleValue,setFloatValue,setLongValue。
二、对日期进行索引其实对于日期的存储可以转化为对long类型的数值进行存储,所以没有所谓的DateField,我们只需要转化成long类型并存储即可,代码和上面很类似。
document.add(new NumericField("date", Field.Store.YES, true).setLongValue(dates[i].getTime()));