1
package org.apache.solr.search.grouping.distributed.shardresultserializer;
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.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;
30
import java.io.IOException;
34
* Implementation for transforming {@link SearchGroup} into a {@link NamedList} structure and visa versa.
36
public class SearchGroupsResultTransformer implements ShardResultTransformer<List<Command>, Map<String, Collection<SearchGroup<String>>>> {
38
private final SolrIndexSearcher searcher;
40
public SearchGroupsResultTransformer(SolrIndexSearcher searcher) {
41
this.searcher = searcher;
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) {
58
commandResult = serializeSearchGroup(searchGroups, fieldCommand.getGroupSort());
63
result.add(command.getKey(), commandResult);
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);
84
result.put(command.getKey(), searchGroups);
89
private NamedList serializeSearchGroup(Collection<SearchGroup<String>> data, Sort groupSort) {
90
NamedList<Comparable[]> result = new NamedList<Comparable[]>();
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;
98
FieldType fieldType = field.getType();
99
if (sortValue instanceof String) {
100
sortValue = (Comparable) fieldType.toObject(field.createField(fieldType.indexedToReadable((String) sortValue), 0.0f));
103
convertedSortValues[i] = sortValue;
105
String groupValue = searchGroup.groupValue != null ? searchGroup.groupValue : null;
106
result.add(groupValue, convertedSortValues);