use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class SimpleQueryParser method newPrefixQuery.
/**
* Factory method to generate a prefix query.
*/
protected Query newPrefixQuery(String text) {
BooleanQuery.Builder bq = new BooleanQuery.Builder();
for (Map.Entry<String, Float> entry : weights.entrySet()) {
final String fieldName = entry.getKey();
final BytesRef term = getAnalyzer().normalize(fieldName, text);
Query q = new PrefixQuery(new Term(fieldName, term));
float boost = entry.getValue();
if (boost != 1f) {
q = new BoostQuery(q, boost);
}
bq.add(q, BooleanClause.Occur.SHOULD);
}
return simplify(bq.build());
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class FuzzyLikeThisQuery method rewrite.
@Override
public Query rewrite(IndexReader reader) throws IOException {
ScoreTermQueue q = new ScoreTermQueue(maxNumTerms);
//load up the list of possible terms
for (FieldVals f : fieldVals) {
addTerms(reader, f, q);
}
BooleanQuery.Builder bq = new BooleanQuery.Builder();
//create BooleanQueries to hold the variants for each token/field pair and ensure it
// has no coord factor
//Step 1: sort the termqueries by term/field
HashMap<Term, ArrayList<ScoreTerm>> variantQueries = new HashMap<>();
int size = q.size();
for (int i = 0; i < size; i++) {
ScoreTerm st = q.pop();
ArrayList<ScoreTerm> l = variantQueries.get(st.fuzziedSourceTerm);
if (l == null) {
l = new ArrayList<>();
variantQueries.put(st.fuzziedSourceTerm, l);
}
l.add(st);
}
//Step 2: Organize the sorted termqueries into zero-coord scoring boolean queries
for (Iterator<ArrayList<ScoreTerm>> iter = variantQueries.values().iterator(); iter.hasNext(); ) {
ArrayList<ScoreTerm> variants = iter.next();
if (variants.size() == 1) {
//optimize where only one selected variant
ScoreTerm st = variants.get(0);
Query tq = newTermQuery(reader, st.term);
// set the boost to a mix of IDF and score
bq.add(new BoostQuery(tq, st.score), BooleanClause.Occur.SHOULD);
} else {
BooleanQuery.Builder termVariants = new BooleanQuery.Builder();
for (Iterator<ScoreTerm> iterator2 = variants.iterator(); iterator2.hasNext(); ) {
ScoreTerm st = iterator2.next();
// found a match
Query tq = newTermQuery(reader, st.term);
// set the boost using the ScoreTerm's score
// add to query
termVariants.add(new BoostQuery(tq, st.score), BooleanClause.Occur.SHOULD);
}
// add to query
bq.add(termVariants.build(), BooleanClause.Occur.SHOULD);
}
}
// booleans with a minimum-should-match of NumFields-1?
return bq.build();
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class FilterQuery method createWeight.
@Override
public Weight createWeight(IndexSearcher searcher, boolean needScores, float boost) throws IOException {
if (!(searcher instanceof SolrIndexSearcher)) {
// delete-by-query won't have SolrIndexSearcher
return new BoostQuery(new ConstantScoreQuery(q), 0).createWeight(searcher, needScores, 1f);
}
SolrIndexSearcher solrSearcher = (SolrIndexSearcher) searcher;
DocSet docs = solrSearcher.getDocSet(q);
return new BoostQuery(new SolrConstantScoreQuery(docs.getTopFilter()), 0).createWeight(searcher, needScores, 1f);
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class SolrQueryParserBase method handleBoost.
// Called from parser
// Raw queries are transformed to normal queries before wrapping in a BoostQuery
Query handleBoost(Query q, Token boost) {
// q==null check is to avoid boosting null queries, such as those caused by stop words
if (boost == null || boost.image.length() == 0 || q == null) {
return q;
}
if (boost.image.charAt(0) == '=') {
// syntax looks like foo:x^=3
float val = Float.parseFloat(boost.image.substring(1));
Query newQ = q;
if (q instanceof ConstantScoreQuery || q instanceof SolrConstantScoreQuery) {
// skip
} else {
newQ = new ConstantScoreQuery(rawToNormal(q));
}
return new BoostQuery(newQ, val);
}
float boostVal = Float.parseFloat(boost.image);
return new BoostQuery(rawToNormal(q), boostVal);
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class TestBooleanSimilarity method testPhraseScoreIsEqualToBoost.
public void testPhraseScoreIsEqualToBoost() throws IOException {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig().setSimilarity(new BooleanSimilarity()));
Document doc = new Document();
doc.add(new TextField("foo", "bar baz quux", Store.NO));
w.addDocument(doc);
DirectoryReader reader = w.getReader();
w.close();
IndexSearcher searcher = newSearcher(reader);
searcher.setSimilarity(new BooleanSimilarity());
PhraseQuery query = new PhraseQuery(2, "foo", "bar", "quux");
TopDocs topDocs = searcher.search(query, 2);
assertEquals(1, topDocs.totalHits);
assertEquals(1f, topDocs.scoreDocs[0].score, 0f);
topDocs = searcher.search(new BoostQuery(query, 7), 2);
assertEquals(1, topDocs.totalHits);
assertEquals(7f, topDocs.scoreDocs[0].score, 0f);
reader.close();
dir.close();
}
Aggregations