Search in sources :

Example 76 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class TestScoreJoinQPScore method testBoost.

@Ignore("SOLR-7814, also don't forget cover boost at testCacheHit()")
public void testBoost() throws Exception {
    indexDataForScorring();
    ScoreMode score = ScoreMode.values()[random().nextInt(ScoreMode.values().length)];
    final SolrQueryRequest req = req("q", "{!join from=movieId_s to=id score=" + score + " b=200}title:movie", "fl", "id,score", "omitHeader", "true");
    SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, new SolrQueryResponse()));
    final Query luceneQ = QParser.getParser(req.getParams().get("q"), req).getQuery().rewrite(req.getSearcher().getSlowAtomicReader());
    assertTrue(luceneQ instanceof BoostQuery);
    float boost = ((BoostQuery) luceneQ).getBoost();
    assertEquals("" + luceneQ, Float.floatToIntBits(200), Float.floatToIntBits(boost));
    SolrRequestInfo.clearRequestInfo();
    req.close();
}
Also used : ScoreMode(org.apache.lucene.search.join.ScoreMode) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) Query(org.apache.lucene.search.Query) BoostQuery(org.apache.lucene.search.BoostQuery) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) BoostQuery(org.apache.lucene.search.BoostQuery) Ignore(org.junit.Ignore)

Example 77 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class TestBooleanSimilarity method testTermScoreIsEqualToBoost.

public void testTermScoreIsEqualToBoost() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig());
    Document doc = new Document();
    doc.add(new StringField("foo", "bar", Store.NO));
    doc.add(new StringField("foo", "baz", Store.NO));
    w.addDocument(doc);
    doc = new Document();
    doc.add(new StringField("foo", "bar", Store.NO));
    doc.add(new StringField("foo", "bar", Store.NO));
    w.addDocument(doc);
    DirectoryReader reader = w.getReader();
    w.close();
    IndexSearcher searcher = newSearcher(reader);
    searcher.setSimilarity(new BooleanSimilarity());
    TopDocs topDocs = searcher.search(new TermQuery(new Term("foo", "bar")), 2);
    assertEquals(2, topDocs.totalHits);
    assertEquals(1f, topDocs.scoreDocs[0].score, 0f);
    assertEquals(1f, topDocs.scoreDocs[1].score, 0f);
    topDocs = searcher.search(new TermQuery(new Term("foo", "baz")), 1);
    assertEquals(1, topDocs.totalHits);
    assertEquals(1f, topDocs.scoreDocs[0].score, 0f);
    topDocs = searcher.search(new BoostQuery(new TermQuery(new Term("foo", "baz")), 3f), 1);
    assertEquals(1, topDocs.totalHits);
    assertEquals(3f, topDocs.scoreDocs[0].score, 0f);
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) DirectoryReader(org.apache.lucene.index.DirectoryReader) StringField(org.apache.lucene.document.StringField) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) BoostQuery(org.apache.lucene.search.BoostQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 78 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class TestTaxonomyFacetSumValueSource method testSumScoreAggregator.

public void testSumScoreAggregator() throws Exception {
    Directory indexDir = newDirectory();
    Directory taxoDir = newDirectory();
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
    IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
    FacetsConfig config = new FacetsConfig();
    for (int i = atLeast(30); i > 0; --i) {
        Document doc = new Document();
        if (random().nextBoolean()) {
            // don't match all documents
            doc.add(new StringField("f", "v", Field.Store.NO));
        }
        doc.add(new FacetField("dim", "a"));
        iw.addDocument(config.build(taxoWriter, doc));
    }
    DirectoryReader r = DirectoryReader.open(iw);
    DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
    FacetsCollector fc = new FacetsCollector(true);
    BoostQuery csq = new BoostQuery(new ConstantScoreQuery(new MatchAllDocsQuery()), 2f);
    TopDocs td = FacetsCollector.search(newSearcher(r), csq, 10, fc);
    Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, DoubleValuesSource.SCORES);
    int expected = (int) (td.getMaxScore() * td.totalHits);
    assertEquals(expected, facets.getSpecificValue("dim", "a").intValue());
    iw.close();
    IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
Also used : FacetsConfig(org.apache.lucene.facet.FacetsConfig) Facets(org.apache.lucene.facet.Facets) DirectoryReader(org.apache.lucene.index.DirectoryReader) FacetField(org.apache.lucene.facet.FacetField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BoostQuery(org.apache.lucene.search.BoostQuery) FacetsCollector(org.apache.lucene.facet.FacetsCollector) DirectoryTaxonomyWriter(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyWriter) TopDocs(org.apache.lucene.search.TopDocs) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) StringField(org.apache.lucene.document.StringField) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Directory(org.apache.lucene.store.Directory) DirectoryTaxonomyReader(org.apache.lucene.facet.taxonomy.directory.DirectoryTaxonomyReader)

Example 79 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class BoostQueryNodeBuilder method build.

@Override
public Query build(QueryNode queryNode) throws QueryNodeException {
    BoostQueryNode boostNode = (BoostQueryNode) queryNode;
    QueryNode child = boostNode.getChild();
    if (child == null) {
        return null;
    }
    Query query = (Query) child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
    return new BoostQuery(query, boostNode.getValue());
}
Also used : BoostQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BoostQueryNode) Query(org.apache.lucene.search.Query) BoostQuery(org.apache.lucene.search.BoostQuery) BoostQueryNode(org.apache.lucene.queryparser.flexible.core.nodes.BoostQueryNode) QueryNode(org.apache.lucene.queryparser.flexible.core.nodes.QueryNode) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 80 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class MultiFieldQueryParser method getFieldQuery.

@Override
protected Query getFieldQuery(String field, String queryText, boolean quoted) throws ParseException {
    if (field == null) {
        List<Query> clauses = new ArrayList<>();
        Query[] fieldQueries = new Query[fields.length];
        int maxTerms = 0;
        for (int i = 0; i < fields.length; i++) {
            Query q = super.getFieldQuery(fields[i], queryText, quoted);
            if (q != null) {
                if (q instanceof BooleanQuery) {
                    maxTerms = Math.max(maxTerms, ((BooleanQuery) q).clauses().size());
                } else {
                    maxTerms = Math.max(1, maxTerms);
                }
                fieldQueries[i] = q;
            }
        }
        for (int termNum = 0; termNum < maxTerms; termNum++) {
            List<Query> termClauses = new ArrayList<>();
            for (int i = 0; i < fields.length; i++) {
                if (fieldQueries[i] != null) {
                    Query q = null;
                    if (fieldQueries[i] instanceof BooleanQuery) {
                        List<BooleanClause> nestedClauses = ((BooleanQuery) fieldQueries[i]).clauses();
                        if (termNum < nestedClauses.size()) {
                            q = nestedClauses.get(termNum).getQuery();
                        }
                    } else if (termNum == 0) {
                        // e.g. TermQuery-s
                        q = fieldQueries[i];
                    }
                    if (q != null) {
                        if (boosts != null) {
                            //Get the boost from the map and apply them
                            Float boost = boosts.get(fields[i]);
                            if (boost != null) {
                                q = new BoostQuery(q, boost);
                            }
                        }
                        termClauses.add(q);
                    }
                }
            }
            if (maxTerms > 1) {
                if (termClauses.size() > 0) {
                    BooleanQuery.Builder builder = newBooleanQuery();
                    for (Query termClause : termClauses) {
                        builder.add(termClause, BooleanClause.Occur.SHOULD);
                    }
                    clauses.add(builder.build());
                }
            } else {
                clauses.addAll(termClauses);
            }
        }
        if (// happens for stopwords
        clauses.size() == 0)
            return null;
        return getMultiFieldQuery(clauses);
    }
    Query q = super.getFieldQuery(field, queryText, quoted);
    return q;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) ArrayList(java.util.ArrayList) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

BoostQuery (org.apache.lucene.search.BoostQuery)128 Query (org.apache.lucene.search.Query)107 BooleanQuery (org.apache.lucene.search.BooleanQuery)96 TermQuery (org.apache.lucene.search.TermQuery)84 Term (org.apache.lucene.index.Term)54 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)45 PhraseQuery (org.apache.lucene.search.PhraseQuery)35 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)32 PrefixQuery (org.apache.lucene.search.PrefixQuery)29 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)27 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)24 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)23 BooleanClause (org.apache.lucene.search.BooleanClause)20 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)20 SynonymQuery (org.apache.lucene.search.SynonymQuery)19 WildcardQuery (org.apache.lucene.search.WildcardQuery)19 ArrayList (java.util.ArrayList)18 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)18 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)17 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)16