Search in sources :

Example 21 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project querydsl by querydsl.

the class LuceneSerializer method ne.

protected Query ne(Operation<?> operation, QueryMetadata metadata, boolean ignoreCase) {
    BooleanQuery bq = new BooleanQuery();
    bq.add(new BooleanClause(eq(operation, metadata, ignoreCase), Occur.MUST_NOT));
    bq.add(new BooleanClause(new MatchAllDocsQuery(), Occur.MUST));
    return bq;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 22 with BooleanClause

use of org.apache.lucene.search.BooleanClause 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 23 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project zm-mailbox by Zimbra.

the class LuceneQueryOperation method combineOps.

@Override
protected QueryOperation combineOps(QueryOperation other, boolean union) {
    assert (!haveRunSearch);
    if (union) {
        if (other.hasNoResults()) {
            queryInfo.addAll(other.getResultInfo());
            // a query for (other OR nothing) == other
            return this;
        }
    } else {
        if (other.hasAllResults()) {
            if (other.hasSpamTrashSetting()) {
                forceHasSpamTrashSetting();
            }
            queryInfo.addAll(other.getResultInfo());
            // we match all results.  (other AND anything) == other
            return this;
        }
    }
    if (other instanceof LuceneQueryOperation) {
        LuceneQueryOperation otherLucene = (LuceneQueryOperation) other;
        if (union) {
            queryString = '(' + queryString + ") OR (" + otherLucene.queryString + ')';
        } else {
            queryString = '(' + queryString + ") AND (" + otherLucene.queryString + ')';
        }
        BooleanQuery top = new BooleanQuery();
        if (union) {
            if (luceneQuery instanceof BooleanQuery) {
                orCopy((BooleanQuery) luceneQuery, top);
            } else {
                top.add(new BooleanClause(luceneQuery, Occur.SHOULD));
            }
            if (otherLucene.luceneQuery instanceof BooleanQuery) {
                orCopy((BooleanQuery) otherLucene.luceneQuery, top);
            } else {
                top.add(new BooleanClause(otherLucene.luceneQuery, Occur.SHOULD));
            }
        } else {
            if (luceneQuery instanceof BooleanQuery) {
                andCopy((BooleanQuery) luceneQuery, top);
            } else {
                top.add(new BooleanClause(luceneQuery, Occur.MUST));
            }
            if (otherLucene.luceneQuery instanceof BooleanQuery) {
                andCopy((BooleanQuery) otherLucene.luceneQuery, top);
            } else {
                top.add(new BooleanClause(otherLucene.luceneQuery, Occur.MUST));
            }
        }
        luceneQuery = top;
        queryInfo.addAll(other.getResultInfo());
        if (other.hasSpamTrashSetting()) {
            forceHasSpamTrashSetting();
        }
        return this;
    }
    return null;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery)

Example 24 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project incubator-atlas by apache.

the class BooleanQueryExpression method processAndClauses.

private Collection<Pipe> processAndClauses(Map<BooleanClause.Occur, Collection<BooleanClause>> groupedClauses) {
    Collection<BooleanClause> andClauses = groupedClauses.get(BooleanClause.Occur.MUST);
    Collection<Pipe> andPipes = new ArrayList<>();
    if (andClauses != null) {
        for (BooleanClause andClause : andClauses) {
            QueryExpression queryExpression = queryFactory.create(andClause.getQuery(), resourceDefinition);
            properties.addAll(queryExpression.getProperties());
            andPipes.add(queryExpression.asPipe());
        }
    }
    return andPipes;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) Pipe(com.tinkerpop.pipes.Pipe) OrFilterPipe(com.tinkerpop.pipes.filter.OrFilterPipe) AndFilterPipe(com.tinkerpop.pipes.filter.AndFilterPipe)

Example 25 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project incubator-atlas by apache.

the class BooleanQueryExpression method processNotClauses.

private Collection<Pipe> processNotClauses(Map<BooleanClause.Occur, Collection<BooleanClause>> groupedClauses) {
    Collection<BooleanClause> notClauses = groupedClauses.get(BooleanClause.Occur.MUST_NOT);
    Collection<Pipe> notPipes = new ArrayList<>();
    if (notClauses != null) {
        for (BooleanClause notClause : notClauses) {
            QueryExpression queryExpression = queryFactory.create(notClause.getQuery(), resourceDefinition);
            queryExpression.setNegate();
            properties.addAll(queryExpression.getProperties());
            notPipes.add(queryExpression.asPipe());
        }
    }
    return notPipes;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) Pipe(com.tinkerpop.pipes.Pipe) OrFilterPipe(com.tinkerpop.pipes.filter.OrFilterPipe) AndFilterPipe(com.tinkerpop.pipes.filter.AndFilterPipe)

Aggregations

BooleanClause (org.apache.lucene.search.BooleanClause)102 BooleanQuery (org.apache.lucene.search.BooleanQuery)92 Query (org.apache.lucene.search.Query)56 TermQuery (org.apache.lucene.search.TermQuery)46 Term (org.apache.lucene.index.Term)44 BoostQuery (org.apache.lucene.search.BoostQuery)33 ArrayList (java.util.ArrayList)23 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)21 PhraseQuery (org.apache.lucene.search.PhraseQuery)20 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)19 SynonymQuery (org.apache.lucene.search.SynonymQuery)18 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)18 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)17 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)16 SpanQuery (org.apache.lucene.search.spans.SpanQuery)15 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)14 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)14 WildcardQuery (org.apache.lucene.search.WildcardQuery)14 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)13 PrefixQuery (org.apache.lucene.search.PrefixQuery)13