java - Lucene strange behaviour -
i'm trying start using lucene. code, i'm using index documents is:
public void index(string type, string words) { indexwriter indexwriter = null; try { if (dir == null) dir = createandpropagate(); indexwriter = new indexwriter(dir, new standardanalyzer(), true, new keeponlylastcommitdeletionpolicy(), indexwriter.maxfieldlength.unlimited); field wordsfield = new field(field_words, words, field.store.yes, field.index.analyzed); field typefield = new field(field_type, type, field.store.yes, field.index.analyzed); document doc = new document(); doc.add(wordsfield); doc.add(typefield); indexwriter.adddocument(doc); indexwriter.commit(); } catch (ioexception e) { logger.error("problems while adding entry index.", e); } { try { if (indexwriter != null) indexwriter.close(); } catch (ioexception e) { logger.error("unable close index writer.", e); } } }
the search looks this:
public list<tagsearchentity> searchfor(final string type, string words, int amount) { list<tagsearchentity> result = new arraylist<tagsearchentity>(); try { if (dir == null) dir = createandpropagate(); (final document doc : searchfor(dir, type, words, amount)) { @suppresswarnings("serial") tagsearchentity searchresult = new tagsearchentity() {{ settype(type); setwords(doc.getfield(field_words).stringvalue()); }}; result.add(searchresult); } } catch (ioexception e) { logger.error("problems while searching", e); } return result; } private list<document> searchfor(directory indexdirectory, string type, string words, int amount) throws ioexception { searcher indexsearcher = new indexsearcher(indexdirectory); final query tagquery = new termquery(new term(field_words, words)); final query typequery = new termquery(new term(field_type, type)); @suppresswarnings("serial") booleanquery query = new booleanquery() {{ add(tagquery, booleanclause.occur.should); add(typequery, booleanclause.occur.must); }}; list<document> result = new arraylist<document>(); (scoredoc scoredoc : indexsearcher.search(query, amount).scoredocs) { result.add(indexsearcher.doc(scoredoc.doc)); } indexsearcher.close(); return result; }
i've got 2 use cases. first 1 adds document of type, searches it, adds document of type, searches it, etc. other 1 adds documents, searches them. first 1 works fine:
@test public void testsearch() { search.index("type1", "test type1 test purposes test test"); list<tagsearchentity> result = search.searchfor("type1", "test", 10); assertnotnull("retrieved list should not null.", result); asserttrue("retrieved list should not empty.", !result.isempty()); search.index("type2", "test type2 test purposes test test"); result.clear(); result = search.searchfor("type2", "test", 10); asserttrue("retrieved list should not empty.", !result.isempty()); search.index("type3", "test type3 test purposes test test"); result.clear(); result = search.searchfor("type3", "test", 10); asserttrue("retrieved list should not empty.", !result.isempty()); }
but other 1 seems indexing last document:
@test public void testbuggy() { search.index("type1", "test type1 test purposes test test"); search.index("type2", "test type2 test purposes test test"); search.index("type3", "test type3 test purposes test test"); list<tagsearchentity> result = search.searchfor("type3", "test", 10); assertnotnull("retrieved list should not null.", result); asserttrue("retrieved list should not empty.", !result.isempty()); result.clear(); result = search.searchfor("type2", "test", 10); asserttrue("retrieved list should not empty.", !result.isempty()); result.clear(); result = search.searchfor("type1", "test", 10); asserttrue("retrieved list should not empty.", !result.isempty()); }
it finds type3
, fails find others. if shullfle calls around, still find last indexed document. lucene version, i'm using is:
<dependency> <groupid>org.apache.lucene</groupid> <artifactid>lucene-core</artifactid> <version>2.4.1</version> </dependency> <dependency> <groupid>lucene</groupid> <artifactid>lucene</artifactid> <version>1.4.3</version> </dependency>
what doing wrong? how make index documents?
a new index getting created after every index operation. third argument create
flag , being set true. per documentation of indexwriter, if flag set, either create new index or overwrite existing one. set false append existing index.
Comments
Post a Comment