I am looking for a way to filtering lucene index with multiple conditions. For this purpose I checked two different method of filtering search, none of them work for me:
Using BooleanQuery:
BooleanQuery query = new BooleanQuery();
String lower = "*";
String upper = "*";
for (String fieldName : keywordSourceFields) {
TermRangeQuery rangeQuery = TermRangeQuery.newStringRange(fieldName,
lower, upper, true, true);
query.add(rangeQuery, Occur.MUST);
}
TermRangeQuery rangeQuery = TermRangeQuery.newStringRange(keywordField,
lower, upper, true, true);
query.add(rangeQuery, Occur.MUST_NOT);
try {
TopDocs results = searcher.search(query, null,
maxNumDocs);
Using BooleanFilter:
BooleanFilter filter = new BooleanFilter();
String lower = "*";
String upper = "*";
for (String fieldName : keywordSourceFields) {
TermRangeFilter rangeFilter = TermRangeFilter.newStringRange(fieldName,
lower, upper, true, true);
filter.add(rangeFilter, Occur.MUST_NOT);
}
TermRangeFilter rangeFilter = TermRangeFilter.newStringRange(keywordField,
lower, upper, true, true);
filter.add(rangeFilter, Occur.MUST);
try {
TopDocs results = searcher.search(new MatchAllDocsQuery(), filter,
maxNumDocs);
I was wondering what part of chosen queries are wrong? I am looking for documents that for each keywordSourceFields, the field has some value AND also has not value for keyword field. Please guide me through correcting the corresponding query.
Best regards.