use of com.zimbra.cs.index.ZimbraIndexReader.TermFieldEnumeration in project zm-mailbox by Zimbra.
the class LuceneQueryOperation method expandLazyMultiPhraseQuery.
private Query expandLazyMultiPhraseQuery(Query query) throws IOException {
if (query instanceof LazyMultiPhraseQuery) {
LazyMultiPhraseQuery lazy = (LazyMultiPhraseQuery) query;
int max = LC.zimbra_index_wildcard_max_terms_expanded.intValue();
MultiPhraseQuery mquery = new MultiPhraseQuery();
for (Term[] terms : lazy.getTermArrays()) {
if (terms.length != 1) {
mquery.add(terms);
continue;
}
Term base = terms[0];
if (!lazy.expand.contains(base)) {
mquery.add(terms);
continue;
}
List<Term> expanded = Lists.newArrayList();
TermFieldEnumeration itr = searcher.getIndexReader().getTermsForField(base.field(), base.text());
try {
while (itr.hasMoreElements()) {
BrowseTerm term = itr.nextElement();
if (term != null && term.getText().startsWith(base.text())) {
if (expanded.size() >= max) {
// too many terms expanded
break;
}
expanded.add(new Term(base.field(), term.getText()));
} else {
break;
}
}
} finally {
Closeables.closeQuietly(itr);
}
if (expanded.isEmpty()) {
return null;
} else {
mquery.add(expanded.toArray(new Term[expanded.size()]));
}
}
return mquery;
} else if (query instanceof BooleanQuery) {
ListIterator<BooleanClause> itr = ((BooleanQuery) query).clauses().listIterator();
while (itr.hasNext()) {
BooleanClause clause = itr.next();
Query result = expandLazyMultiPhraseQuery(clause.getQuery());
if (result == null) {
if (clause.isRequired()) {
return null;
} else {
itr.remove();
}
} else if (result != clause.getQuery()) {
clause.setQuery(result);
}
}
return ((BooleanQuery) query).clauses().isEmpty() ? null : query;
} else {
return query;
}
}
Aggregations