Search in sources :

Example 91 with BoostQuery

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

the class SimpleQueryParser method newFuzzyQuery.

/**
   * Factory method to generate a fuzzy query.
   */
protected Query newFuzzyQuery(String text, int fuzziness) {
    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 FuzzyQuery(new Term(fieldName, term), fuzziness);
        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) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef)

Example 92 with BoostQuery

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

the class SimpleQueryParser method newDefaultQuery.

/**
   * Factory method to generate a standard query (no phrase or prefix operators).
   */
protected Query newDefaultQuery(String text) {
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    for (Map.Entry<String, Float> entry : weights.entrySet()) {
        Query q = createBooleanQuery(entry.getKey(), text, defaultOperator);
        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());
}
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) Map(java.util.Map) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 93 with BoostQuery

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

the class TestUnifiedHighlighterMTQ method testOnePrefix.

public void testOnePrefix() throws Exception {
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);
    Field body = new Field("body", "", fieldType);
    Document doc = new Document();
    doc.add(body);
    body.setStringValue("This is a test.");
    iw.addDocument(doc);
    body.setStringValue("Test a one sentence document.");
    iw.addDocument(doc);
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher searcher = newSearcher(ir);
    UnifiedHighlighter highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);
    // wrap in a BoostQuery to also show we see inside it
    Query query = new BoostQuery(new PrefixQuery(new Term("body", "te")), 2.0f);
    TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    String[] snippets = highlighter.highlight("body", query, topDocs);
    assertEquals(2, snippets.length);
    assertEquals("This is a <b>test</b>.", snippets[0]);
    assertEquals("<b>Test</b> a one sentence document.", snippets[1]);
    // wrong field
    BooleanQuery bq = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), BooleanClause.Occur.SHOULD).add(new PrefixQuery(new Term("bogus", "te")), BooleanClause.Occur.SHOULD).build();
    topDocs = searcher.search(bq, 10, Sort.INDEXORDER);
    assertEquals(2, topDocs.totalHits);
    snippets = highlighter.highlight("body", bq, topDocs);
    assertEquals(2, snippets.length);
    assertEquals("This is a test.", snippets[0]);
    assertEquals("Test a one sentence document.", snippets[1]);
    ir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) PhraseQuery(org.apache.lucene.search.PhraseQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) SpanFirstQuery(org.apache.lucene.search.spans.SpanFirstQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) SpanNotQuery(org.apache.lucene.search.spans.SpanNotQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SpanBoostQuery(org.apache.lucene.search.spans.SpanBoostQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TopDocs(org.apache.lucene.search.TopDocs) Field(org.apache.lucene.document.Field) PrefixQuery(org.apache.lucene.search.PrefixQuery) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Example 94 with BoostQuery

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

the class QueryElevationComponent method prepare.

//---------------------------------------------------------------------------------
// SearchComponent
//---------------------------------------------------------------------------------
@Override
public void prepare(ResponseBuilder rb) throws IOException {
    SolrQueryRequest req = rb.req;
    SolrParams params = req.getParams();
    // A runtime param can skip 
    if (!params.getBool(QueryElevationParams.ENABLE, true)) {
        return;
    }
    boolean exclusive = params.getBool(QueryElevationParams.EXCLUSIVE, false);
    // A runtime parameter can alter the config value for forceElevation
    boolean force = params.getBool(QueryElevationParams.FORCE_ELEVATION, forceElevation);
    boolean markExcludes = params.getBool(QueryElevationParams.MARK_EXCLUDES, false);
    String boostStr = params.get(QueryElevationParams.IDS);
    String exStr = params.get(QueryElevationParams.EXCLUDE);
    Query query = rb.getQuery();
    SolrParams localParams = rb.getQparser().getLocalParams();
    String qstr = localParams == null ? rb.getQueryString() : localParams.get(QueryParsing.V);
    if (query == null || qstr == null) {
        return;
    }
    ElevationObj booster = null;
    try {
        if (boostStr != null || exStr != null) {
            List<String> boosts = (boostStr != null) ? StrUtils.splitSmart(boostStr, ",", true) : new ArrayList<String>(0);
            List<String> excludes = (exStr != null) ? StrUtils.splitSmart(exStr, ",", true) : new ArrayList<String>(0);
            booster = new ElevationObj(qstr, boosts, excludes);
        } else {
            IndexReader reader = req.getSearcher().getIndexReader();
            qstr = getAnalyzedQuery(qstr);
            booster = getElevationMap(reader, req.getCore()).get(qstr);
        }
    } catch (Exception ex) {
        throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Error loading elevation", ex);
    }
    if (booster != null) {
        rb.req.getContext().put(BOOSTED, booster.ids);
        rb.req.getContext().put(BOOSTED_PRIORITY, booster.priority);
        // Change the query to insert forced documents
        if (exclusive == true) {
            //we only want these results
            rb.setQuery(new BoostQuery(booster.include, 0f));
        } else {
            BooleanQuery.Builder newq = new BooleanQuery.Builder();
            newq.add(query, BooleanClause.Occur.SHOULD);
            newq.add(new BoostQuery(booster.include, 0f), BooleanClause.Occur.SHOULD);
            if (booster.exclude != null) {
                if (markExcludes == false) {
                    for (TermQuery tq : booster.exclude) {
                        newq.add(new BooleanClause(tq, BooleanClause.Occur.MUST_NOT));
                    }
                } else {
                    //we are only going to mark items as excluded, not actually exclude them.  This works
                    //with the EditorialMarkerFactory
                    rb.req.getContext().put(EXCLUDED, booster.excludeIds);
                }
            }
            rb.setQuery(newq.build());
        }
        ElevationComparatorSource comparator = new ElevationComparatorSource(booster);
        // if the sort is 'score desc' use a custom sorting method to 
        // insert documents in their proper place 
        SortSpec sortSpec = rb.getSortSpec();
        if (sortSpec.getSort() == null) {
            sortSpec.setSortAndFields(new Sort(new SortField[] { new SortField("_elevate_", comparator, true), new SortField(null, SortField.Type.SCORE, false) }), Arrays.asList(new SchemaField[2]));
        } else {
            // Check if the sort is based on score
            SortSpec modSortSpec = this.modifySortSpec(sortSpec, force, comparator);
            if (null != modSortSpec) {
                rb.setSortSpec(modSortSpec);
            }
        }
        // alter the sorting in the grouping specification if there is one
        GroupingSpecification groupingSpec = rb.getGroupingSpec();
        if (groupingSpec != null) {
            SortSpec groupSortSpec = groupingSpec.getGroupSortSpec();
            SortSpec modGroupSortSpec = this.modifySortSpec(groupSortSpec, force, comparator);
            if (modGroupSortSpec != null) {
                groupingSpec.setGroupSortSpec(modGroupSortSpec);
            }
            SortSpec withinGroupSortSpec = groupingSpec.getWithinGroupSortSpec();
            SortSpec modWithinGroupSortSpec = this.modifySortSpec(withinGroupSortSpec, force, comparator);
            if (modWithinGroupSortSpec != null) {
                groupingSpec.setWithinGroupSortSpec(modWithinGroupSortSpec);
            }
        }
    }
    // Add debugging information
    if (rb.isDebug()) {
        List<String> match = null;
        if (booster != null) {
            // Extract the elevated terms into a list
            match = new ArrayList<>(booster.priority.size());
            for (Object o : booster.include.clauses()) {
                TermQuery tq = (TermQuery) ((BooleanClause) o).getQuery();
                match.add(tq.getTerm().text());
            }
        }
        SimpleOrderedMap<Object> dbg = new SimpleOrderedMap<>();
        dbg.add("q", qstr);
        dbg.add("match", match);
        if (rb.isDebugQuery()) {
            rb.addDebugInfo("queryBoosting", dbg);
        }
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) SortField(org.apache.lucene.search.SortField) BoostQuery(org.apache.lucene.search.BoostQuery) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Sort(org.apache.lucene.search.Sort) GroupingSpecification(org.apache.solr.search.grouping.GroupingSpecification) SolrException(org.apache.solr.common.SolrException) TermQuery(org.apache.lucene.search.TermQuery) XPathExpressionException(javax.xml.xpath.XPathExpressionException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) BooleanClause(org.apache.lucene.search.BooleanClause) SchemaField(org.apache.solr.schema.SchemaField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) IndexReader(org.apache.lucene.index.IndexReader) SolrParams(org.apache.solr.common.params.SolrParams) SortSpec(org.apache.solr.search.SortSpec)

Example 95 with BoostQuery

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

the class TestMoreLikeThis method testBoostFactor.

public void testBoostFactor() throws Throwable {
    Map<String, Float> originalValues = getOriginalValues();
    MoreLikeThis mlt = new MoreLikeThis(reader);
    Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false);
    mlt.setAnalyzer(analyzer);
    mlt.setMinDocFreq(1);
    mlt.setMinTermFreq(1);
    mlt.setMinWordLen(1);
    mlt.setFieldNames(new String[] { "text" });
    mlt.setBoost(true);
    // this mean that every term boost factor will be multiplied by this
    // number
    float boostFactor = 5;
    mlt.setBoostFactor(boostFactor);
    BooleanQuery query = (BooleanQuery) mlt.like("text", new StringReader("lucene release"));
    Collection<BooleanClause> clauses = query.clauses();
    assertEquals("Expected " + originalValues.size() + " clauses.", originalValues.size(), clauses.size());
    for (BooleanClause clause : clauses) {
        BoostQuery bq = (BoostQuery) clause.getQuery();
        TermQuery tq = (TermQuery) bq.getQuery();
        Float termBoost = originalValues.get(tq.getTerm().text());
        assertNotNull("Expected term " + tq.getTerm().text(), termBoost);
        float totalBoost = termBoost * boostFactor;
        assertEquals("Expected boost of " + totalBoost + " for term '" + tq.getTerm().text() + "' got " + bq.getBoost(), totalBoost, bq.getBoost(), 0.0001);
    }
    analyzer.close();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) BoostQuery(org.apache.lucene.search.BoostQuery) BooleanClause(org.apache.lucene.search.BooleanClause) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) StringReader(java.io.StringReader)

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