1.Adding documents to an index:
protected String[] keywords = {"1", "2"};
protected String[] unindexed = {"Netherlands", "Italy"};
protected String[] unstored = {"Amsterdam has lots of bridges", "Venice has lots of canals"};
protected String[] text = {"Amsterdam", "Venice"};
Directory dir = FSDirectory.getDirectory(indexDir, true);
IndexWriter writer = new IndexWriter(dir, new SimpleAnalyzer(), true);
writer.setUseCompoundFile(true);
for (int i = 0; i < keywords.length; i++) {
Document doc = new Document();
doc.add(Field.Keyword("id", keywords[i]));
doc.add(Field.UnIndexed("country", unindexed[i]));
doc.add(Field.UnStored("contents", unstored[i]));
doc.add(Field.Text("city", text[i]));
writer.addDocument(doc);
}
writer.optimize();
writer.close();
2.Removing Documents from an index:
IndexReader reader = IndexReader.open(dir);
reader.delete(1);
上面的方式一次只能删除一个document,下面的方法可以删除多个满足条件的document
IndexReader reader = IndexReader.open(dir);
reader.delete(new Term("city", "Amsterdam"));
reader.close();
3.Index dates
Document doc = new Document();
doc.add(Field.Keyword("indexDate", new Date()));
4.Tuning indexing performance
IndexWriter System property Default value Description
--------------------------------------------------------------------------------------------------
mergeFactor org.apache.lucene.mergeFactor 10 Controls segment merge frequency and size
maxMergeDocs org.apache.lucene.maxMergeDocs Integar.MAX_VALUE Limits the number of documents per segement
minMergeDocs org.apache.lucene.minMergeDocs 10 Controls the amount of RAM used when indexing
mergeFactor控制写入硬盘前内存中缓存的document数量,同时控制merge index segments的频率。其默认值是10,即存满10个
documents后就必须写入硬盘,而且如果segment的数量达到10的级数的时候会merge成一个segment,当然maxMergeDocs限制了每个
segment最大能够保存的document数量。mergeFactor越大的话就越能利用RAM,提高index的效率,但是mergeFactor越高也就意味着
merge的频率就越低,会可能导致segments的数量很大(因为没有merge),这样search的时候就需要打开更多的segment文件,也就
降低了search的效率。minMergeDocs is another IndexWriter instance variable that affects indexing performance. Its
value controls how many Documents have to be buffered before they’re merged to a segment.也即是说minMergeDocs也具有
mergeFactor控制缓存document数量的功能。
5.RAMDirectory帮助利用RAM,也可以采用集群或者多线程的方式充分利用硬件和软件资源,提高index的效率。
6.有时候对于每个field可能希望控制其大小,比如只对前1000个term做index,这个时候就需要使用maxFieldLength来控制。
7.IndexWriter’s optimize()方法就是将segments进行merge,降低segments的数量从而减少search的时候读取index的时间。
8.注意多线程环境下的工作:an index-modifying IndexReader operation can’t be executed
while an index-modifying IndexWriter operation is in progress.为了防止误用,Lucene在使用某些API时会给
index上锁。