Search in sources :

Example 1 with SearchGroupsFieldCommandResult

use of org.apache.solr.search.grouping.distributed.command.SearchGroupsFieldCommandResult in project lucene-solr by apache.

the class SearchGroupsResultTransformer method transform.

/**
   * {@inheritDoc}
   */
@Override
public NamedList transform(List<Command> data) throws IOException {
    final NamedList<NamedList> result = new NamedList<>(data.size());
    for (Command command : data) {
        final NamedList<Object> commandResult = new NamedList<>(2);
        if (SearchGroupsFieldCommand.class.isInstance(command)) {
            SearchGroupsFieldCommand fieldCommand = (SearchGroupsFieldCommand) command;
            final SearchGroupsFieldCommandResult fieldCommandResult = fieldCommand.result();
            final Collection<SearchGroup<BytesRef>> searchGroups = fieldCommandResult.getSearchGroups();
            if (searchGroups != null) {
                commandResult.add(TOP_GROUPS, serializeSearchGroup(searchGroups, fieldCommand));
            }
            final Integer groupedCount = fieldCommandResult.getGroupCount();
            if (groupedCount != null) {
                commandResult.add(GROUP_COUNT, groupedCount);
            }
        } else {
            continue;
        }
        result.add(command.getKey(), commandResult);
    }
    return result;
}
Also used : Command(org.apache.solr.search.grouping.Command) SearchGroupsFieldCommand(org.apache.solr.search.grouping.distributed.command.SearchGroupsFieldCommand) SearchGroup(org.apache.lucene.search.grouping.SearchGroup) NamedList(org.apache.solr.common.util.NamedList) SearchGroupsFieldCommand(org.apache.solr.search.grouping.distributed.command.SearchGroupsFieldCommand) SearchGroupsFieldCommandResult(org.apache.solr.search.grouping.distributed.command.SearchGroupsFieldCommandResult)

Example 2 with SearchGroupsFieldCommandResult

use of org.apache.solr.search.grouping.distributed.command.SearchGroupsFieldCommandResult 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)

Example 3 with SearchGroupsFieldCommandResult

use of org.apache.solr.search.grouping.distributed.command.SearchGroupsFieldCommandResult in project lucene-solr by apache.

the class SearchGroupsResultTransformer method transformToNative.

/**
   * {@inheritDoc}
   */
@Override
public Map<String, SearchGroupsFieldCommandResult> transformToNative(NamedList<NamedList> shardResponse, Sort groupSort, Sort withinGroupSort, String shard) {
    final Map<String, SearchGroupsFieldCommandResult> result = new HashMap<>(shardResponse.size());
    for (Map.Entry<String, NamedList> command : shardResponse) {
        List<SearchGroup<BytesRef>> searchGroups = new ArrayList<>();
        NamedList topGroupsAndGroupCount = command.getValue();
        @SuppressWarnings("unchecked") final NamedList<List<Comparable>> rawSearchGroups = (NamedList<List<Comparable>>) topGroupsAndGroupCount.get(TOP_GROUPS);
        if (rawSearchGroups != null) {
            for (Map.Entry<String, List<Comparable>> rawSearchGroup : rawSearchGroups) {
                SearchGroup<BytesRef> searchGroup = new SearchGroup<>();
                SchemaField groupField = rawSearchGroup.getKey() != null ? searcher.getSchema().getFieldOrNull(command.getKey()) : null;
                searchGroup.groupValue = null;
                if (rawSearchGroup.getKey() != null) {
                    if (groupField != null) {
                        BytesRefBuilder builder = new BytesRefBuilder();
                        groupField.getType().readableToIndexed(rawSearchGroup.getKey(), builder);
                        searchGroup.groupValue = builder.get();
                    } else {
                        searchGroup.groupValue = new BytesRef(rawSearchGroup.getKey());
                    }
                }
                searchGroup.sortValues = rawSearchGroup.getValue().toArray(new Comparable[rawSearchGroup.getValue().size()]);
                for (int i = 0; i < searchGroup.sortValues.length; i++) {
                    SchemaField field = groupSort.getSort()[i].getField() != null ? searcher.getSchema().getFieldOrNull(groupSort.getSort()[i].getField()) : null;
                    searchGroup.sortValues[i] = ShardResultTransformerUtils.unmarshalSortValue(searchGroup.sortValues[i], field);
                }
                searchGroups.add(searchGroup);
            }
        }
        final Integer groupCount = (Integer) topGroupsAndGroupCount.get(GROUP_COUNT);
        result.put(command.getKey(), new SearchGroupsFieldCommandResult(groupCount, searchGroups));
    }
    return result;
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) SearchGroup(org.apache.lucene.search.grouping.SearchGroup) NamedList(org.apache.solr.common.util.NamedList) SchemaField(org.apache.solr.schema.SchemaField) NamedList(org.apache.solr.common.util.NamedList) SearchGroupsFieldCommandResult(org.apache.solr.search.grouping.distributed.command.SearchGroupsFieldCommandResult) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

SearchGroup (org.apache.lucene.search.grouping.SearchGroup)3 NamedList (org.apache.solr.common.util.NamedList)3 SearchGroupsFieldCommandResult (org.apache.solr.search.grouping.distributed.command.SearchGroupsFieldCommandResult)3 BytesRef (org.apache.lucene.util.BytesRef)2 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Sort (org.apache.lucene.search.Sort)1 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)1 ShardResponse (org.apache.solr.handler.component.ShardResponse)1 SchemaField (org.apache.solr.schema.SchemaField)1 SortSpec (org.apache.solr.search.SortSpec)1