use of org.apache.lucene.search.BoostQuery in project elasticsearch by elastic.
the class MultiMatchQuery method parseAndApply.
private Query parseAndApply(Type type, String fieldName, Object value, String minimumShouldMatch, Float boostValue) throws IOException {
Query query = parse(type, fieldName, value);
query = Queries.maybeApplyMinimumShouldMatch(query, minimumShouldMatch);
if (query != null && boostValue != null && boostValue != AbstractQueryBuilder.DEFAULT_BOOST) {
query = new BoostQuery(query, boostValue);
}
return query;
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class TestSolrQueryParser method testCSQ.
@Test
public void testCSQ() throws Exception {
SolrQueryRequest req = req();
QParser qParser = QParser.getParser("text:x^=3", req);
Query q = qParser.getQuery();
assertTrue(q instanceof BoostQuery);
assertTrue(((BoostQuery) q).getQuery() instanceof ConstantScoreQuery);
assertEquals(3.0, ((BoostQuery) q).getBoost(), 0.0f);
qParser = QParser.getParser("(text:x text:y)^=-3", req);
q = qParser.getQuery();
assertTrue(q instanceof BoostQuery);
assertTrue(((BoostQuery) q).getQuery() instanceof ConstantScoreQuery);
assertEquals(-3.0, ((BoostQuery) q).getBoost(), 0.0f);
req.close();
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class TestExtendedDismaxParser method containsClause.
private boolean containsClause(Query query, String field, String value, int boost, boolean fuzzy) {
float queryBoost = 1f;
if (query instanceof BoostQuery) {
BoostQuery bq = (BoostQuery) query;
query = bq.getQuery();
queryBoost = bq.getBoost();
}
if (query instanceof BooleanQuery) {
return containsClause((BooleanQuery) query, field, value, boost, fuzzy);
}
if (query instanceof DisjunctionMaxQuery) {
return containsClause((DisjunctionMaxQuery) query, field, value, boost, fuzzy);
}
if (boost != queryBoost) {
return false;
}
if (query instanceof TermQuery && !fuzzy) {
return containsClause((TermQuery) query, field, value);
}
if (query instanceof FuzzyQuery && fuzzy) {
return containsClause((FuzzyQuery) query, field, value);
}
return false;
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class CommonTermsQuery method buildQuery.
protected Query buildQuery(final int maxDoc, final TermContext[] contextArray, final Term[] queryTerms) {
List<Query> lowFreqQueries = new ArrayList<>();
List<Query> highFreqQueries = new ArrayList<>();
for (int i = 0; i < queryTerms.length; i++) {
TermContext termContext = contextArray[i];
if (termContext == null) {
lowFreqQueries.add(newTermQuery(queryTerms[i], null));
} else {
if ((maxTermFrequency >= 1f && termContext.docFreq() > maxTermFrequency) || (termContext.docFreq() > (int) Math.ceil(maxTermFrequency * (float) maxDoc))) {
highFreqQueries.add(newTermQuery(queryTerms[i], termContext));
} else {
lowFreqQueries.add(newTermQuery(queryTerms[i], termContext));
}
}
}
final int numLowFreqClauses = lowFreqQueries.size();
final int numHighFreqClauses = highFreqQueries.size();
Occur lowFreqOccur = this.lowFreqOccur;
Occur highFreqOccur = this.highFreqOccur;
int lowFreqMinShouldMatch = 0;
int highFreqMinShouldMatch = 0;
if (lowFreqOccur == Occur.SHOULD && numLowFreqClauses > 0) {
lowFreqMinShouldMatch = calcLowFreqMinimumNumberShouldMatch(numLowFreqClauses);
}
if (highFreqOccur == Occur.SHOULD && numHighFreqClauses > 0) {
highFreqMinShouldMatch = calcHighFreqMinimumNumberShouldMatch(numHighFreqClauses);
}
if (lowFreqQueries.isEmpty()) {
/*
* if lowFreq is empty we rewrite the high freq terms in a conjunction to
* prevent slow queries.
*/
if (highFreqMinShouldMatch == 0 && highFreqOccur != Occur.MUST) {
highFreqOccur = Occur.MUST;
}
}
BooleanQuery.Builder builder = new BooleanQuery.Builder();
if (lowFreqQueries.isEmpty() == false) {
BooleanQuery.Builder lowFreq = new BooleanQuery.Builder();
for (Query query : lowFreqQueries) {
lowFreq.add(query, lowFreqOccur);
}
lowFreq.setMinimumNumberShouldMatch(lowFreqMinShouldMatch);
Query lowFreqQuery = lowFreq.build();
builder.add(new BoostQuery(lowFreqQuery, lowFreqBoost), Occur.MUST);
}
if (highFreqQueries.isEmpty() == false) {
BooleanQuery.Builder highFreq = new BooleanQuery.Builder();
for (Query query : highFreqQueries) {
highFreq.add(query, highFreqOccur);
}
highFreq.setMinimumNumberShouldMatch(highFreqMinShouldMatch);
Query highFreqQuery = highFreq.build();
builder.add(new BoostQuery(highFreqQuery, highFreqBoost), Occur.SHOULD);
}
return builder.build();
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class SimpleQueryParser method newPhraseQuery.
/**
* Factory method to generate a phrase query with slop.
*/
protected Query newPhraseQuery(String text, int slop) {
BooleanQuery.Builder bq = new BooleanQuery.Builder();
for (Map.Entry<String, Float> entry : weights.entrySet()) {
Query q = createPhraseQuery(entry.getKey(), text, slop);
if (q != null) {
float boost = entry.getValue();
if (boost != 1f) {
q = new BoostQuery(q, boost);
}
bq.add(q, BooleanClause.Occur.SHOULD);
}
}
return simplify(bq.build());
}
Aggregations