Search in sources :

Example 16 with ConstantScoreQuery

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

the class LatLonPoint method newBoxQuery.

// static methods for generating queries
/**
   * Create a query for matching a bounding box.
   * <p>
   * The box may cross over the dateline.
   * @param field field name. must not be null.
   * @param minLatitude latitude lower bound: must be within standard +/-90 coordinate bounds.
   * @param maxLatitude latitude upper bound: must be within standard +/-90 coordinate bounds.
   * @param minLongitude longitude lower bound: must be within standard +/-180 coordinate bounds.
   * @param maxLongitude longitude upper bound: must be within standard +/-180 coordinate bounds.
   * @return query matching points within this box
   * @throws IllegalArgumentException if {@code field} is null, or the box has invalid coordinates.
   */
public static Query newBoxQuery(String field, double minLatitude, double maxLatitude, double minLongitude, double maxLongitude) {
    // and should not drag in extra bogus junk! TODO: should encodeCeil just throw ArithmeticException to be less trappy here?
    if (minLatitude == 90.0) {
        // range cannot match as 90.0 can never exist
        return new MatchNoDocsQuery("LatLonPoint.newBoxQuery with minLatitude=90.0");
    }
    if (minLongitude == 180.0) {
        if (maxLongitude == 180.0) {
            // range cannot match as 180.0 can never exist
            return new MatchNoDocsQuery("LatLonPoint.newBoxQuery with minLongitude=maxLongitude=180.0");
        } else if (maxLongitude < minLongitude) {
            // encodeCeil() with dateline wrapping!
            minLongitude = -180.0;
        }
    }
    byte[] lower = encodeCeil(minLatitude, minLongitude);
    byte[] upper = encode(maxLatitude, maxLongitude);
    // Crosses date line: we just rewrite into OR of two bboxes, with longitude as an open range:
    if (maxLongitude < minLongitude) {
        // Disable coord here because a multi-valued doc could match both rects and get unfairly boosted:
        BooleanQuery.Builder q = new BooleanQuery.Builder();
        // E.g.: maxLon = -179, minLon = 179
        byte[] leftOpen = lower.clone();
        // leave longitude open
        NumericUtils.intToSortableBytes(Integer.MIN_VALUE, leftOpen, Integer.BYTES);
        Query left = newBoxInternal(field, leftOpen, upper);
        q.add(new BooleanClause(left, BooleanClause.Occur.SHOULD));
        byte[] rightOpen = upper.clone();
        // leave longitude open
        NumericUtils.intToSortableBytes(Integer.MAX_VALUE, rightOpen, Integer.BYTES);
        Query right = newBoxInternal(field, lower, rightOpen);
        q.add(new BooleanClause(right, BooleanClause.Occur.SHOULD));
        return new ConstantScoreQuery(q.build());
    } else {
        return newBoxInternal(field, lower, upper);
    }
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) PointRangeQuery(org.apache.lucene.search.PointRangeQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery)

Example 17 with ConstantScoreQuery

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

the class FuzzyLikeThisQuery method newTermQuery.

private Query newTermQuery(IndexReader reader, Term term) throws IOException {
    if (ignoreTF) {
        return new ConstantScoreQuery(new TermQuery(term));
    } else {
        // we build an artificial TermContext that will give an overall df and ttf
        // equal to 1
        TermContext context = new TermContext(reader.getContext());
        for (LeafReaderContext leafContext : reader.leaves()) {
            Terms terms = leafContext.reader().terms(term.field());
            if (terms != null) {
                TermsEnum termsEnum = terms.iterator();
                if (termsEnum.seekExact(term.bytes())) {
                    // we want the total df and ttf to be 1
                    int freq = 1 - context.docFreq();
                    context.register(termsEnum.termState(), leafContext.ord, freq, freq);
                }
            }
        }
        return new TermQuery(term, context);
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Terms(org.apache.lucene.index.Terms) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) TermContext(org.apache.lucene.index.TermContext) TermsEnum(org.apache.lucene.index.TermsEnum) FuzzyTermsEnum(org.apache.lucene.search.FuzzyTermsEnum)

Example 18 with ConstantScoreQuery

use of org.apache.lucene.search.ConstantScoreQuery 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 19 with ConstantScoreQuery

use of org.apache.lucene.search.ConstantScoreQuery 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 20 with ConstantScoreQuery

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

the class TestUnifiedHighlighterMTQ method testWildcardInConstantScore.

public void testWildcardInConstantScore() 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);
    ConstantScoreQuery query = new ConstantScoreQuery(new WildcardQuery(new Term("body", "te*")));
    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]);
    ir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) Field(org.apache.lucene.document.Field) WildcardQuery(org.apache.lucene.search.WildcardQuery) IndexReader(org.apache.lucene.index.IndexReader) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter)

Aggregations

ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)68 BooleanQuery (org.apache.lucene.search.BooleanQuery)43 TermQuery (org.apache.lucene.search.TermQuery)42 Query (org.apache.lucene.search.Query)40 Term (org.apache.lucene.index.Term)30 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)19 BoostQuery (org.apache.lucene.search.BoostQuery)14 IndexSearcher (org.apache.lucene.search.IndexSearcher)14 ArrayList (java.util.ArrayList)13 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)13 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)12 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)11 Document (org.apache.lucene.document.Document)10 Test (org.junit.Test)10 Sort (org.apache.lucene.search.Sort)9 TopDocs (org.apache.lucene.search.TopDocs)9 StringField (org.apache.lucene.document.StringField)8 BooleanClause (org.apache.lucene.search.BooleanClause)8 RegexpQuery (org.apache.lucene.search.RegexpQuery)8 SortField (org.apache.lucene.search.SortField)8