Reputation: 374
I'm experiencing some problems on lucene. I'm querying a database. As far as i know the indexes are OK (i checked it with lukeall-4.4.0). the Query is constructed as following:
Q = Query.split(" ");
booleanQuery = new BooleanQuery();
//Query[] Queryy = new Query[5 + 5 * Q.length];
Query[] Queryy = new Query[3 + 3*Q.length];
//USING THE ALL TEXT
Queryy[0] = new TermQuery(new Term("DESIGNACAO", Query));
Queryy[1] = new TermQuery(new Term("DESCRICAO", Query));
Queryy[2] = new TermQuery(new Term("TAG", Query));
//USING THE SEPARETED VALUES
for (int i = 3, j = 0; j < Q.length; i++, j++) {
Queryy[i] = new TermQuery(new Term("DESIGNACAO", Q[j]));
Queryy[++i] = new TermQuery(new Term("DESCRICAO", Q[j]));
Queryy[++i] = new TermQuery(new Term("TAG", Q[j]));
}
for (int i = 0; i < Queryy.length; i++) {
booleanQuery.add(Queryy[i], BooleanClause.Occur.MUST);
}
The query is OK. For searching for "not or" the query (a booleanQuery) is going to be as following:
+DESIGNACAO:not or +DESCRICAO:not or +TAG:not or +DESIGNACAO:not +DESCRICAO:not +TAG:not +DESIGNACAO:or +DESCRICAO:or +TAG:or
I'm using SimpleAnalyser, thus the words not and or will not be removed. The problem is that i can't get hits. I can only have hits if i make a search with lukeall-4.4.0 but not with my code. My search method is the following one:
IndexReader reader = IndexReader.open(directory1);
TopScoreDocCollector collector = TopScoreDocCollector.create(50, true);
searcher = new IndexSearcher(reader);
searcher.search(booleanQuery, collector);
hits = collector.topDocs().scoreDocs;
int total = collector.getTotalHits();
displayResults();
Is there anything wrong in collecting the data or something??
Kind regards
Upvotes: 0
Views: 301
Reputation: 374
Piece of cake. The problem was in the construction of the query:
for (int i = 0; i < Queryy.length; i++) {
booleanQuery.add(Queryy[i], BooleanClause.Occur.MUST);
}
The BooleanClause.Occur.MUST
means that this has to exist. Thus, all the terms i was adding to the booleanquery must exist (term1 AND term2 AND term3). The correct is:
booleanQuery.add(Queryy[i], BooleanClause.Occur.SHOULD);
This way I can say that has to exist one of those terms I have added (term1 OR term2 OR term3).
Kind regards
Upvotes: 2