Search in sources :

Example 21 with DocList

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

the class SolrPluginUtils method doStandardResultsDebug.

public static void doStandardResultsDebug(SolrQueryRequest req, Query query, DocList results, boolean dbgResults, NamedList dbg) throws IOException {
    if (dbgResults) {
        SolrIndexSearcher searcher = req.getSearcher();
        IndexSchema schema = searcher.getSchema();
        boolean explainStruct = req.getParams().getBool(CommonParams.EXPLAIN_STRUCT, false);
        if (results != null) {
            NamedList<Explanation> explain = getExplanations(query, results, searcher, schema);
            dbg.add("explain", explainStruct ? explanationsToNamedLists(explain) : explanationsToStrings(explain));
        }
        String otherQueryS = req.getParams().get(CommonParams.EXPLAIN_OTHER);
        if (otherQueryS != null && otherQueryS.length() > 0) {
            DocList otherResults = doSimpleQuery(otherQueryS, req, 0, 10);
            dbg.add("otherQuery", otherQueryS);
            NamedList<Explanation> explainO = getExplanations(query, otherResults, searcher, schema);
            dbg.add("explainOther", explainStruct ? explanationsToNamedLists(explainO) : explanationsToStrings(explainO));
        }
    }
}
Also used : Explanation(org.apache.lucene.search.Explanation) IndexSchema(org.apache.solr.schema.IndexSchema) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) DocList(org.apache.solr.search.DocList)

Example 22 with DocList

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

the class SolrPluginUtils method doSimpleQuery.

/**
   * Executes a basic query
   */
public static DocList doSimpleQuery(String sreq, SolrQueryRequest req, int start, int limit) throws IOException {
    List<String> commands = StrUtils.splitSmart(sreq, ';');
    String qs = commands.size() >= 1 ? commands.get(0) : "";
    try {
        Query query = QParser.getParser(qs, req).getQuery();
        // If the first non-query, non-filter command is a simple sort on an indexed field, then
        // we can use the Lucene sort ability.
        Sort sort = null;
        if (commands.size() >= 2) {
            sort = SortSpecParsing.parseSortSpec(commands.get(1), req).getSort();
        }
        DocList results = req.getSearcher().getDocList(query, (DocSet) null, sort, start, limit);
        return results;
    } catch (SyntaxError e) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing query: " + qs);
    }
}
Also used : Query(org.apache.lucene.search.Query) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) SyntaxError(org.apache.solr.search.SyntaxError) Sort(org.apache.lucene.search.Sort) DocList(org.apache.solr.search.DocList) SolrException(org.apache.solr.common.SolrException)

Example 23 with DocList

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

the class QueryComponent method doFieldSortValues.

protected void doFieldSortValues(ResponseBuilder rb, SolrIndexSearcher searcher) throws IOException {
    SolrQueryRequest req = rb.req;
    SolrQueryResponse rsp = rb.rsp;
    // The query cache doesn't currently store sort field values, and SolrIndexSearcher doesn't
    // currently have an option to return sort field values.  Because of this, we
    // take the documents given and re-derive the sort values.
    //
    // TODO: See SOLR-5595
    boolean fsv = req.getParams().getBool(ResponseBuilder.FIELD_SORT_VALUES, false);
    if (fsv) {
        // order is important for the sort fields
        NamedList<Object[]> sortVals = new NamedList<>();
        IndexReaderContext topReaderContext = searcher.getTopReaderContext();
        List<LeafReaderContext> leaves = topReaderContext.leaves();
        LeafReaderContext currentLeaf = null;
        if (leaves.size() == 1) {
            // if there is a single segment, use that subReader and avoid looking up each time
            currentLeaf = leaves.get(0);
            leaves = null;
        }
        DocList docList = rb.getResults().docList;
        // sort ids from lowest to highest so we can access them in order
        int nDocs = docList.size();
        final long[] sortedIds = new long[nDocs];
        // doc scores, parallel to sortedIds
        final float[] scores = new float[nDocs];
        DocList docs = rb.getResults().docList;
        DocIterator it = docs.iterator();
        for (int i = 0; i < nDocs; i++) {
            sortedIds[i] = (((long) it.nextDoc()) << 32) | i;
            scores[i] = docs.hasScores() ? it.score() : Float.NaN;
        }
        // sort ids and scores together
        new InPlaceMergeSorter() {

            @Override
            protected void swap(int i, int j) {
                long tmpId = sortedIds[i];
                float tmpScore = scores[i];
                sortedIds[i] = sortedIds[j];
                scores[i] = scores[j];
                sortedIds[j] = tmpId;
                scores[j] = tmpScore;
            }

            @Override
            protected int compare(int i, int j) {
                return Long.compare(sortedIds[i], sortedIds[j]);
            }
        }.sort(0, sortedIds.length);
        SortSpec sortSpec = rb.getSortSpec();
        Sort sort = searcher.weightSort(sortSpec.getSort());
        SortField[] sortFields = sort == null ? new SortField[] { SortField.FIELD_SCORE } : sort.getSort();
        List<SchemaField> schemaFields = sortSpec.getSchemaFields();
        for (int fld = 0; fld < schemaFields.size(); fld++) {
            SchemaField schemaField = schemaFields.get(fld);
            FieldType ft = null == schemaField ? null : schemaField.getType();
            SortField sortField = sortFields[fld];
            SortField.Type type = sortField.getType();
            // :TODO: would be simpler to always serialize every position of SortField[]
            if (type == SortField.Type.SCORE || type == SortField.Type.DOC)
                continue;
            FieldComparator<?> comparator = sortField.getComparator(1, 0);
            LeafFieldComparator leafComparator = null;
            Object[] vals = new Object[nDocs];
            int lastIdx = -1;
            int idx = 0;
            for (int i = 0; i < sortedIds.length; ++i) {
                long idAndPos = sortedIds[i];
                float score = scores[i];
                int doc = (int) (idAndPos >>> 32);
                int position = (int) idAndPos;
                if (leaves != null) {
                    idx = ReaderUtil.subIndex(doc, leaves);
                    currentLeaf = leaves.get(idx);
                    if (idx != lastIdx) {
                        // we switched segments.  invalidate leafComparator.
                        lastIdx = idx;
                        leafComparator = null;
                    }
                }
                if (leafComparator == null) {
                    leafComparator = comparator.getLeafComparator(currentLeaf);
                }
                // adjust for what segment this is in
                doc -= currentLeaf.docBase;
                leafComparator.setScorer(new FakeScorer(doc, score));
                leafComparator.copy(0, doc);
                Object val = comparator.value(0);
                if (null != ft)
                    val = ft.marshalSortValue(val);
                vals[position] = val;
            }
            sortVals.add(sortField.getField(), vals);
        }
        rsp.add("sort_values", sortVals);
    }
}
Also used : DocIterator(org.apache.solr.search.DocIterator) SortField(org.apache.lucene.search.SortField) LeafFieldComparator(org.apache.lucene.search.LeafFieldComparator) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Sort(org.apache.lucene.search.Sort) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) NamedList(org.apache.solr.common.util.NamedList) InPlaceMergeSorter(org.apache.lucene.util.InPlaceMergeSorter) IndexReaderContext(org.apache.lucene.index.IndexReaderContext) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) DocList(org.apache.solr.search.DocList) SortSpec(org.apache.solr.search.SortSpec)

Aggregations

DocList (org.apache.solr.search.DocList)23 SolrParams (org.apache.solr.common.params.SolrParams)11 NamedList (org.apache.solr.common.util.NamedList)11 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)11 SolrDocument (org.apache.solr.common.SolrDocument)9 ArrayList (java.util.ArrayList)8 Query (org.apache.lucene.search.Query)8 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)8 SchemaField (org.apache.solr.schema.SchemaField)8 DocIterator (org.apache.solr.search.DocIterator)8 SolrDocumentList (org.apache.solr.common.SolrDocumentList)6 ResultContext (org.apache.solr.response.ResultContext)6 FieldType (org.apache.solr.schema.FieldType)6 Document (org.apache.lucene.document.Document)5 Sort (org.apache.lucene.search.Sort)5 SolrException (org.apache.solr.common.SolrException)5 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)5 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4