Example usage for org.apache.lucene.search.grouping TopGroups TopGroups

List of usage examples for org.apache.lucene.search.grouping TopGroups TopGroups

Introduction

In this page you can find the example usage for org.apache.lucene.search.grouping TopGroups TopGroups.

Prototype

public TopGroups(SortField[] groupSort, SortField[] withinGroupSort, int totalHitCount,
            int totalGroupedHitCount, GroupDocs<T>[] groups, float maxScore) 

Source Link

Usage

From source file:org.apache.solr.handler.component.HelloHandlerComponent.java

License:Apache License

@SuppressWarnings("unchecked")
private 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));
        }//from  w w w .j  a  v a2  s. com
        rb.resultIds = new HashMap<Object, ShardDoc>();
    }

    EndResultTransformer.SolrDocumentSource solrDocumentSource = new EndResultTransformer.SolrDocumentSource() {

        public SolrDocument retrieve(ScoreDoc 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<String, Object>();
    combinedMap.putAll(rb.mergedTopGroups);
    combinedMap.putAll(rb.mergedQueryCommandResults);
    endResultTransformer.transform(combinedMap, rb, solrDocumentSource);
}

From source file:org.apache.solr.handler.component.QueryComponent.java

License:Apache License

@SuppressWarnings("unchecked")
private 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));
        }//from   www .ja  va 2  s. c  o  m
        rb.resultIds = new HashMap<Object, ShardDoc>();
    }

    EndResultTransformer.SolrDocumentSource solrDocumentSource = new EndResultTransformer.SolrDocumentSource() {

        @Override
        public SolrDocument retrieve(ScoreDoc 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<String, Object>();
    combinedMap.putAll(rb.mergedTopGroups);
    combinedMap.putAll(rb.mergedQueryCommandResults);
    endResultTransformer.transform(combinedMap, rb, solrDocumentSource);
}

From source file:org.apache.solr.search.grouping.distributed.command.GroupConverter.java

License:Apache License

static TopGroups<BytesRef> fromMutable(SchemaField field, TopGroups<MutableValue> values) {
    if (values == null) {
        return null;
    }/*from w  w w.  ja  v a 2 s  .  c o m*/

    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);
}

From source file:org.apache.solr.search.grouping.distributed.command.TopGroupsFieldCommand.java

License:Apache License

@Override
@SuppressWarnings("unchecked")
public TopGroups<BytesRef> result() {
    if (firstPhaseGroups.isEmpty()) {
        return new TopGroups<BytesRef>(groupSort.getSort(), sortWithinGroup.getSort(), 0, 0, new GroupDocs[0],
                Float.NaN);//from   w  w w  .ja va 2  s. com
    }

    return secondPassCollector.getTopGroups(0);
}

From source file:org.apache.solr.search.grouping.distributed.shardresultserializer.TopGroupsResultTransformer.java

License:Apache License

/**
 * {@inheritDoc}// w w w . ja  v  a  2s  . c  o m
 */
@Override
public Map<String, ?> transformToNative(NamedList<NamedList> shardResponse, Sort groupSort,
        Sort sortWithinGroup, String shard) {
    Map<String, Object> result = new HashMap<String, Object>();

    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 = new ScoreDoc[documents.size()];
            int j = 0;
            for (NamedList<Object> document : documents) {
                Object docId = document.get("id");
                Object uniqueId = null;
                if (docId != null)
                    uniqueId = docId.toString();
                else
                    log.warn("doc {} has null 'id'", document);
                Float score = (Float) document.get("score");
                if (score == null) {
                    score = Float.NaN;
                }
                Object[] sortValues = null;
                Object sortValuesVal = document.get("sortValues");
                if (sortValuesVal != null) {
                    sortValues = ((List) sortValuesVal).toArray();
                } else {
                    log.warn("doc {} has null 'sortValues'", document);
                }
                scoreDocs[j++] = new ShardDoc(score, sortValues, uniqueId, shard);
            }
            result.put(key, new QueryCommandResult(new TopDocs(totalHits, scoreDocs, maxScore), matches));
            continue;
        }

        Integer totalHitCount = (Integer) commandResult.get("totalHitCount");

        List<GroupDocs<BytesRef>> groupDocs = new ArrayList<GroupDocs<BytesRef>>();
        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 = new ScoreDoc[documents.size()];
            int j = 0;
            for (NamedList<Object> document : documents) {
                Object uniqueId = document.get("id").toString();
                Float score = (Float) document.get("score");
                if (score == null) {
                    score = Float.NaN;
                }
                Object[] sortValues = ((List) document.get("sortValues")).toArray();
                scoreDocs[j++] = new ShardDoc(score, sortValues, uniqueId, shard);
            }

            BytesRef groupValueRef = groupValue != null ? new BytesRef(groupValue) : null;
            groupDocs.add(new GroupDocs<BytesRef>(Float.NaN, maxScore, totalGroupHits, scoreDocs, groupValueRef,
                    null));
        }

        @SuppressWarnings("unchecked")
        GroupDocs<BytesRef>[] groupDocsArr = groupDocs.toArray(new GroupDocs[groupDocs.size()]);
        TopGroups<BytesRef> topGroups = new TopGroups<BytesRef>(groupSort.getSort(), sortWithinGroup.getSort(),
                totalHitCount, totalGroupedHitCount, groupDocsArr, Float.NaN);

        result.put(key, topGroups);
    }

    return result;
}