Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-5399, SOLR-5354: fix distributed grouping to marshal/unmarshal sort values properly

Robert Muir11a5162fca6a
diff --git a/solr/core/src/java/org/apache/solr/search/grouping/distributed/shardresultserializer/SearchGroupsResultTransformer.java b/solr/core/src/java/org/apache/solr/search/grouping/distributed/shardresultserializer/SearchGroupsResultTransformer.java
index 8e5569bd61b..33d3c6b57be 100644
--- a/solr/core/src/java/org/apache/solr/search/grouping/distributed/shardresultserializer/SearchGroupsResultTransformer.java
+++ b/solr/core/src/java/org/apache/solr/search/grouping/distributed/shardresultserializer/SearchGroupsResultTransformer.java
@@ -88,6 +88,15 @@ public class SearchGroupsResultTransformer implements ShardResultTransformer<Lis
SearchGroup<BytesRef> searchGroup = new SearchGroup<BytesRef>();
searchGroup.groupValue = rawSearchGroup.getKey() != null ? new BytesRef(rawSearchGroup.getKey()) : null;
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;
+ if (field != null) {
+ FieldType fieldType = field.getType();
+ if (searchGroup.sortValues[i] != null) {
+ searchGroup.sortValues[i] = fieldType.unmarshalSortValue(searchGroup.sortValues[i]);
+ }
+ }
+ }
searchGroups.add(searchGroup);
}
}
@@ -99,22 +108,17 @@ public class SearchGroupsResultTransformer implements ShardResultTransformer<Lis
}
private NamedList serializeSearchGroup(Collection<SearchGroup<BytesRef>> data, Sort groupSort) {
- NamedList<Comparable[]> result = new NamedList<Comparable[]>();
- CharsRef spare = new CharsRef();
+ NamedList<Object[]> result = new NamedList<Object[]>();
for (SearchGroup<BytesRef> searchGroup : data) {
- Comparable[] convertedSortValues = new Comparable[searchGroup.sortValues.length];
+ Object[] convertedSortValues = new Object[searchGroup.sortValues.length];
for (int i = 0; i < searchGroup.sortValues.length; i++) {
- Comparable sortValue = (Comparable) searchGroup.sortValues[i];
+ Object sortValue = searchGroup.sortValues[i];
SchemaField field = groupSort.getSort()[i].getField() != null ? searcher.getSchema().getFieldOrNull(groupSort.getSort()[i].getField()) : null;
if (field != null) {
FieldType fieldType = field.getType();
- if (sortValue instanceof BytesRef) {
- UnicodeUtil.UTF8toUTF16((BytesRef)sortValue, spare);
- String indexedValue = spare.toString();
- sortValue = (Comparable) fieldType.toObject(field.createField(fieldType.indexedToReadable(indexedValue), 1.0f));
- } else if (sortValue instanceof String) {
- sortValue = (Comparable) fieldType.toObject(field.createField(fieldType.indexedToReadable((String) sortValue), 1.0f));
+ if (sortValue != null) {
+ sortValue = fieldType.marshalSortValue(sortValue);
}
}
convertedSortValues[i] = sortValue;
diff --git a/solr/core/src/java/org/apache/solr/search/grouping/distributed/shardresultserializer/TopGroupsResultTransformer.java b/solr/core/src/java/org/apache/solr/search/grouping/distributed/shardresultserializer/TopGroupsResultTransformer.java
index 8e5454b6c27..1513d09e66d 100644
--- a/solr/core/src/java/org/apache/solr/search/grouping/distributed/shardresultserializer/TopGroupsResultTransformer.java
+++ b/solr/core/src/java/org/apache/solr/search/grouping/distributed/shardresultserializer/TopGroupsResultTransformer.java
@@ -93,6 +93,8 @@ public class TopGroupsResultTransformer implements ShardResultTransformer<List<C
public Map<String, ?> transformToNative(NamedList<NamedList> shardResponse, Sort groupSort, Sort sortWithinGroup, String shard) {
Map<String, Object> result = new HashMap<String, Object>();
+ final IndexSchema schema = rb.req.getSearcher().getSchema();
+
for (Map.Entry<String, NamedList> entry : shardResponse) {
String key = entry.getKey();
NamedList commandResult = entry.getValue();
@@ -124,6 +126,15 @@ public class TopGroupsResultTransformer implements ShardResultTransformer<List<C
Object sortValuesVal = document.get("sortValues");
if (sortValuesVal != null) {
sortValues = ((List) sortValuesVal).toArray();
+ for (int k = 0; k < sortValues.length; k++) {
+ SchemaField field = groupSort.getSort()[k].getField() != null ? schema.getFieldOrNull(groupSort.getSort()[k].getField()) : null;
+ if (field != null) {
+ FieldType fieldType = field.getType();
+ if (sortValues[k] != null) {
+ sortValues[k] = fieldType.unmarshalSortValue(sortValues[k]);
+ }
+ }
+ }
}
else {
log.warn("doc {} has null 'sortValues'", document);
@@ -158,6 +169,15 @@ public class TopGroupsResultTransformer implements ShardResultTransformer<List<C
score = Float.NaN;
}
Object[] sortValues = ((List) document.get("sortValues")).toArray();
+ for (int k = 0; k < sortValues.length; k++) {
+ SchemaField field = sortWithinGroup.getSort()[k].getField() != null ? schema.getFieldOrNull(sortWithinGroup.getSort()[k].getField()) : null;
+ if (field != null) {
+ FieldType fieldType = field.getType();
+ if (sortValues[k] != null) {
+ sortValues[k] = fieldType.unmarshalSortValue(sortValues[k]);
+ }
+ }
+ }
scoreDocs[j++] = new ShardDoc(score, sortValues, uniqueId, shard);
}
@@ -217,12 +237,8 @@ public class TopGroupsResultTransformer implements ShardResultTransformer<List<C
SchemaField field = sortWithinGroup.getSort()[j].getField() != null ? schema.getFieldOrNull(sortWithinGroup.getSort()[j].getField()) : null;
if (field != null) {
FieldType fieldType = field.getType();
- if (sortValue instanceof BytesRef) {
- UnicodeUtil.UTF8toUTF16((BytesRef)sortValue, spare);
- String indexedValue = spare.toString();
- sortValue = fieldType.toObject(field.createField(fieldType.indexedToReadable(indexedValue), 1.0f));
- } else if (sortValue instanceof String) {
- sortValue = fieldType.toObject(field.createField(fieldType.indexedToReadable((String) sortValue), 1.0f));
+ if (sortValue != null) {
+ sortValue = fieldType.marshalSortValue(sortValue);
}
}
convertedSortValues[j] = sortValue;
@@ -272,12 +288,8 @@ public class TopGroupsResultTransformer implements ShardResultTransformer<List<C
? schema.getFieldOrNull(groupSort.getSort()[j].getField()) : null;
if (field != null) {
FieldType fieldType = field.getType();
- if (sortValue instanceof BytesRef) {
- UnicodeUtil.UTF8toUTF16((BytesRef)sortValue, spare);
- String indexedValue = spare.toString();
- sortValue = fieldType.toObject(field.createField(fieldType.indexedToReadable(indexedValue), 1.0f));
- } else if (sortValue instanceof String) {
- sortValue = fieldType.toObject(field.createField(fieldType.indexedToReadable((String) sortValue), 1.0f));
+ if (sortValue != null) {
+ sortValue = fieldType.marshalSortValue(sortValue);
}
}
convertedSortValues[j] = sortValue;