use of org.apache.lucene.search.BooleanClause in project lucene-solr by apache.
the class FastVectorHighlighterTest method testWithSynonym.
public void testWithSynonym() throws IOException {
Directory dir = newDirectory();
IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
FieldType type = new FieldType(TextField.TYPE_STORED);
type.setStoreTermVectorOffsets(true);
type.setStoreTermVectorPositions(true);
type.setStoreTermVectors(true);
type.freeze();
Document doc = new Document();
doc.add(new Field("field", "the quick brown fox", type));
writer.addDocument(doc);
FastVectorHighlighter highlighter = new FastVectorHighlighter();
IndexReader reader = DirectoryReader.open(writer);
int docId = 0;
// query1: simple synonym query
SynonymQuery synQuery = new SynonymQuery(new Term("field", "quick"), new Term("field", "fast"));
FieldQuery fieldQuery = highlighter.getFieldQuery(synQuery, reader);
String[] bestFragments = highlighter.getBestFragments(fieldQuery, reader, docId, "field", 54, 1);
assertEquals("the <b>quick</b> brown fox", bestFragments[0]);
// query2: boolean query with synonym query
BooleanQuery.Builder bq = new BooleanQuery.Builder().add(new BooleanClause(synQuery, Occur.MUST)).add(new BooleanClause(new TermQuery(new Term("field", "fox")), Occur.MUST));
fieldQuery = highlighter.getFieldQuery(bq.build(), reader);
bestFragments = highlighter.getBestFragments(fieldQuery, reader, docId, "field", 54, 1);
assertEquals("the <b>quick</b> brown <b>fox</b>", bestFragments[0]);
reader.close();
writer.close();
dir.close();
}
use of org.apache.lucene.search.BooleanClause in project lucene-solr by apache.
the class SpellChecker method add.
/**
* Add a clause to a boolean query.
*/
private static void add(BooleanQuery.Builder q, String name, String value, float boost) {
Query tq = new TermQuery(new Term(name, value));
q.add(new BooleanClause(new BoostQuery(tq, boost), BooleanClause.Occur.SHOULD));
}
use of org.apache.lucene.search.BooleanClause in project lucene-solr by apache.
the class QueryUtils method fixNegativeQuery.
/** Fixes a negative query by adding a MatchAllDocs query clause.
* The query passed in *must* be a negative query.
*/
public static Query fixNegativeQuery(Query q) {
float boost = 1f;
if (q instanceof BoostQuery) {
BoostQuery bq = (BoostQuery) q;
boost = bq.getBoost();
q = bq.getQuery();
}
BooleanQuery bq = (BooleanQuery) q;
BooleanQuery.Builder newBqB = new BooleanQuery.Builder();
newBqB.setMinimumNumberShouldMatch(bq.getMinimumNumberShouldMatch());
for (BooleanClause clause : bq) {
newBqB.add(clause);
}
newBqB.add(new MatchAllDocsQuery(), Occur.MUST);
BooleanQuery newBq = newBqB.build();
return new BoostQuery(newBq, boost);
}
use of org.apache.lucene.search.BooleanClause in project lucene-solr by apache.
the class QueryUtils method isNegative.
/** return true if this query has no positive components */
public static boolean isNegative(Query q) {
if (!(q instanceof BooleanQuery))
return false;
BooleanQuery bq = (BooleanQuery) q;
Collection<BooleanClause> clauses = bq.clauses();
if (clauses.size() == 0)
return false;
for (BooleanClause clause : clauses) {
if (!clause.isProhibited())
return false;
}
return true;
}
use of org.apache.lucene.search.BooleanClause in project lucene-solr by apache.
the class QueryUtils method getAbs.
/** Returns the original query if it was already a positive query, otherwise
* return the negative of the query (i.e., a positive query).
* <p>
* Example: both id:10 and id:-10 will return id:10
* <p>
* The caller can tell the sign of the original by a reference comparison between
* the original and returned query.
* @param q Query to create the absolute version of
* @return Absolute version of the Query
*/
public static Query getAbs(Query q) {
if (q instanceof BoostQuery) {
BoostQuery bq = (BoostQuery) q;
Query subQ = bq.getQuery();
Query absSubQ = getAbs(subQ);
if (absSubQ == subQ)
return q;
return new BoostQuery(absSubQ, bq.getBoost());
}
if (q instanceof WrappedQuery) {
Query subQ = ((WrappedQuery) q).getWrappedQuery();
Query absSubQ = getAbs(subQ);
if (absSubQ == subQ)
return q;
return new WrappedQuery(absSubQ);
}
if (!(q instanceof BooleanQuery))
return q;
BooleanQuery bq = (BooleanQuery) q;
Collection<BooleanClause> clauses = bq.clauses();
if (clauses.size() == 0)
return q;
for (BooleanClause clause : clauses) {
if (!clause.isProhibited())
return q;
}
if (clauses.size() == 1) {
// if only one clause, dispense with the wrapping BooleanQuery
Query negClause = clauses.iterator().next().getQuery();
// not contribute to a score.
return negClause;
} else {
BooleanQuery.Builder newBqB = new BooleanQuery.Builder();
// the inverse of -a -b is a OR b
for (BooleanClause clause : clauses) {
newBqB.add(clause.getQuery(), BooleanClause.Occur.SHOULD);
}
return newBqB.build();
}
}
Aggregations