1
package org.apache.solr.search.grouping.distributed.responseprocessor;
4
* Licensed to the Apache Software Foundation (ASF) under one or more
5
* contributor license agreements. See the NOTICE file distributed with
6
* this work for additional information regarding copyright ownership.
7
* The ASF licenses this file to You under the Apache License, Version 2.0
8
* (the "License"); you may not use this file except in compliance with
9
* the License. You may obtain a copy of the License at
11
* http://www.apache.org/licenses/LICENSE-2.0
13
* Unless required by applicable law or agreed to in writing, software
14
* distributed under the License is distributed on an "AS IS" BASIS,
15
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
* See the License for the specific language governing permissions and
17
* limitations under the License.
20
import org.apache.lucene.search.Sort;
21
import org.apache.lucene.search.grouping.SearchGroup;
22
import org.apache.solr.common.SolrException;
23
import org.apache.solr.common.util.NamedList;
24
import org.apache.solr.handler.component.ResponseBuilder;
25
import org.apache.solr.handler.component.ShardRequest;
26
import org.apache.solr.handler.component.ShardResponse;
27
import org.apache.solr.search.SortSpec;
28
import org.apache.solr.search.grouping.distributed.ShardResponseProcessor;
29
import org.apache.solr.search.grouping.distributed.shardresultserializer.SearchGroupsResultTransformer;
31
import java.io.IOException;
35
* Concrete implementation for merging {@link SearchGroup} instances from shard responses.
37
public class SearchGroupShardResponseProcessor implements ShardResponseProcessor {
42
public void process(ResponseBuilder rb, ShardRequest shardRequest) {
43
SortSpec ss = rb.getSortSpec();
44
Sort groupSort = rb.getGroupingSpec().getGroupSort();
45
String[] fields = rb.getGroupingSpec().getFields();
47
Map<String, List<Collection<SearchGroup<String>>>> commandSearchGroups = new HashMap<String, List<Collection<SearchGroup<String>>>>();
48
Map<String, Map<SearchGroup<String>, String>> tempSearchGroupToShard = new HashMap<String, Map<SearchGroup<String>, String>>();
49
for (String field : fields) {
50
commandSearchGroups.put(field, new ArrayList<Collection<SearchGroup<String>>>(shardRequest.responses.size()));
51
tempSearchGroupToShard.put(field, new HashMap<SearchGroup<String>, String>());
52
if (!rb.searchGroupToShard.containsKey(field)) {
53
rb.searchGroupToShard.put(field, new HashMap<SearchGroup<String>, String>());
57
SearchGroupsResultTransformer serializer = new SearchGroupsResultTransformer(rb.req.getSearcher());
59
for (ShardResponse srsp : shardRequest.responses) {
60
@SuppressWarnings("unchecked")
61
NamedList<NamedList> firstPhaseResult = (NamedList<NamedList>) srsp.getSolrResponse().getResponse().get("firstPhase");
62
Map<String, Collection<SearchGroup<String>>> result = serializer.transformToNative(firstPhaseResult, groupSort, null, srsp.getShard());
63
for (String field : commandSearchGroups.keySet()) {
64
Collection<SearchGroup<String>> searchGroups = result.get(field);
65
if (searchGroups == null) {
69
commandSearchGroups.get(field).add(searchGroups);
70
for (SearchGroup<String> searchGroup : searchGroups) {
71
tempSearchGroupToShard.get(field).put(searchGroup, srsp.getShard());
75
for (String groupField : commandSearchGroups.keySet()) {
76
List<Collection<SearchGroup<String>>> topGroups = commandSearchGroups.get(groupField);
77
Collection<SearchGroup<String>> mergedTopGroups = SearchGroup.merge(topGroups, ss.getOffset(), ss.getCount(), groupSort);
78
if (mergedTopGroups == null) {
82
rb.mergedSearchGroups.put(groupField, mergedTopGroups);
83
for (SearchGroup<String> mergedTopGroup : mergedTopGroups) {
84
rb.searchGroupToShard.get(groupField).put(mergedTopGroup, tempSearchGroupToShard.get(groupField).get(mergedTopGroup));
87
} catch (IOException e) {
88
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);