Hi, I got unexpected behavior while testing lucene. To shortly address the
problem: Using IndexWriter I add docs with fields named ID with a consecutive
order (1,2,3,4, etc) then close that index. I get new IndexReader, and call
IndexReader.deleteDocuments(Term). The term is simply new Term("ID", "1"). and
then class close on IndexReader. Things work out fine. But if i add docs using
IndexWriter, close writer, then create new IndexReader to delete one of the
docs already inserted, but without closing index. while the indexReader that
perform deletion is still not closed, I add more docs, and then commit the
IndexWriter, so when i search I get all added docs in the two phases (before
using deleteDocuments() on IndexReader and after because i haven't closed
IndexReader, howerer, closed IndexWriter). I close IndexReader and then query
the index, so i deletes all docs after opening it till closing it, in addition
to the specified doc in the Term object (in this test case: ID=1). I know that
i can avoid this by close IndexReader directly after deleting docs, but what
about runing it on mutiThread app like web application? There you are the
code:
IndexSearcher indexSearcher = new IndexSearcher(this.indexDirectory);
Hits hitsB4InsertAndClose = null;
hitsB4InsertAndClose = getAllAsHits(indexSearcher);
int beforeInsertAndClose = hitsB4InsertAndClose.length();
indexWriter.addDocument(getNewElement());
indexWriter.addDocument(getNewElement());
indexWriter.addDocument(getNewElement());
indexWriter.close();
IndexSearcher indexSearcherDel = new IndexSearcher(this.indexDirectory);
indexSearcherDel.getIndexReader().deleteDocuments(new Term("ID","1"));
indexWriter.addDocument(getNewElement());
indexWriter.addDocument(getNewElement());
indexWriter.addDocument(getNewElement());
indexWriter.addDocument(getNewElement());
indexWriter.addDocument(getNewElement());
indexWriter.addDocument(getNewElement());
indexWriter.addDocument(getNewElement());
indexWriter.addDocument(getNewElement());
indexWriter.addDocument(getNewElement());
indexWriter.addDocument(getNewElement());
indexWriter.addDocument(getNewElement());
indexWriter.close();
Hits hitsAfterInsertAndClose = getAllAsHits(indexSearcher);
int AfterInsertAndClose = hitsAfterInsertAndClose.length();//This is 14
indexWriter.addDocument(getNewElement());
indexWriter.close();
Hits hitsAfterInsertAndAfterCloseb4Delete = getAllAsHits(indexSearcher);
int hitsAfterInsertButAndAfterCountb4Delete =
hitsAfterInsertAndAfterCloseb4Delete.length();//This is 15
indexSearcherDel.close();
Hits hitsAfterInsertAndAfterClose = getAllAsHits(indexSearcher);int
hitsAfterInsertButAndAfterCount = hitsAfterInsertAndAfterClose.length();//This
is 2 The two methods I Use
private Hits getAllAsHits(IndexSearcher indexSearcher){
try{
Analyzer analyzer = new StandardAnalyzer();
String defaultSearchField = "all";
QueryParser parser = new QueryParser(defaultSearchField, analyzer);
indexSearcher = new IndexSearcher(this.indexDirectory);
Hits hits = indexSearcher.search(parser.parse("+alias:mydoc"));
indexSearcher.close();
return hits;
}catch(IOException ex){
throw new RuntimeException(ex);
}catch(org.apache.lucene.queryParser.ParseException ex){
throw new RuntimeException(ex);
}
}
private Document getNewElement(){
Map<String, String> map = new HashMap();
map.put("ID", new Integer(insertCounter).toString());
map.put("name", "name"+insertCounter);
insertCounter++;
Document document = new Document();
for (Iterator iter = map.keySet().iterator(); iter.hasNext();) {
String key = (String) iter.next();
document.add(new Field(key, map.get(key), Store.YES, Index.TOKENIZED));
}
document.add(new Field("alias", "mydoc", Store.YES, Index.UN_TOKENIZED));
return document;} any clue why it works that way? I expect it to delete only
one doc?
_________________________________________________________________
PC Magazine’s 2007 editors’ choice for best web mail—award-winning Windows Live
Hotmail.
http://imagine-windowslive.com/hotmail/?locale=en-us&ocid=TXT_TAGHM_migration_HMWL_mini_pcmag_0707