Search in sources :

Example 1 with TopGroups

use of org.apache.lucene.search.grouping.TopGroups 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 TopGroups

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

the class MainEndResultTransformer method transform.

/**
   * {@inheritDoc}
   */
@Override
public void transform(Map<String, ?> result, ResponseBuilder rb, SolrDocumentSource solrDocumentSource) {
    Object value = result.get(rb.getGroupingSpec().getFields()[0]);
    if (TopGroups.class.isInstance(value)) {
        @SuppressWarnings("unchecked") TopGroups<BytesRef> topGroups = (TopGroups<BytesRef>) value;
        SolrDocumentList docList = new SolrDocumentList();
        docList.setStart(rb.getGroupingSpec().getOffset());
        docList.setNumFound(rb.totalHitCount);
        Float maxScore = Float.NEGATIVE_INFINITY;
        for (GroupDocs<BytesRef> group : topGroups.groups) {
            for (ScoreDoc scoreDoc : group.scoreDocs) {
                if (maxScore < scoreDoc.score) {
                    maxScore = scoreDoc.score;
                }
                docList.add(solrDocumentSource.retrieve(scoreDoc));
            }
        }
        if (maxScore != Float.NEGATIVE_INFINITY) {
            docList.setMaxScore(maxScore);
        }
        rb.rsp.addResponse(docList);
    }
}
Also used : TopGroups(org.apache.lucene.search.grouping.TopGroups) SolrDocumentList(org.apache.solr.common.SolrDocumentList) BytesRef(org.apache.lucene.util.BytesRef) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 3 with TopGroups

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

the class GroupedEndResultTransformer method transform.

/**
   * {@inheritDoc}
   */
@Override
public void transform(Map<String, ?> result, ResponseBuilder rb, SolrDocumentSource solrDocumentSource) {
    NamedList<Object> commands = new SimpleOrderedMap<>();
    for (Map.Entry<String, ?> entry : result.entrySet()) {
        Object value = entry.getValue();
        if (TopGroups.class.isInstance(value)) {
            @SuppressWarnings("unchecked") TopGroups<BytesRef> topGroups = (TopGroups<BytesRef>) value;
            NamedList<Object> command = new SimpleOrderedMap<>();
            command.add("matches", rb.totalHitCount);
            Integer totalGroupCount = rb.mergedGroupCounts.get(entry.getKey());
            if (totalGroupCount != null) {
                command.add("ngroups", totalGroupCount);
            }
            List<NamedList> groups = new ArrayList<>();
            SchemaField groupField = searcher.getSchema().getField(entry.getKey());
            FieldType groupFieldType = groupField.getType();
            for (GroupDocs<BytesRef> group : topGroups.groups) {
                SimpleOrderedMap<Object> groupResult = new SimpleOrderedMap<>();
                if (group.groupValue != null) {
                    groupResult.add("groupValue", groupFieldType.toObject(groupField.createField(group.groupValue.utf8ToString())));
                } else {
                    groupResult.add("groupValue", null);
                }
                SolrDocumentList docList = new SolrDocumentList();
                docList.setNumFound(group.totalHits);
                if (!Float.isNaN(group.maxScore)) {
                    docList.setMaxScore(group.maxScore);
                }
                docList.setStart(rb.getGroupingSpec().getWithinGroupOffset());
                for (ScoreDoc scoreDoc : group.scoreDocs) {
                    docList.add(solrDocumentSource.retrieve(scoreDoc));
                }
                groupResult.add("doclist", docList);
                groups.add(groupResult);
            }
            command.add("groups", groups);
            commands.add(entry.getKey(), command);
        } else if (QueryCommandResult.class.isInstance(value)) {
            QueryCommandResult queryCommandResult = (QueryCommandResult) value;
            NamedList<Object> command = new SimpleOrderedMap<>();
            command.add("matches", queryCommandResult.getMatches());
            SolrDocumentList docList = new SolrDocumentList();
            docList.setNumFound(queryCommandResult.getTopDocs().totalHits);
            if (!Float.isNaN(queryCommandResult.getTopDocs().getMaxScore())) {
                docList.setMaxScore(queryCommandResult.getTopDocs().getMaxScore());
            }
            docList.setStart(rb.getGroupingSpec().getWithinGroupOffset());
            for (ScoreDoc scoreDoc : queryCommandResult.getTopDocs().scoreDocs) {
                docList.add(solrDocumentSource.retrieve(scoreDoc));
            }
            command.add("doclist", docList);
            commands.add(entry.getKey(), command);
        }
    }
    rb.rsp.add("grouped", commands);
}
Also used : NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) QueryCommandResult(org.apache.solr.search.grouping.distributed.command.QueryCommandResult) SolrDocumentList(org.apache.solr.common.SolrDocumentList) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) FieldType(org.apache.solr.schema.FieldType) ScoreDoc(org.apache.lucene.search.ScoreDoc) SchemaField(org.apache.solr.schema.SchemaField) TopGroups(org.apache.lucene.search.grouping.TopGroups) SimpleOrderedMap(org.apache.solr.common.util.SimpleOrderedMap) Map(java.util.Map) BytesRef(org.apache.lucene.util.BytesRef)

Example 4 with TopGroups

use of org.apache.lucene.search.grouping.TopGroups 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 5 with TopGroups

use of org.apache.lucene.search.grouping.TopGroups 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

TopGroups (org.apache.lucene.search.grouping.TopGroups)7 BytesRef (org.apache.lucene.util.BytesRef)7 ScoreDoc (org.apache.lucene.search.ScoreDoc)5 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 SolrDocumentList (org.apache.solr.common.SolrDocumentList)4 NamedList (org.apache.solr.common.util.NamedList)4 HashMap (java.util.HashMap)3 List (java.util.List)3 GroupDocs (org.apache.lucene.search.grouping.GroupDocs)3 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)3 FieldType (org.apache.solr.schema.FieldType)3 QueryCommandResult (org.apache.solr.search.grouping.distributed.command.QueryCommandResult)3 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 Sort (org.apache.lucene.search.Sort)2 TopDocs (org.apache.lucene.search.TopDocs)2 IndexSchema (org.apache.solr.schema.IndexSchema)2 SchemaField (org.apache.solr.schema.SchemaField)2 IOException (java.io.IOException)1