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