Search in sources :

Example 21 with BoostQuery

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());
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) QueryBuilder(org.apache.lucene.util.QueryBuilder) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef)

Example 22 with BoostQuery

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();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 23 with BoostQuery

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);
}
Also used : SolrConstantScoreQuery(org.apache.solr.search.SolrConstantScoreQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrConstantScoreQuery(org.apache.solr.search.SolrConstantScoreQuery) BoostQuery(org.apache.lucene.search.BoostQuery) DocSet(org.apache.solr.search.DocSet)

Example 24 with BoostQuery

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);
}
Also used : Query(org.apache.lucene.search.Query) AutomatonQuery(org.apache.lucene.search.AutomatonQuery) SolrConstantScoreQuery(org.apache.solr.search.SolrConstantScoreQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) FilterQuery(org.apache.solr.query.FilterQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SolrConstantScoreQuery(org.apache.solr.search.SolrConstantScoreQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SolrConstantScoreQuery(org.apache.solr.search.SolrConstantScoreQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 25 with BoostQuery

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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) PhraseQuery(org.apache.lucene.search.PhraseQuery) DirectoryReader(org.apache.lucene.index.DirectoryReader) TextField(org.apache.lucene.document.TextField) Document(org.apache.lucene.document.Document) BoostQuery(org.apache.lucene.search.BoostQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Aggregations

BoostQuery (org.apache.lucene.search.BoostQuery)109 Query (org.apache.lucene.search.Query)91 BooleanQuery (org.apache.lucene.search.BooleanQuery)80 TermQuery (org.apache.lucene.search.TermQuery)71 Term (org.apache.lucene.index.Term)48 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)43 PhraseQuery (org.apache.lucene.search.PhraseQuery)32 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)27 PrefixQuery (org.apache.lucene.search.PrefixQuery)27 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)25 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)22 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)21 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)20 SynonymQuery (org.apache.lucene.search.SynonymQuery)19 WildcardQuery (org.apache.lucene.search.WildcardQuery)19 BooleanClause (org.apache.lucene.search.BooleanClause)18 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)17 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)16 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)16 ArrayList (java.util.ArrayList)14