Search in sources :

Example 96 with TermQuery

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;
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Element(com.zimbra.common.soap.Element) Iterator(java.util.Iterator) Term(org.apache.lucene.index.Term)

Example 97 with TermQuery

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);
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term)

Example 98 with TermQuery

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);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term) IOException(java.io.IOException)

Example 99 with TermQuery

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;
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) LuceneQueryOperation(com.zimbra.cs.index.LuceneQueryOperation) Term(org.apache.lucene.index.Term)

Example 100 with TermQuery

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());
    });
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) SerializableRunnableIF(org.apache.geode.test.dunit.SerializableRunnableIF) Cache(org.apache.geode.cache.Cache) Parameters(junitparams.Parameters) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

TermQuery (org.apache.lucene.search.TermQuery)656 Term (org.apache.lucene.index.Term)543 BooleanQuery (org.apache.lucene.search.BooleanQuery)337 Query (org.apache.lucene.search.Query)265 IndexSearcher (org.apache.lucene.search.IndexSearcher)249 Document (org.apache.lucene.document.Document)209 Directory (org.apache.lucene.store.Directory)164 TopDocs (org.apache.lucene.search.TopDocs)162 IndexReader (org.apache.lucene.index.IndexReader)124 PhraseQuery (org.apache.lucene.search.PhraseQuery)120 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)116 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)116 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)114 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)97 BoostQuery (org.apache.lucene.search.BoostQuery)84 Field (org.apache.lucene.document.Field)81 PrefixQuery (org.apache.lucene.search.PrefixQuery)74 Test (org.junit.Test)71 ArrayList (java.util.ArrayList)70 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)62