下面是我的代码。
Document doc = new Document();
String str = "Lucene in Action"; //first document
doc.add(new Field("title", newString,Field.Store.YES,Field.Index.ANALYZED));
writer.addDocument(doc);
System.out.println(doc.getFields());
如果我需要索引1000个文档,那么我需要为那1000个文档运行上面的代码,如果是,那么我们如何在循环中运行此代码,我尝试制作类型为Document的数组,但不允许这样做我这样做。我如何摆脱这个问题?
答案 0 :(得分:0)
您可以创建文档并向其添加字段一次,然后只需更改该字段的值,然后再将文档写入索引
Document doc = new Document();
StringField stringField = new StringField(<your_name>, "", Field.Store.YES);
doc.add(stringField);
....
for (String value : <ListOfStrings>) {
stringField.setStringValue(value);
writer.addDocument(doc);
}
答案 1 :(得分:0)
这可能不是立即可用的示例,但我想这个想法本身可能会有所帮助。
您可以将文档创建提取到方法中:
sudo chmod u+s `which ping`
现在,如果您需要多次,可以尝试执行以下操作:
// methods params should be everything you need every time you want to create a new document
// input param str is instead of this String str = "Lucene in Action";
// it's not used but I left it in case you need it
public Document createDocument(String str,
String newString,
Field.Store storeVal,
Field.Index indexVal) {
final Document doc = new Document();
// if you need to add many fields - you can do it here also
// let's say having this in the loop as well
doc.add(new Field("title", newString, storeVal, indexVal));
return document;
}
希望它有用。
Happy Hacking :)