use of org.apache.lucene.search.TermQuery in project zm-mailbox by Zimbra.
the class GetMailQueue method buildLuceneQuery.
public static Query buildLuceneQuery(Element queryElem) throws ServiceException {
BooleanQuery fq = new BooleanQuery();
boolean emptyQuery = true;
for (Iterator fieldIter = queryElem.elementIterator(AdminConstants.E_FIELD); fieldIter.hasNext(); ) {
emptyQuery = false;
Element fieldElement = (Element) fieldIter.next();
String fieldName = fieldElement.getAttribute(AdminConstants.A_NAME);
BooleanQuery mq = new BooleanQuery();
for (Iterator matchIter = fieldElement.elementIterator(AdminConstants.E_MATCH); matchIter.hasNext(); ) {
Element matchElement = (Element) matchIter.next();
String matchValue = matchElement.getAttribute(AdminConstants.A_VALUE);
Term term = new Term(fieldName, matchValue);
mq.add(new TermQuery(term), Occur.SHOULD);
}
fq.add(mq, Occur.MUST);
}
if (emptyQuery) {
return null;
} else {
return fq;
}
}
use of org.apache.lucene.search.TermQuery in project zm-mailbox by Zimbra.
the class LuceneQueryOperation method fixMustNotOnly.
/**
* It is not possible to search for queries that only consist of a MUST_NOT clause. Combining with MatchAllDocsQuery
* works in general, but we generate more than one documents per item for multipart messages. If we match including
* non top level parts, negative queries will end up matching everything. Therefore we only match the top level part
* for negative queries.
*/
private void fixMustNotOnly(BooleanQuery query) {
for (BooleanClause clause : query.clauses()) {
if (clause.getQuery() instanceof BooleanQuery) {
fixMustNotOnly((BooleanQuery) clause.getQuery());
}
if (clause.getOccur() != BooleanClause.Occur.MUST_NOT) {
return;
}
}
query.add(new TermQuery(new Term(LuceneFields.L_PARTNAME, LuceneFields.L_PARTNAME_TOP)), BooleanClause.Occur.SHOULD);
Set<MailItem.Type> types = context.getParams().getTypes();
if (types.contains(MailItem.Type.CONTACT)) {
query.add(new TermQuery(new Term(LuceneFields.L_PARTNAME, LuceneFields.L_PARTNAME_CONTACT)), BooleanClause.Occur.SHOULD);
}
if (types.contains(MailItem.Type.NOTE)) {
query.add(new TermQuery(new Term(LuceneFields.L_PARTNAME, LuceneFields.L_PARTNAME_NOTE)), BooleanClause.Occur.SHOULD);
}
}
use of org.apache.lucene.search.TermQuery in project zm-mailbox by Zimbra.
the class LuceneIndex method warmup.
/**
* Runs a common search query + common sort order (and throw away the result) to warm up the Lucene cache and OS
* file system cache.
*/
@Override
public synchronized void warmup() {
if (SEARCHER_CACHE.asMap().containsKey(mailbox.getId()) || GAL_SEARCHER_CACHE.containsKey(mailbox.getId())) {
// already warmed up
return;
}
long start = System.currentTimeMillis();
IndexSearcher searcher = null;
try {
searcher = (IndexSearcher) openSearcher();
searcher.search(new TermQuery(new Term(LuceneFields.L_CONTENT, "zimbra")), 1, new Sort(new SortField(LuceneFields.L_SORT_DATE, SortField.STRING, true)));
} catch (IOException e) {
ZimbraLog.search.warn("Failed to warm up", e);
} finally {
Closeables.closeQuietly(searcher);
}
ZimbraLog.search.debug("WarmUpLuceneSearcher elapsed=%d", System.currentTimeMillis() - start);
}
use of org.apache.lucene.search.TermQuery in project zm-mailbox by Zimbra.
the class DomainQuery method compile.
@Override
public QueryOperation compile(Mailbox mbox, boolean bool) {
LuceneQueryOperation op = new LuceneQueryOperation();
op.addClause(toQueryString(field, term), new TermQuery(new Term(field, term)), evalBool(bool));
return op;
}
use of org.apache.lucene.search.TermQuery in project geode by apache.
the class LuceneQueriesDUnitTest method canQueryWithCustomLuceneQueryObject.
@Test
@Parameters(method = "getListOfRegionTestTypes")
public void canQueryWithCustomLuceneQueryObject(RegionTestableType regionTestType) {
SerializableRunnableIF createIndex = () -> {
LuceneService luceneService = LuceneServiceProvider.get(getCache());
luceneService.createIndexFactory().addField("text").create(INDEX_NAME, REGION_NAME);
};
dataStore1.invoke(() -> initDataStore(createIndex, regionTestType));
dataStore2.invoke(() -> initDataStore(createIndex, regionTestType));
accessor.invoke(() -> initAccessor(createIndex, regionTestType));
putDataInRegion(accessor);
assertTrue(waitForFlushBeforeExecuteTextSearch(accessor, 60000));
assertTrue(waitForFlushBeforeExecuteTextSearch(dataStore1, 60000));
// Execute a query with a custom lucene query object
accessor.invoke(() -> {
Cache cache = getCache();
LuceneService service = LuceneServiceProvider.get(cache);
LuceneQuery query = service.createLuceneQueryFactory().create(INDEX_NAME, REGION_NAME, index -> {
return new TermQuery(new Term("text", "world"));
});
final PageableLuceneQueryResults results = query.findPages();
assertEquals(3, results.size());
});
}
Aggregations