Search in sources :

Example 31 with BooleanClause

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

the class CachingNaiveBayesClassifier method getWordFreqForClassess.

private Map<BytesRef, Integer> getWordFreqForClassess(String word) throws IOException {
    Map<BytesRef, Integer> insertPoint;
    insertPoint = termCClassHitCache.get(word);
    // if we get the answer from the cache
    if (insertPoint != null) {
        if (!insertPoint.isEmpty()) {
            return insertPoint;
        }
    }
    Map<BytesRef, Integer> searched = new ConcurrentHashMap<>();
    // if we dont get the answer, but it's relevant we must search it and insert to the cache
    if (insertPoint != null || !justCachedTerms) {
        for (BytesRef cclass : cclasses) {
            BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
            BooleanQuery.Builder subQuery = new BooleanQuery.Builder();
            for (String textFieldName : textFieldNames) {
                subQuery.add(new BooleanClause(new TermQuery(new Term(textFieldName, word)), BooleanClause.Occur.SHOULD));
            }
            booleanQuery.add(new BooleanClause(subQuery.build(), BooleanClause.Occur.MUST));
            booleanQuery.add(new BooleanClause(new TermQuery(new Term(classFieldName, cclass)), BooleanClause.Occur.MUST));
            if (query != null) {
                booleanQuery.add(query, BooleanClause.Occur.MUST);
            }
            TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
            indexSearcher.search(booleanQuery.build(), totalHitCountCollector);
            int ret = totalHitCountCollector.getTotalHits();
            if (ret != 0) {
                searched.put(cclass, ret);
            }
        }
        if (insertPoint != null) {
            // threadsafe and concurrent write
            termCClassHitCache.put(word, searched);
        }
    }
    return searched;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) BooleanClause(org.apache.lucene.search.BooleanClause) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BytesRef(org.apache.lucene.util.BytesRef)

Example 32 with BooleanClause

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

the class KNearestFuzzyClassifier method knnSearch.

private TopDocs knnSearch(String text) throws IOException {
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    FuzzyLikeThisQuery fuzzyLikeThisQuery = new FuzzyLikeThisQuery(300, analyzer);
    for (String fieldName : textFieldNames) {
        // TODO: make this parameters configurable
        fuzzyLikeThisQuery.addTerms(text, fieldName, 1f, 2);
    }
    bq.add(fuzzyLikeThisQuery, BooleanClause.Occur.MUST);
    Query classFieldQuery = new WildcardQuery(new Term(classFieldName, "*"));
    bq.add(new BooleanClause(classFieldQuery, BooleanClause.Occur.MUST));
    if (query != null) {
        bq.add(query, BooleanClause.Occur.MUST);
    }
    return indexSearcher.search(bq.build(), k);
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) FuzzyLikeThisQuery(org.apache.lucene.sandbox.queries.FuzzyLikeThisQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) Query(org.apache.lucene.search.Query) FuzzyLikeThisQuery(org.apache.lucene.sandbox.queries.FuzzyLikeThisQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Term(org.apache.lucene.index.Term)

Example 33 with BooleanClause

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

the class BM25NBClassifier method getTermProbForClass.

private double getTermProbForClass(Term classTerm, String... words) throws IOException {
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    builder.add(new BooleanClause(new TermQuery(classTerm), BooleanClause.Occur.MUST));
    for (String textFieldName : textFieldNames) {
        for (String word : words) {
            builder.add(new BooleanClause(new TermQuery(new Term(textFieldName, word)), BooleanClause.Occur.SHOULD));
        }
    }
    if (query != null) {
        builder.add(query, BooleanClause.Occur.MUST);
    }
    TopDocs search = indexSearcher.search(builder.build(), 1);
    return search.totalHits > 0 ? search.getMaxScore() : 1;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) TopDocs(org.apache.lucene.search.TopDocs) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term)

Example 34 with BooleanClause

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

the class HandyQueryBuilder method getQuery.

public Query getQuery(Element e) throws ParserException {
    final BooleanQuery.Builder bq = new BooleanQuery.Builder();
    final Query lhsQ = getSubQuery(e, "Left");
    final Query rhsQ = getSubQuery(e, "Right");
    bq.add(new BooleanClause(lhsQ, BooleanClause.Occur.SHOULD));
    bq.add(new BooleanClause(rhsQ, BooleanClause.Occur.SHOULD));
    return bq.build();
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanQueryBuilder(org.apache.lucene.queryparser.xml.builders.SpanQueryBuilder)

Example 35 with BooleanClause

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

the class SimpleQueryParser method addClause.

private static BooleanQuery addClause(BooleanQuery bq, Query query, BooleanClause.Occur occur) {
    BooleanQuery.Builder newBq = new BooleanQuery.Builder();
    newBq.setMinimumNumberShouldMatch(bq.getMinimumNumberShouldMatch());
    for (BooleanClause clause : bq) {
        newBq.add(clause);
    }
    newBq.add(query, occur);
    return newBq.build();
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) QueryBuilder(org.apache.lucene.util.QueryBuilder)

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