Search in sources :

Example 6 with SortSpec

use of org.apache.solr.search.SortSpec 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 7 with SortSpec

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

the class MoreLikeThisHandler method handleRequestBody.

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    SolrParams params = req.getParams();
    long timeAllowed = (long) params.getInt(CommonParams.TIME_ALLOWED, -1);
    if (timeAllowed > 0) {
        SolrQueryTimeoutImpl.set(timeAllowed);
    }
    try {
        // Set field flags
        ReturnFields returnFields = new SolrReturnFields(req);
        rsp.setReturnFields(returnFields);
        int flags = 0;
        if (returnFields.wantsScore()) {
            flags |= SolrIndexSearcher.GET_SCORES;
        }
        String defType = params.get(QueryParsing.DEFTYPE, QParserPlugin.DEFAULT_QTYPE);
        String q = params.get(CommonParams.Q);
        Query query = null;
        SortSpec sortSpec = null;
        List<Query> filters = null;
        try {
            if (q != null) {
                QParser parser = QParser.getParser(q, defType, req);
                query = parser.getQuery();
                sortSpec = parser.getSortSpec(true);
            }
            String[] fqs = req.getParams().getParams(CommonParams.FQ);
            if (fqs != null && fqs.length != 0) {
                filters = new ArrayList<>();
                for (String fq : fqs) {
                    if (fq != null && fq.trim().length() != 0) {
                        QParser fqp = QParser.getParser(fq, req);
                        filters.add(fqp.getQuery());
                    }
                }
            }
        } catch (SyntaxError e) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
        }
        SolrIndexSearcher searcher = req.getSearcher();
        MoreLikeThisHelper mlt = new MoreLikeThisHelper(params, searcher);
        // Hold on to the interesting terms if relevant
        TermStyle termStyle = TermStyle.get(params.get(MoreLikeThisParams.INTERESTING_TERMS));
        List<InterestingTerm> interesting = (termStyle == TermStyle.NONE) ? null : new ArrayList<>(mlt.mlt.getMaxQueryTerms());
        DocListAndSet mltDocs = null;
        // Parse Required Params
        // This will either have a single Reader or valid query
        Reader reader = null;
        try {
            if (q == null || q.trim().length() < 1) {
                Iterable<ContentStream> streams = req.getContentStreams();
                if (streams != null) {
                    Iterator<ContentStream> iter = streams.iterator();
                    if (iter.hasNext()) {
                        reader = iter.next().getReader();
                    }
                    if (iter.hasNext()) {
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "MoreLikeThis does not support multiple ContentStreams");
                    }
                }
            }
            int start = params.getInt(CommonParams.START, CommonParams.START_DEFAULT);
            int rows = params.getInt(CommonParams.ROWS, CommonParams.ROWS_DEFAULT);
            // --------------------------------------------------------------------------------
            if (reader != null) {
                mltDocs = mlt.getMoreLikeThis(reader, start, rows, filters, interesting, flags);
            } else if (q != null) {
                // Matching options
                boolean includeMatch = params.getBool(MoreLikeThisParams.MATCH_INCLUDE, true);
                int matchOffset = params.getInt(MoreLikeThisParams.MATCH_OFFSET, 0);
                // Find the base match
                DocList match = searcher.getDocList(query, null, null, matchOffset, 1, // only get the first one...
                flags);
                if (includeMatch) {
                    rsp.add("match", match);
                }
                // This is an iterator, but we only handle the first match
                DocIterator iterator = match.iterator();
                if (iterator.hasNext()) {
                    // do a MoreLikeThis query for each document in results
                    int id = iterator.nextDoc();
                    mltDocs = mlt.getMoreLikeThis(id, start, rows, filters, interesting, flags);
                }
            } else {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "MoreLikeThis requires either a query (?q=) or text to find similar documents.");
            }
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
        if (mltDocs == null) {
            // avoid NPE
            mltDocs = new DocListAndSet();
        }
        rsp.addResponse(mltDocs.docList);
        if (interesting != null) {
            if (termStyle == TermStyle.DETAILS) {
                NamedList<Float> it = new NamedList<>();
                for (InterestingTerm t : interesting) {
                    it.add(t.term.toString(), t.boost);
                }
                rsp.add("interestingTerms", it);
            } else {
                List<String> it = new ArrayList<>(interesting.size());
                for (InterestingTerm t : interesting) {
                    it.add(t.term.text());
                }
                rsp.add("interestingTerms", it);
            }
        }
        // maybe facet the results
        if (params.getBool(FacetParams.FACET, false)) {
            if (mltDocs.docSet == null) {
                rsp.add("facet_counts", null);
            } else {
                SimpleFacets f = new SimpleFacets(req, mltDocs.docSet, params);
                rsp.add("facet_counts", FacetComponent.getFacetCounts(f));
            }
        }
        boolean dbg = req.getParams().getBool(CommonParams.DEBUG_QUERY, false);
        boolean dbgQuery = false, dbgResults = false;
        if (dbg == false) {
            //if it's true, we are doing everything anyway.
            String[] dbgParams = req.getParams().getParams(CommonParams.DEBUG);
            if (dbgParams != null) {
                for (String dbgParam : dbgParams) {
                    if (dbgParam.equals(CommonParams.QUERY)) {
                        dbgQuery = true;
                    } else if (dbgParam.equals(CommonParams.RESULTS)) {
                        dbgResults = true;
                    }
                }
            }
        } else {
            dbgQuery = true;
            dbgResults = true;
        }
        // Copied from StandardRequestHandler... perhaps it should be added to doStandardDebug?
        if (dbg == true) {
            try {
                NamedList<Object> dbgInfo = SolrPluginUtils.doStandardDebug(req, q, mlt.getRawMLTQuery(), mltDocs.docList, dbgQuery, dbgResults);
                if (null != dbgInfo) {
                    if (null != filters) {
                        dbgInfo.add("filter_queries", req.getParams().getParams(CommonParams.FQ));
                        List<String> fqs = new ArrayList<>(filters.size());
                        for (Query fq : filters) {
                            fqs.add(QueryParsing.toString(fq, req.getSchema()));
                        }
                        dbgInfo.add("parsed_filter_queries", fqs);
                    }
                    rsp.add("debug", dbgInfo);
                }
            } catch (Exception e) {
                SolrException.log(log, "Exception during debug", e);
                rsp.add("exception_during_debug", SolrException.toStr(e));
            }
        }
    } catch (ExitableDirectoryReader.ExitingReaderException ex) {
        log.warn("Query: " + req.getParamString() + "; " + ex.getMessage());
    } finally {
        SolrQueryTimeoutImpl.reset();
    }
}
Also used : DocIterator(org.apache.solr.search.DocIterator) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) DocListAndSet(org.apache.solr.search.DocListAndSet) ArrayList(java.util.ArrayList) ExitableDirectoryReader(org.apache.lucene.index.ExitableDirectoryReader) Reader(java.io.Reader) IndexReader(org.apache.lucene.index.IndexReader) ExitableDirectoryReader(org.apache.lucene.index.ExitableDirectoryReader) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ContentStream(org.apache.solr.common.util.ContentStream) SyntaxError(org.apache.solr.search.SyntaxError) TermStyle(org.apache.solr.common.params.MoreLikeThisParams.TermStyle) SolrException(org.apache.solr.common.SolrException) SolrReturnFields(org.apache.solr.search.SolrReturnFields) ReturnFields(org.apache.solr.search.ReturnFields) NamedList(org.apache.solr.common.util.NamedList) SimpleFacets(org.apache.solr.request.SimpleFacets) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) QParser(org.apache.solr.search.QParser) SolrParams(org.apache.solr.common.params.SolrParams) SortSpec(org.apache.solr.search.SortSpec) DocList(org.apache.solr.search.DocList)

Example 8 with SortSpec

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

the class ExportWriter method write.

public void write(OutputStream os) throws IOException {
    QueryResponseWriter rw = req.getCore().getResponseWriters().get(wt);
    if (rw instanceof BinaryResponseWriter) {
        //todo add support for other writers after testing
        writer = new JavaBinCodec(os, null);
    } else {
        respWriter = new OutputStreamWriter(os, StandardCharsets.UTF_8);
        writer = JSONResponseWriter.getPushWriter(respWriter, req, res);
    }
    Exception exception = res.getException();
    if (exception != null) {
        if (!(exception instanceof IgnoreException)) {
            writeException(exception, writer, false);
        }
        return;
    }
    SolrRequestInfo info = SolrRequestInfo.getRequestInfo();
    SortSpec sortSpec = info.getResponseBuilder().getSortSpec();
    if (sortSpec == null) {
        writeException((new IOException(new SyntaxError("No sort criteria was provided."))), writer, true);
        return;
    }
    SolrIndexSearcher searcher = req.getSearcher();
    Sort sort = searcher.weightSort(sortSpec.getSort());
    if (sort == null) {
        writeException((new IOException(new SyntaxError("No sort criteria was provided."))), writer, true);
        return;
    }
    if (sort != null && sort.needsScores()) {
        writeException((new IOException(new SyntaxError("Scoring is not currently supported with xsort."))), writer, true);
        return;
    }
    // This came to light in the very artifical case of indexing a single doc to Cloud.
    if (req.getContext().get("totalHits") != null) {
        totalHits = ((Integer) req.getContext().get("totalHits")).intValue();
        sets = (FixedBitSet[]) req.getContext().get("export");
        if (sets == null) {
            writeException((new IOException(new SyntaxError("xport RankQuery is required for xsort: rq={!xport}"))), writer, true);
            return;
        }
    }
    SolrParams params = req.getParams();
    String fl = params.get("fl");
    String[] fields = null;
    if (fl == null) {
        writeException((new IOException(new SyntaxError("export field list (fl) must be specified."))), writer, true);
        return;
    } else {
        fields = fl.split(",");
        for (int i = 0; i < fields.length; i++) {
            fields[i] = fields[i].trim();
            if (fields[i].equals("score")) {
                writeException((new IOException(new SyntaxError("Scoring is not currently supported with xsort."))), writer, true);
                return;
            }
        }
    }
    try {
        fieldWriters = getFieldWriters(fields, req.getSearcher());
    } catch (Exception e) {
        writeException(e, writer, true);
        return;
    }
    writer.writeMap(m -> {
        m.put("responseHeader", singletonMap("status", 0));
        m.put("response", (MapWriter) mw -> {
            mw.put("numFound", totalHits);
            mw.put("docs", (IteratorWriter) iw -> writeDocs(req, iw, sort));
        });
    });
}
Also used : BitSetIterator(org.apache.lucene.util.BitSetIterator) Date(java.util.Date) EntryWriter(org.apache.solr.common.MapWriter.EntryWriter) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) IndexableField(org.apache.lucene.index.IndexableField) JSONResponseWriter(org.apache.solr.response.JSONResponseWriter) BoolField(org.apache.solr.schema.BoolField) StrField(org.apache.solr.schema.StrField) LoggerFactory(org.slf4j.LoggerFactory) LongValues(org.apache.lucene.util.LongValues) SolrParams(org.apache.solr.common.params.SolrParams) Collections.singletonList(java.util.Collections.singletonList) SolrException(org.apache.solr.common.SolrException) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SortField(org.apache.lucene.search.SortField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) PrintWriter(java.io.PrintWriter) MapWriter(org.apache.solr.common.MapWriter) Sort(org.apache.lucene.search.Sort) BytesRef(org.apache.lucene.util.BytesRef) SolrCore(org.apache.solr.core.SolrCore) MethodHandles(java.lang.invoke.MethodHandles) StandardCharsets(java.nio.charset.StandardCharsets) Utils.makeMap(org.apache.solr.common.util.Utils.makeMap) List(java.util.List) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) LeafReader(org.apache.lucene.index.LeafReader) IteratorWriter(org.apache.solr.common.IteratorWriter) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) TrieDateField(org.apache.solr.schema.TrieDateField) PushWriter(org.apache.solr.common.PushWriter) NumericDocValues(org.apache.lucene.index.NumericDocValues) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) FieldType(org.apache.solr.schema.FieldType) FixedBitSet(org.apache.lucene.util.FixedBitSet) CharsRefBuilder(org.apache.lucene.util.CharsRefBuilder) SortSpec(org.apache.solr.search.SortSpec) BinaryResponseParser(org.apache.solr.client.solrj.impl.BinaryResponseParser) SchemaField(org.apache.solr.schema.SchemaField) TrieFloatField(org.apache.solr.schema.TrieFloatField) TrieLongField(org.apache.solr.schema.TrieLongField) SyntaxError(org.apache.solr.search.SyntaxError) SortedSetDocValues(org.apache.lucene.index.SortedSetDocValues) TrieIntField(org.apache.solr.schema.TrieIntField) OutputStreamWriter(java.io.OutputStreamWriter) Collections.singletonMap(java.util.Collections.singletonMap) SortedDocValues(org.apache.lucene.index.SortedDocValues) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) OutputStream(java.io.OutputStream) ArrayUtil(org.apache.lucene.util.ArrayUtil) Logger(org.slf4j.Logger) MultiDocValues(org.apache.lucene.index.MultiDocValues) BinaryResponseWriter(org.apache.solr.response.BinaryResponseWriter) IOException(java.io.IOException) TrieDoubleField(org.apache.solr.schema.TrieDoubleField) IndexSchema(org.apache.solr.schema.IndexSchema) DocValues(org.apache.lucene.index.DocValues) Closeable(java.io.Closeable) BinaryResponseWriter(org.apache.solr.response.BinaryResponseWriter) IOException(java.io.IOException) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) SyntaxError(org.apache.solr.search.SyntaxError) FixedBitSet(org.apache.lucene.util.FixedBitSet) IteratorWriter(org.apache.solr.common.IteratorWriter) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) Sort(org.apache.lucene.search.Sort) SolrParams(org.apache.solr.common.params.SolrParams) OutputStreamWriter(java.io.OutputStreamWriter) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) SortSpec(org.apache.solr.search.SortSpec)

Example 9 with SortSpec

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

the class SearchGroupShardResponseProcessor method process.

/**
   * {@inheritDoc}
   */
@Override
public void process(ResponseBuilder rb, ShardRequest shardRequest) {
    SortSpec groupSortSpec = rb.getGroupingSpec().getGroupSortSpec();
    Sort groupSort = rb.getGroupingSpec().getGroupSort();
    final String[] fields = rb.getGroupingSpec().getFields();
    Sort withinGroupSort = rb.getGroupingSpec().getSortWithinGroup();
    assert withinGroupSort != null;
    final Map<String, List<Collection<SearchGroup<BytesRef>>>> commandSearchGroups = new HashMap<>(fields.length, 1.0f);
    final Map<String, Map<SearchGroup<BytesRef>, Set<String>>> tempSearchGroupToShards = new HashMap<>(fields.length, 1.0f);
    for (String field : fields) {
        commandSearchGroups.put(field, new ArrayList<Collection<SearchGroup<BytesRef>>>(shardRequest.responses.size()));
        tempSearchGroupToShards.put(field, new HashMap<SearchGroup<BytesRef>, Set<String>>());
        if (!rb.searchGroupToShards.containsKey(field)) {
            rb.searchGroupToShards.put(field, new HashMap<SearchGroup<BytesRef>, Set<String>>());
        }
    }
    SearchGroupsResultTransformer serializer = new SearchGroupsResultTransformer(rb.req.getSearcher());
    int maxElapsedTime = 0;
    int hitCountDuringFirstPhase = 0;
    NamedList<Object> shardInfo = null;
    if (rb.req.getParams().getBool(ShardParams.SHARDS_INFO, false)) {
        shardInfo = new SimpleOrderedMap<>(shardRequest.responses.size());
        rb.rsp.getValues().add(ShardParams.SHARDS_INFO + ".firstPhase", shardInfo);
    }
    for (ShardResponse srsp : shardRequest.responses) {
        if (shardInfo != null) {
            SimpleOrderedMap<Object> nl = new SimpleOrderedMap<>(4);
            if (srsp.getException() != null) {
                Throwable t = srsp.getException();
                if (t instanceof SolrServerException) {
                    t = ((SolrServerException) t).getCause();
                }
                nl.add("error", t.toString());
                StringWriter trace = new StringWriter();
                t.printStackTrace(new PrintWriter(trace));
                nl.add("trace", trace.toString());
            } else {
                nl.add("numFound", (Integer) srsp.getSolrResponse().getResponse().get("totalHitCount"));
            }
            if (srsp.getSolrResponse() != null) {
                nl.add("time", srsp.getSolrResponse().getElapsedTime());
            }
            if (srsp.getShardAddress() != null) {
                nl.add("shardAddress", srsp.getShardAddress());
            }
            shardInfo.add(srsp.getShard(), nl);
        }
        if (rb.req.getParams().getBool(ShardParams.SHARDS_TOLERANT, false) && srsp.getException() != null) {
            if (rb.rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_PARTIAL_RESULTS_KEY) == null) {
                rb.rsp.getResponseHeader().add(SolrQueryResponse.RESPONSE_HEADER_PARTIAL_RESULTS_KEY, Boolean.TRUE);
            }
            // continue if there was an error and we're tolerant.
            continue;
        }
        maxElapsedTime = (int) Math.max(maxElapsedTime, srsp.getSolrResponse().getElapsedTime());
        @SuppressWarnings("unchecked") NamedList<NamedList> firstPhaseResult = (NamedList<NamedList>) srsp.getSolrResponse().getResponse().get("firstPhase");
        final Map<String, SearchGroupsFieldCommandResult> result = serializer.transformToNative(firstPhaseResult, groupSort, withinGroupSort, srsp.getShard());
        for (String field : commandSearchGroups.keySet()) {
            final SearchGroupsFieldCommandResult firstPhaseCommandResult = result.get(field);
            final Integer groupCount = firstPhaseCommandResult.getGroupCount();
            if (groupCount != null) {
                Integer existingGroupCount = rb.mergedGroupCounts.get(field);
                // Assuming groups don't cross shard boundary...
                rb.mergedGroupCounts.put(field, existingGroupCount != null ? Integer.valueOf(existingGroupCount + groupCount) : groupCount);
            }
            final Collection<SearchGroup<BytesRef>> searchGroups = firstPhaseCommandResult.getSearchGroups();
            if (searchGroups == null) {
                continue;
            }
            commandSearchGroups.get(field).add(searchGroups);
            for (SearchGroup<BytesRef> searchGroup : searchGroups) {
                Map<SearchGroup<BytesRef>, Set<String>> map = tempSearchGroupToShards.get(field);
                Set<String> shards = map.get(searchGroup);
                if (shards == null) {
                    shards = new HashSet<>();
                    map.put(searchGroup, shards);
                }
                shards.add(srsp.getShard());
            }
        }
        hitCountDuringFirstPhase += (Integer) srsp.getSolrResponse().getResponse().get("totalHitCount");
    }
    rb.totalHitCount = hitCountDuringFirstPhase;
    rb.firstPhaseElapsedTime = maxElapsedTime;
    for (String groupField : commandSearchGroups.keySet()) {
        List<Collection<SearchGroup<BytesRef>>> topGroups = commandSearchGroups.get(groupField);
        Collection<SearchGroup<BytesRef>> mergedTopGroups = SearchGroup.merge(topGroups, groupSortSpec.getOffset(), groupSortSpec.getCount(), groupSort);
        if (mergedTopGroups == null) {
            continue;
        }
        rb.mergedSearchGroups.put(groupField, mergedTopGroups);
        for (SearchGroup<BytesRef> mergedTopGroup : mergedTopGroups) {
            rb.searchGroupToShards.get(groupField).put(mergedTopGroup, tempSearchGroupToShards.get(groupField).get(mergedTopGroup));
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) ShardResponse(org.apache.solr.handler.component.ShardResponse) StringWriter(java.io.StringWriter) Sort(org.apache.lucene.search.Sort) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) List(java.util.List) SearchGroupsFieldCommandResult(org.apache.solr.search.grouping.distributed.command.SearchGroupsFieldCommandResult) SearchGroupsResultTransformer(org.apache.solr.search.grouping.distributed.shardresultserializer.SearchGroupsResultTransformer) BytesRef(org.apache.lucene.util.BytesRef) PrintWriter(java.io.PrintWriter) SearchGroup(org.apache.lucene.search.grouping.SearchGroup) NamedList(org.apache.solr.common.util.NamedList) Collection(java.util.Collection) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) HashMap(java.util.HashMap) Map(java.util.Map) SortSpec(org.apache.solr.search.SortSpec)

Aggregations

SortSpec (org.apache.solr.search.SortSpec)9 Sort (org.apache.lucene.search.Sort)7 SortField (org.apache.lucene.search.SortField)6 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)5 SchemaField (org.apache.solr.schema.SchemaField)5 SolrException (org.apache.solr.common.SolrException)4 SolrParams (org.apache.solr.common.params.SolrParams)4 NamedList (org.apache.solr.common.util.NamedList)4 IOException (java.io.IOException)3 PrintWriter (java.io.PrintWriter)3 ArrayList (java.util.ArrayList)3 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)3 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)3 StringWriter (java.io.StringWriter)2 HashMap (java.util.HashMap)2 List (java.util.List)2 IndexReader (org.apache.lucene.index.IndexReader)2 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 BoostQuery (org.apache.lucene.search.BoostQuery)2