Search in sources :

Example 21 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.

the class Queries method applyMinimumShouldMatch.

public static Query applyMinimumShouldMatch(BooleanQuery query, @Nullable String minimumShouldMatch) {
    if (minimumShouldMatch == null) {
        return query;
    }
    int optionalClauses = 0;
    for (BooleanClause c : query.clauses()) {
        if (c.getOccur() == BooleanClause.Occur.SHOULD) {
            optionalClauses++;
        }
    }
    int msm = calculateMinShouldMatch(optionalClauses, minimumShouldMatch);
    if (0 < msm) {
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        builder.setDisableCoord(query.isCoordDisabled());
        for (BooleanClause clause : query) {
            builder.add(clause);
        }
        builder.setMinimumNumberShouldMatch(msm);
        return builder.build();
    } else {
        return query;
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery)

Example 22 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.

the class BoolQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
    BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();
    booleanQueryBuilder.setDisableCoord(disableCoord);
    addBooleanClauses(context, booleanQueryBuilder, mustClauses, BooleanClause.Occur.MUST);
    addBooleanClauses(context, booleanQueryBuilder, mustNotClauses, BooleanClause.Occur.MUST_NOT);
    addBooleanClauses(context, booleanQueryBuilder, shouldClauses, BooleanClause.Occur.SHOULD);
    addBooleanClauses(context, booleanQueryBuilder, filterClauses, BooleanClause.Occur.FILTER);
    BooleanQuery booleanQuery = booleanQueryBuilder.build();
    if (booleanQuery.clauses().isEmpty()) {
        return new MatchAllDocsQuery();
    }
    final String minimumShouldMatch;
    if (context.isFilter() && this.minimumShouldMatch == null && shouldClauses.size() > 0) {
        minimumShouldMatch = "1";
    } else {
        minimumShouldMatch = this.minimumShouldMatch;
    }
    Query query = Queries.applyMinimumShouldMatch(booleanQuery, minimumShouldMatch);
    return adjustPureNegative ? fixNegativeQueryIfNeeded(query) : query;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 23 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project elasticsearch by elastic.

the class CustomUnifiedHighlighterTests method testSentenceBoundedBreakIterator.

public void testSentenceBoundedBreakIterator() throws Exception {
    final String[] inputs = { "The quick brown fox in a long sentence with another quick brown fox. " + "Another sentence with brown fox." };
    final String[] outputs = { "The <b>quick</b> <b>brown</b>", "<b>fox</b> in a long", "with another <b>quick</b>", "<b>brown</b> <b>fox</b>.", "sentence with <b>brown</b>", "<b>fox</b>." };
    BooleanQuery query = new BooleanQuery.Builder().add(new TermQuery(new Term("text", "quick")), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term("text", "brown")), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term("text", "fox")), BooleanClause.Occur.SHOULD).build();
    assertHighlightOneDoc("text", inputs, new StandardAnalyzer(), query, Locale.ROOT, BoundedBreakIteratorScanner.getSentence(Locale.ROOT, 10), 0, outputs);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) AllTermQuery(org.elasticsearch.common.lucene.all.AllTermQuery) TermQuery(org.apache.lucene.search.TermQuery) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Term(org.apache.lucene.index.Term)

Example 24 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project neo4j-mobile-android by neo4j-contrib.

the class FullTxData method injectOrphans.

private Query injectOrphans(Query query) {
    if (query instanceof BooleanQuery) {
        BooleanQuery source = (BooleanQuery) query;
        BooleanQuery result = new BooleanQuery();
        for (BooleanClause clause : source.clauses()) {
            result.add(injectOrphans(clause.getQuery()), clause.getOccur());
        }
        return result;
    } else {
        BooleanQuery result = new BooleanQuery();
        result.add(query, Occur.SHOULD);
        result.add(new TermQuery(new Term(ORPHANS_KEY, extractTermField(query))), Occur.SHOULD);
        return result;
    }
}
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 25 with BooleanQuery

use of org.apache.lucene.search.BooleanQuery in project graphdb by neo4j-attic.

the class FullTxData method injectOrphans.

private Query injectOrphans(Query query) {
    if (query instanceof BooleanQuery) {
        BooleanQuery source = (BooleanQuery) query;
        BooleanQuery result = new BooleanQuery();
        for (BooleanClause clause : source.clauses()) {
            result.add(injectOrphans(clause.getQuery()), clause.getOccur());
        }
        return result;
    } else {
        Set<Term> terms = new HashSet<Term>();
        query.extractTerms(terms);
        // TODO Don't only use the first term
        Term term = terms.iterator().next();
        BooleanQuery result = new BooleanQuery();
        result.add(query, Occur.SHOULD);
        result.add(new TermQuery(new Term(ORPHANS_KEY, term.field())), Occur.SHOULD);
        return result;
    }
}
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) HashSet(java.util.HashSet)

Aggregations

BooleanQuery (org.apache.lucene.search.BooleanQuery)218 TermQuery (org.apache.lucene.search.TermQuery)132 Term (org.apache.lucene.index.Term)114 Query (org.apache.lucene.search.Query)91 BooleanClause (org.apache.lucene.search.BooleanClause)59 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)52 BoostQuery (org.apache.lucene.search.BoostQuery)49 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)45 PhraseQuery (org.apache.lucene.search.PhraseQuery)43 PrefixQuery (org.apache.lucene.search.PrefixQuery)39 TopDocs (org.apache.lucene.search.TopDocs)38 IndexReader (org.apache.lucene.index.IndexReader)37 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)34 IndexSearcher (org.apache.lucene.search.IndexSearcher)33 WildcardQuery (org.apache.lucene.search.WildcardQuery)32 Document (org.apache.lucene.document.Document)30 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)28 ArrayList (java.util.ArrayList)27 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)27 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)26