Search in sources :

Example 1 with GroupDocs

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

the class QueryComponent method groupedFinishStage.

@SuppressWarnings("unchecked")
protected void groupedFinishStage(final ResponseBuilder rb) {
    // To have same response as non-distributed request.
    GroupingSpecification groupSpec = rb.getGroupingSpec();
    if (rb.mergedTopGroups.isEmpty()) {
        for (String field : groupSpec.getFields()) {
            rb.mergedTopGroups.put(field, new TopGroups(null, null, 0, 0, new GroupDocs[] {}, Float.NaN));
        }
        rb.resultIds = new HashMap<>();
    }
    EndResultTransformer.SolrDocumentSource solrDocumentSource = doc -> {
        ShardDoc solrDoc = (ShardDoc) doc;
        return rb.retrievedDocuments.get(solrDoc.id);
    };
    EndResultTransformer endResultTransformer;
    if (groupSpec.isMain()) {
        endResultTransformer = MAIN_END_RESULT_TRANSFORMER;
    } else if (Grouping.Format.grouped == groupSpec.getResponseFormat()) {
        endResultTransformer = new GroupedEndResultTransformer(rb.req.getSearcher());
    } else if (Grouping.Format.simple == groupSpec.getResponseFormat() && !groupSpec.isMain()) {
        endResultTransformer = SIMPLE_END_RESULT_TRANSFORMER;
    } else {
        return;
    }
    Map<String, Object> combinedMap = new LinkedHashMap<>();
    combinedMap.putAll(rb.mergedTopGroups);
    combinedMap.putAll(rb.mergedQueryCommandResults);
    endResultTransformer.transform(combinedMap, rb, solrDocumentSource);
}
Also used : Query(org.apache.lucene.search.Query) Arrays(java.util.Arrays) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) SolrDocumentList(org.apache.solr.common.SolrDocumentList) RankQuery(org.apache.solr.search.RankQuery) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) DocList(org.apache.solr.search.DocList) SolrPluginUtils(org.apache.solr.util.SolrPluginUtils) Term(org.apache.lucene.index.Term) LoggerFactory(org.slf4j.LoggerFactory) SearchGroupsFieldCommand(org.apache.solr.search.grouping.distributed.command.SearchGroupsFieldCommand) SolrParams(org.apache.solr.common.params.SolrParams) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) QueryCommand(org.apache.solr.search.QueryCommand) Locale(java.util.Locale) Map(java.util.Map) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SortField(org.apache.lucene.search.SortField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) QParserPlugin(org.apache.solr.search.QParserPlugin) QueryResult(org.apache.solr.search.QueryResult) PrintWriter(java.io.PrintWriter) CursorMark(org.apache.solr.search.CursorMark) Scorer(org.apache.lucene.search.Scorer) ShardParams(org.apache.solr.common.params.ShardParams) StatsCache(org.apache.solr.search.stats.StatsCache) Sort(org.apache.lucene.search.Sort) BytesRef(org.apache.lucene.util.BytesRef) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) MethodHandles(java.lang.invoke.MethodHandles) Collection(java.util.Collection) InPlaceMergeSorter(org.apache.lucene.util.InPlaceMergeSorter) BasicResultContext(org.apache.solr.response.BasicResultContext) ShardResponseProcessor(org.apache.solr.search.grouping.distributed.ShardResponseProcessor) LeafFieldComparator(org.apache.lucene.search.LeafFieldComparator) EndResultTransformer(org.apache.solr.search.grouping.endresulttransformer.EndResultTransformer) SearchGroupsResultTransformer(org.apache.solr.search.grouping.distributed.shardresultserializer.SearchGroupsResultTransformer) GroupDocs(org.apache.lucene.search.grouping.GroupDocs) List(java.util.List) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) TopGroupsResultTransformer(org.apache.solr.search.grouping.distributed.shardresultserializer.TopGroupsResultTransformer) Grouping(org.apache.solr.search.Grouping) DocSlice(org.apache.solr.search.DocSlice) TopGroupsShardResponseProcessor(org.apache.solr.search.grouping.distributed.responseprocessor.TopGroupsShardResponseProcessor) MainEndResultTransformer(org.apache.solr.search.grouping.endresulttransformer.MainEndResultTransformer) SimpleEndResultTransformer(org.apache.solr.search.grouping.endresulttransformer.SimpleEndResultTransformer) ReaderUtil(org.apache.lucene.index.ReaderUtil) QParser(org.apache.solr.search.QParser) SolrReturnFields(org.apache.solr.search.SolrReturnFields) StoredFieldsShardRequestFactory(org.apache.solr.search.grouping.distributed.requestfactory.StoredFieldsShardRequestFactory) StoredFieldsShardResponseProcessor(org.apache.solr.search.grouping.distributed.responseprocessor.StoredFieldsShardResponseProcessor) FieldType(org.apache.solr.schema.FieldType) Weight(org.apache.lucene.search.Weight) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DocListAndSet(org.apache.solr.search.DocListAndSet) GroupingSpecification(org.apache.solr.search.grouping.GroupingSpecification) HashMap(java.util.HashMap) FieldComparator(org.apache.lucene.search.FieldComparator) CursorMarkParams(org.apache.solr.common.params.CursorMarkParams) TopGroupsShardRequestFactory(org.apache.solr.search.grouping.distributed.requestfactory.TopGroupsShardRequestFactory) StrUtils(org.apache.solr.common.util.StrUtils) SearchGroupShardResponseProcessor(org.apache.solr.search.grouping.distributed.responseprocessor.SearchGroupShardResponseProcessor) ArrayList(java.util.ArrayList) SortSpec(org.apache.solr.search.SortSpec) LinkedHashMap(java.util.LinkedHashMap) SchemaField(org.apache.solr.schema.SchemaField) SyntaxError(org.apache.solr.search.SyntaxError) TopGroups(org.apache.lucene.search.grouping.TopGroups) GroupParams(org.apache.solr.common.params.GroupParams) SearchGroup(org.apache.lucene.search.grouping.SearchGroup) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) QueryParsing(org.apache.solr.search.QueryParsing) CommandHandler(org.apache.solr.search.grouping.CommandHandler) GroupedEndResultTransformer(org.apache.solr.search.grouping.endresulttransformer.GroupedEndResultTransformer) DocIterator(org.apache.solr.search.DocIterator) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) SearchGroupsRequestFactory(org.apache.solr.search.grouping.distributed.requestfactory.SearchGroupsRequestFactory) StringWriter(java.io.StringWriter) IOException(java.io.IOException) ShardRequestFactory(org.apache.solr.search.grouping.distributed.ShardRequestFactory) ReturnFields(org.apache.solr.search.ReturnFields) NamedList(org.apache.solr.common.util.NamedList) IndexSchema(org.apache.solr.schema.IndexSchema) CommonParams(org.apache.solr.common.params.CommonParams) ResultContext(org.apache.solr.response.ResultContext) SolrDocument(org.apache.solr.common.SolrDocument) Builder(org.apache.solr.search.grouping.distributed.command.QueryCommand.Builder) TopGroupsFieldCommand(org.apache.solr.search.grouping.distributed.command.TopGroupsFieldCommand) SortSpecParsing(org.apache.solr.search.SortSpecParsing) Collections(java.util.Collections) IndexReaderContext(org.apache.lucene.index.IndexReaderContext) GroupedEndResultTransformer(org.apache.solr.search.grouping.endresulttransformer.GroupedEndResultTransformer) LinkedHashMap(java.util.LinkedHashMap) EndResultTransformer(org.apache.solr.search.grouping.endresulttransformer.EndResultTransformer) MainEndResultTransformer(org.apache.solr.search.grouping.endresulttransformer.MainEndResultTransformer) SimpleEndResultTransformer(org.apache.solr.search.grouping.endresulttransformer.SimpleEndResultTransformer) GroupedEndResultTransformer(org.apache.solr.search.grouping.endresulttransformer.GroupedEndResultTransformer) TopGroups(org.apache.lucene.search.grouping.TopGroups) GroupingSpecification(org.apache.solr.search.grouping.GroupingSpecification) GroupDocs(org.apache.lucene.search.grouping.GroupDocs)

Example 2 with GroupDocs

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

the class TopGroupsResultTransformer method transformToNative.

/**
   * {@inheritDoc}
   */
@Override
public Map<String, ?> transformToNative(NamedList<NamedList> shardResponse, Sort groupSort, Sort withinGroupSort, String shard) {
    Map<String, Object> result = new HashMap<>();
    final IndexSchema schema = rb.req.getSearcher().getSchema();
    for (Map.Entry<String, NamedList> entry : shardResponse) {
        String key = entry.getKey();
        NamedList commandResult = entry.getValue();
        Integer totalGroupedHitCount = (Integer) commandResult.get("totalGroupedHitCount");
        Integer totalHits = (Integer) commandResult.get("totalHits");
        if (totalHits != null) {
            Integer matches = (Integer) commandResult.get("matches");
            Float maxScore = (Float) commandResult.get("maxScore");
            if (maxScore == null) {
                maxScore = Float.NaN;
            }
            @SuppressWarnings("unchecked") List<NamedList<Object>> documents = (List<NamedList<Object>>) commandResult.get("documents");
            ScoreDoc[] scoreDocs = transformToNativeShardDoc(documents, groupSort, shard, schema);
            final TopDocs topDocs;
            if (withinGroupSort.equals(Sort.RELEVANCE)) {
                topDocs = new TopDocs(totalHits, scoreDocs, maxScore);
            } else {
                topDocs = new TopFieldDocs(totalHits, scoreDocs, withinGroupSort.getSort(), maxScore);
            }
            result.put(key, new QueryCommandResult(topDocs, matches));
            continue;
        }
        Integer totalHitCount = (Integer) commandResult.get("totalHitCount");
        List<GroupDocs<BytesRef>> groupDocs = new ArrayList<>();
        for (int i = 2; i < commandResult.size(); i++) {
            String groupValue = commandResult.getName(i);
            @SuppressWarnings("unchecked") NamedList<Object> groupResult = (NamedList<Object>) commandResult.getVal(i);
            Integer totalGroupHits = (Integer) groupResult.get("totalHits");
            Float maxScore = (Float) groupResult.get("maxScore");
            if (maxScore == null) {
                maxScore = Float.NaN;
            }
            @SuppressWarnings("unchecked") List<NamedList<Object>> documents = (List<NamedList<Object>>) groupResult.get("documents");
            ScoreDoc[] scoreDocs = transformToNativeShardDoc(documents, withinGroupSort, shard, schema);
            BytesRef groupValueRef = groupValue != null ? new BytesRef(groupValue) : null;
            groupDocs.add(new GroupDocs<>(Float.NaN, maxScore, totalGroupHits, scoreDocs, groupValueRef, null));
        }
        @SuppressWarnings("unchecked") GroupDocs<BytesRef>[] groupDocsArr = groupDocs.toArray(new GroupDocs[groupDocs.size()]);
        TopGroups<BytesRef> topGroups = new TopGroups<>(groupSort.getSort(), withinGroupSort.getSort(), totalHitCount, totalGroupedHitCount, groupDocsArr, Float.NaN);
        result.put(key, topGroups);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) TopGroups(org.apache.lucene.search.grouping.TopGroups) BytesRef(org.apache.lucene.util.BytesRef) NamedList(org.apache.solr.common.util.NamedList) QueryCommandResult(org.apache.solr.search.grouping.distributed.command.QueryCommandResult) IndexSchema(org.apache.solr.schema.IndexSchema) GroupDocs(org.apache.lucene.search.grouping.GroupDocs) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with GroupDocs

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

the class GroupConverter method fromMutable.

static TopGroups<BytesRef> fromMutable(SchemaField field, TopGroups<MutableValue> values) {
    if (values == null) {
        return null;
    }
    FieldType fieldType = field.getType();
    @SuppressWarnings("unchecked") GroupDocs<BytesRef>[] groupDocs = new GroupDocs[values.groups.length];
    for (int i = 0; i < values.groups.length; i++) {
        GroupDocs<MutableValue> original = values.groups[i];
        final BytesRef groupValue;
        if (original.groupValue.exists) {
            BytesRefBuilder binary = new BytesRefBuilder();
            fieldType.readableToIndexed(original.groupValue.toString(), binary);
            groupValue = binary.get();
        } else {
            groupValue = null;
        }
        groupDocs[i] = new GroupDocs<BytesRef>(original.score, original.maxScore, original.totalHits, original.scoreDocs, groupValue, original.groupSortValues);
    }
    return new TopGroups<BytesRef>(values.groupSort, values.withinGroupSort, values.totalHitCount, values.totalGroupedHitCount, groupDocs, values.maxScore);
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) MutableValue(org.apache.lucene.util.mutable.MutableValue) TopGroups(org.apache.lucene.search.grouping.TopGroups) GroupDocs(org.apache.lucene.search.grouping.GroupDocs) BytesRef(org.apache.lucene.util.BytesRef) FieldType(org.apache.solr.schema.FieldType)

Aggregations

GroupDocs (org.apache.lucene.search.grouping.GroupDocs)3 TopGroups (org.apache.lucene.search.grouping.TopGroups)3 BytesRef (org.apache.lucene.util.BytesRef)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)2 NamedList (org.apache.solr.common.util.NamedList)2 IndexSchema (org.apache.solr.schema.IndexSchema)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 MethodHandles (java.lang.invoke.MethodHandles)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 Locale (java.util.Locale)1