~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/grouping/distributed/responseprocessor/SearchGroupShardResponseProcessor.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.apache.solr.search.grouping.distributed.responseprocessor;
2
 
 
3
 
/*
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
10
 
 *
11
 
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 
 *
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.
18
 
 */
19
 
 
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;
30
 
 
31
 
import java.io.IOException;
32
 
import java.util.*;
33
 
 
34
 
/**
35
 
 * Concrete implementation for merging {@link SearchGroup} instances from shard responses.
36
 
 */
37
 
public class SearchGroupShardResponseProcessor implements ShardResponseProcessor {
38
 
 
39
 
  /**
40
 
   * {@inheritDoc}
41
 
   */
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();
46
 
 
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>());
54
 
      }
55
 
    }
56
 
 
57
 
    SearchGroupsResultTransformer serializer = new SearchGroupsResultTransformer(rb.req.getSearcher());
58
 
    try {
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) {
66
 
            continue;
67
 
          }
68
 
 
69
 
          commandSearchGroups.get(field).add(searchGroups);
70
 
          for (SearchGroup<String> searchGroup : searchGroups) {
71
 
            tempSearchGroupToShard.get(field).put(searchGroup, srsp.getShard());
72
 
          }
73
 
        }
74
 
      }
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) {
79
 
          continue;
80
 
        }
81
 
 
82
 
        rb.mergedSearchGroups.put(groupField, mergedTopGroups);
83
 
        for (SearchGroup<String> mergedTopGroup : mergedTopGroups) {
84
 
          rb.searchGroupToShard.get(groupField).put(mergedTopGroup, tempSearchGroupToShard.get(groupField).get(mergedTopGroup));
85
 
        }
86
 
      }
87
 
    } catch (IOException e) {
88
 
      throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
89
 
    }
90
 
  }
91
 
 
92
 
}