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;
}
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);
}
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;
}
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();
}
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();
}
Aggregations