Search in sources :

Example 36 with SortField

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

the class SortSpecParsing method parseSortSpecImpl.

private static SortSpec parseSortSpecImpl(String sortSpec, IndexSchema schema, SolrQueryRequest optionalReq) {
    if (sortSpec == null || sortSpec.length() == 0)
        return newEmptySortSpec();
    List<SortField> sorts = new ArrayList<>(4);
    List<SchemaField> fields = new ArrayList<>(4);
    try {
        StrParser sp = new StrParser(sortSpec);
        while (sp.pos < sp.end) {
            sp.eatws();
            final int start = sp.pos;
            // short circuit test for a really simple field name
            String field = sp.getId(null);
            Exception qParserException = null;
            if ((field == null || !Character.isWhitespace(sp.peekChar())) && (optionalReq != null)) {
                // let's try it as a function instead
                field = null;
                String funcStr = sp.val.substring(start);
                QParser parser = QParser.getParser(funcStr, FunctionQParserPlugin.NAME, optionalReq);
                Query q = null;
                try {
                    if (parser instanceof FunctionQParser) {
                        FunctionQParser fparser = (FunctionQParser) parser;
                        fparser.setParseMultipleSources(false);
                        fparser.setParseToEnd(false);
                        q = fparser.getQuery();
                        if (fparser.localParams != null) {
                            if (fparser.valFollowedParams) {
                                // need to find the end of the function query via the string parser
                                int leftOver = fparser.sp.end - fparser.sp.pos;
                                // reset our parser to the same amount of leftover
                                sp.pos = sp.end - leftOver;
                            } else {
                                // the value was via the "v" param in localParams, so we need to find
                                // the end of the local params themselves to pick up where we left off
                                sp.pos = start + fparser.localParamsEnd;
                            }
                        } else {
                            // need to find the end of the function query via the string parser
                            int leftOver = fparser.sp.end - fparser.sp.pos;
                            // reset our parser to the same amount of leftover
                            sp.pos = sp.end - leftOver;
                        }
                    } else {
                        // A QParser that's not for function queries.
                        // It must have been specified via local params.
                        q = parser.getQuery();
                        assert parser.getLocalParams() != null;
                        sp.pos = start + parser.localParamsEnd;
                    }
                    Boolean top = sp.getSortDirection();
                    if (null != top) {
                        // we have a Query and a valid direction
                        if (q instanceof FunctionQuery) {
                            sorts.add(((FunctionQuery) q).getValueSource().getSortField(top));
                        } else {
                            sorts.add((new QueryValueSource(q, 0.0f)).getSortField(top));
                        }
                        fields.add(null);
                        continue;
                    }
                } catch (Exception e) {
                    // hang onto this in case the string isn't a full field name either
                    qParserException = e;
                }
            }
            if (field == null) {
                // try again, simple rules for a field name with no whitespace
                sp.pos = start;
                field = sp.getSimpleString();
            }
            Boolean top = sp.getSortDirection();
            if (null == top) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Can't determine a Sort Order (asc or desc) in sort spec " + sp);
            }
            if (SCORE.equals(field)) {
                if (top) {
                    sorts.add(SortField.FIELD_SCORE);
                } else {
                    sorts.add(new SortField(null, SortField.Type.SCORE, true));
                }
                fields.add(null);
            } else if (DOCID.equals(field)) {
                sorts.add(new SortField(null, SortField.Type.DOC, top));
                fields.add(null);
            } else {
                // try to find the field
                SchemaField sf = schema.getFieldOrNull(field);
                if (null == sf) {
                    if (null != qParserException) {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "sort param could not be parsed as a query, and is not a " + "field that exists in the index: " + field, qParserException);
                    }
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "sort param field can't be found: " + field);
                }
                sorts.add(sf.getSortField(top));
                fields.add(sf);
            }
        }
    } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "error in sort: " + sortSpec, e);
    }
    // normalize a sort on score desc to null
    if (sorts.size() == 1 && sorts.get(0) == SortField.FIELD_SCORE) {
        return newEmptySortSpec();
    }
    Sort s = new Sort(sorts.toArray(new SortField[sorts.size()]));
    return new SortSpec(s, fields);
}
Also used : Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ArrayList(java.util.ArrayList) SortField(org.apache.lucene.search.SortField) SolrException(org.apache.solr.common.SolrException) SchemaField(org.apache.solr.schema.SchemaField) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Sort(org.apache.lucene.search.Sort) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) SolrException(org.apache.solr.common.SolrException)

Example 37 with SortField

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

the class QueryResultKey method equals.

@Override
public boolean equals(Object o) {
    if (o == this)
        return true;
    if (!(o instanceof QueryResultKey))
        return false;
    QueryResultKey other = (QueryResultKey) o;
    // that the full cached hash code will be different.
    if (this.hc != other.hc)
        return false;
    // first.
    if (this.sfields.length != other.sfields.length)
        return false;
    if (!this.query.equals(other.query))
        return false;
    if (!unorderedCompare(this.filters, other.filters))
        return false;
    for (int i = 0; i < sfields.length; i++) {
        SortField sf1 = this.sfields[i];
        SortField sf2 = other.sfields[i];
        if (!sf1.equals(sf2))
            return false;
    }
    return true;
}
Also used : SortField(org.apache.lucene.search.SortField)

Example 38 with SortField

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

the class DistanceFacetsExample method getDistanceValueSource.

private DoubleValuesSource getDistanceValueSource() {
    Expression distance;
    try {
        distance = JavascriptCompiler.compile("haversin(" + ORIGIN_LATITUDE + "," + ORIGIN_LONGITUDE + ",latitude,longitude)");
    } catch (ParseException pe) {
        // Should not happen
        throw new RuntimeException(pe);
    }
    SimpleBindings bindings = new SimpleBindings();
    bindings.add(new SortField("latitude", SortField.Type.DOUBLE));
    bindings.add(new SortField("longitude", SortField.Type.DOUBLE));
    return distance.getDoubleValuesSource(bindings);
}
Also used : Expression(org.apache.lucene.expressions.Expression) SimpleBindings(org.apache.lucene.expressions.SimpleBindings) SortField(org.apache.lucene.search.SortField) ParseException(java.text.ParseException)

Example 39 with SortField

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

the class TestDemoExpressions method testDollarVariable.

/** Uses variables with $ */
public void testDollarVariable() throws Exception {
    Expression expr = JavascriptCompiler.compile("$0+$score");
    SimpleBindings bindings = new SimpleBindings();
    bindings.add(new SortField("$0", SortField.Type.SCORE));
    bindings.add(new SortField("$score", SortField.Type.SCORE));
    Sort sort = new Sort(expr.getSortField(bindings, true));
    Query query = new TermQuery(new Term("body", "contents"));
    TopFieldDocs td = searcher.search(query, 3, sort, true, true);
    for (int i = 0; i < 3; i++) {
        FieldDoc d = (FieldDoc) td.scoreDocs[i];
        float expected = 2 * d.score;
        float actual = ((Double) d.fields[0]).floatValue();
        assertEquals(expected, actual, CheckHits.explainToleranceDelta(expected, actual));
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) FieldDoc(org.apache.lucene.search.FieldDoc) Sort(org.apache.lucene.search.Sort) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term)

Example 40 with SortField

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

the class TestDemoExpressions method testDistanceSort.

public void testDistanceSort() throws Exception {
    Expression distance = JavascriptCompiler.compile("haversin(40.7143528,-74.0059731,latitude,longitude)");
    SimpleBindings bindings = new SimpleBindings();
    bindings.add(new SortField("latitude", SortField.Type.DOUBLE));
    bindings.add(new SortField("longitude", SortField.Type.DOUBLE));
    Sort sort = new Sort(distance.getSortField(bindings, false));
    TopFieldDocs td = searcher.search(new MatchAllDocsQuery(), 3, sort);
    FieldDoc d = (FieldDoc) td.scoreDocs[0];
    assertEquals(0.4621D, (Double) d.fields[0], 1E-4);
    d = (FieldDoc) td.scoreDocs[1];
    assertEquals(1.055D, (Double) d.fields[0], 1E-4);
    d = (FieldDoc) td.scoreDocs[2];
    assertEquals(5.2859D, (Double) d.fields[0], 1E-4);
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) Sort(org.apache.lucene.search.Sort) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Aggregations

SortField (org.apache.lucene.search.SortField)231 Sort (org.apache.lucene.search.Sort)175 Document (org.apache.lucene.document.Document)116 Directory (org.apache.lucene.store.Directory)110 IndexSearcher (org.apache.lucene.search.IndexSearcher)90 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)75 TopDocs (org.apache.lucene.search.TopDocs)74 IndexReader (org.apache.lucene.index.IndexReader)65 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)62 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)56 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)56 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)49 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)37 TermQuery (org.apache.lucene.search.TermQuery)36 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)32 Query (org.apache.lucene.search.Query)29 Term (org.apache.lucene.index.Term)25 BytesRef (org.apache.lucene.util.BytesRef)25 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)24 StoredField (org.apache.lucene.document.StoredField)23