Search in sources :

Example 26 with BooleanClause

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

the class BooleanQueryExpression method groupClauses.

private Map<BooleanClause.Occur, Collection<BooleanClause>> groupClauses() {
    Map<BooleanClause.Occur, Collection<BooleanClause>> groupedClauses = new HashMap<>();
    for (BooleanClause clause : clauses) {
        BooleanClause.Occur occur = resolveClauseOccur(clause);
        Collection<BooleanClause> clauseGrouping = groupedClauses.get(occur);
        if (clauseGrouping == null) {
            clauseGrouping = new ArrayList<>();
            groupedClauses.put(occur, clauseGrouping);
        }
        clauseGrouping.add(clause);
    }
    return groupedClauses;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause)

Example 27 with BooleanClause

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

the class AbstractExcerpt method getQueryTerms.

private static void getQueryTerms(Query q, Set<Term[]> relevantTerms) {
    if (q instanceof BooleanQuery) {
        final BooleanQuery bq = (BooleanQuery) q;
        for (BooleanClause clause : bq.getClauses()) {
            getQueryTerms(clause.getQuery(), relevantTerms);
        }
        return;
    }
    //need to preserve insertion order
    Set<Term> extractedTerms = new LinkedHashSet<Term>();
    q.extractTerms(extractedTerms);
    Set<Term> filteredTerms = filterRelevantTerms(extractedTerms);
    if (!filteredTerms.isEmpty()) {
        if (q instanceof PhraseQuery) {
            // inline the terms, basically a 'must all' condition
            relevantTerms.add(filteredTerms.toArray(new Term[] {}));
        } else {
            // each possible term gets a new slot
            for (Term t : filteredTerms) {
                relevantTerms.add(new Term[] { t });
            }
        }
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) LinkedHashSet(java.util.LinkedHashSet) BooleanQuery(org.apache.lucene.search.BooleanQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) Term(org.apache.lucene.index.Term)

Example 28 with BooleanClause

use of org.apache.lucene.search.BooleanClause in project jackrabbit-oak by apache.

the class LucenePropertyIndex method performAdditionalWraps.

/**
     * Perform additional wraps on the list of queries to allow, for example, the NOT CONTAINS to
     * play properly when sent to lucene.
     *
     * @param qs the list of queries. Cannot be null.
     * @return
     */
@Nonnull
public static LuceneRequestFacade<Query> performAdditionalWraps(@Nonnull List<Query> qs) {
    checkNotNull(qs);
    if (qs.size() == 1) {
        Query q = qs.get(0);
        if (q instanceof BooleanQuery) {
            BooleanQuery ibq = (BooleanQuery) q;
            boolean onlyNotClauses = true;
            for (BooleanClause c : ibq.getClauses()) {
                if (c.getOccur() != BooleanClause.Occur.MUST_NOT) {
                    onlyNotClauses = false;
                    break;
                }
            }
            if (onlyNotClauses) {
                // if we have only NOT CLAUSES we have to add a match all docs (*.*) for the
                // query to work
                ibq.add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD);
            }
        }
        return new LuceneRequestFacade<Query>(qs.get(0));
    }
    BooleanQuery bq = new BooleanQuery();
    for (Query q : qs) {
        boolean unwrapped = false;
        if (q instanceof BooleanQuery) {
            unwrapped = unwrapMustNot((BooleanQuery) q, bq);
        }
        if (!unwrapped) {
            bq.add(q, MUST);
        }
    }
    return new LuceneRequestFacade<Query>(bq);
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) NumericRangeQuery(org.apache.lucene.search.NumericRangeQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Nonnull(javax.annotation.Nonnull)

Example 29 with BooleanClause

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

the class KNearestNeighborClassifier method knnSearch.

private TopDocs knnSearch(String text) throws IOException {
    BooleanQuery.Builder mltQuery = new BooleanQuery.Builder();
    for (String fieldName : textFieldNames) {
        String boost = null;
        //terms boost actually helps in MLT queries
        mlt.setBoost(true);
        if (fieldName.contains("^")) {
            String[] field2boost = fieldName.split("\\^");
            fieldName = field2boost[0];
            boost = field2boost[1];
        }
        if (boost != null) {
            //if we have a field boost, we add it
            mlt.setBoostFactor(Float.parseFloat(boost));
        }
        mltQuery.add(new BooleanClause(mlt.like(fieldName, new StringReader(text)), BooleanClause.Occur.SHOULD));
        // restore neutral boost for next field
        mlt.setBoostFactor(1);
    }
    Query classFieldQuery = new WildcardQuery(new Term(classFieldName, "*"));
    mltQuery.add(new BooleanClause(classFieldQuery, BooleanClause.Occur.MUST));
    if (query != null) {
        mltQuery.add(query, BooleanClause.Occur.MUST);
    }
    return indexSearcher.search(mltQuery.build(), k);
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(org.apache.lucene.search.Query) WildcardQuery(org.apache.lucene.search.WildcardQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) StringReader(java.io.StringReader) Term(org.apache.lucene.index.Term)

Example 30 with BooleanClause

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

the class SimpleNaiveBayesDocumentClassifier method getWordFreqForClass.

/**
   * Returns the number of documents of the input class ( from the whole index or from a subset)
   * that contains the word ( in a specific field or in all the fields if no one selected)
   *
   * @param word      the token produced by the analyzer
   * @param fieldName the field the word is coming from
   * @param term      the class term
   * @return number of documents of the input class
   * @throws java.io.IOException If there is a low-level I/O error
   */
private int getWordFreqForClass(String word, String fieldName, Term term) throws IOException {
    BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
    BooleanQuery.Builder subQuery = new BooleanQuery.Builder();
    subQuery.add(new BooleanClause(new TermQuery(new Term(fieldName, word)), BooleanClause.Occur.SHOULD));
    booleanQuery.add(new BooleanClause(subQuery.build(), BooleanClause.Occur.MUST));
    booleanQuery.add(new BooleanClause(new TermQuery(term), BooleanClause.Occur.MUST));
    if (query != null) {
        booleanQuery.add(query, BooleanClause.Occur.MUST);
    }
    TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
    indexSearcher.search(booleanQuery.build(), totalHitCountCollector);
    return totalHitCountCollector.getTotalHits();
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) Term(org.apache.lucene.index.Term)

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