~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/shardresultserializer/SearchGroupsResultTransformer.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.shardresultserializer;
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.lucene.util.BytesRef;
23
 
import org.apache.solr.common.util.NamedList;
24
 
import org.apache.solr.schema.FieldType;
25
 
import org.apache.solr.schema.SchemaField;
26
 
import org.apache.solr.search.SolrIndexSearcher;
27
 
import org.apache.solr.search.grouping.Command;
28
 
import org.apache.solr.search.grouping.distributed.command.SearchGroupsFieldCommand;
29
 
 
30
 
import java.io.IOException;
31
 
import java.util.*;
32
 
 
33
 
/**
34
 
 * Implementation for transforming {@link SearchGroup} into a {@link NamedList} structure and visa versa.
35
 
 */
36
 
public class SearchGroupsResultTransformer implements ShardResultTransformer<List<Command>, Map<String, Collection<SearchGroup<String>>>> {
37
 
 
38
 
  private final SolrIndexSearcher searcher;
39
 
 
40
 
  public SearchGroupsResultTransformer(SolrIndexSearcher searcher) {
41
 
    this.searcher = searcher;
42
 
  }
43
 
 
44
 
  /**
45
 
   * {@inheritDoc}
46
 
   */
47
 
  public NamedList transform(List<Command> data) throws IOException {
48
 
    NamedList<NamedList> result = new NamedList<NamedList>();
49
 
    for (Command command : data) {
50
 
      NamedList commandResult;
51
 
      if (SearchGroupsFieldCommand.class.isInstance(command)) {
52
 
        SearchGroupsFieldCommand fieldCommand = (SearchGroupsFieldCommand) command;
53
 
        Collection<SearchGroup<String>> searchGroups = fieldCommand.result();
54
 
        if (searchGroups == null) {
55
 
          continue;
56
 
        }
57
 
 
58
 
        commandResult = serializeSearchGroup(searchGroups, fieldCommand.getGroupSort());
59
 
      } else {
60
 
        commandResult = null;
61
 
      }
62
 
 
63
 
      result.add(command.getKey(), commandResult);
64
 
    }
65
 
    return result;
66
 
  }
67
 
 
68
 
  /**
69
 
   * {@inheritDoc}
70
 
   */
71
 
  public Map<String, Collection<SearchGroup<String>>> transformToNative(NamedList<NamedList> shardResponse, Sort groupSort, Sort sortWithinGroup, String shard) throws IOException {
72
 
    Map<String, Collection<SearchGroup<String>>> result = new HashMap<String, Collection<SearchGroup<String>>>();
73
 
    for (Map.Entry<String, NamedList> command : shardResponse) {
74
 
      List<SearchGroup<String>> searchGroups = new ArrayList<SearchGroup<String>>();
75
 
      @SuppressWarnings("unchecked")
76
 
      NamedList<List<Comparable>> rawSearchGroups = command.getValue();
77
 
      for (Map.Entry<String, List<Comparable>> rawSearchGroup : rawSearchGroups){
78
 
        SearchGroup<String> searchGroup = new SearchGroup<String>();
79
 
        searchGroup.groupValue = rawSearchGroup.getKey() != null ? rawSearchGroup.getKey() : null;
80
 
        searchGroup.sortValues = rawSearchGroup.getValue().toArray(new Comparable[rawSearchGroup.getValue().size()]);
81
 
        searchGroups.add(searchGroup);
82
 
      }
83
 
 
84
 
      result.put(command.getKey(), searchGroups);
85
 
    }
86
 
    return result;
87
 
  }
88
 
 
89
 
  private NamedList serializeSearchGroup(Collection<SearchGroup<String>> data, Sort groupSort) {
90
 
    NamedList<Comparable[]> result = new NamedList<Comparable[]>();
91
 
 
92
 
    for (SearchGroup<String> searchGroup : data) {
93
 
      Comparable[] convertedSortValues = new Comparable[searchGroup.sortValues.length];
94
 
      for (int i = 0; i < searchGroup.sortValues.length; i++) {
95
 
        Comparable sortValue = (Comparable) searchGroup.sortValues[i];
96
 
        SchemaField field = groupSort.getSort()[i].getField() != null ? searcher.getSchema().getFieldOrNull(groupSort.getSort()[i].getField()) : null;
97
 
        if (field != null) {
98
 
          FieldType fieldType = field.getType();
99
 
          if (sortValue instanceof String) {
100
 
            sortValue = (Comparable) fieldType.toObject(field.createField(fieldType.indexedToReadable((String) sortValue), 0.0f));
101
 
          }
102
 
        }
103
 
        convertedSortValues[i] = sortValue;
104
 
      }
105
 
      String groupValue = searchGroup.groupValue != null ? searchGroup.groupValue : null;
106
 
      result.add(groupValue, convertedSortValues);
107
 
    }
108
 
 
109
 
    return result;
110
 
  }
111
 
 
112
 
}