From 832bc3dbf57cf370e865e5d7ecbbb7c643e3ca92 Mon Sep 17 00:00:00 2001 From: Cao Manh Dat Date: Mon, 20 Mar 2017 15:21:36 +0700 Subject: [PATCH] SOLR-9992: Add support for grouping with PointFIelds --- .../handler/component/QueryComponent.java | 5 +++- .../TopGroupsShardRequestFactory.java | 4 ++-- .../SearchGroupsResultTransformer.java | 24 +++++++++++++++---- .../TopGroupsResultTransformer.java | 4 +++- .../apache/solr/TestDistributedGrouping.java | 2 -- .../org/apache/solr/TestGroupingSearch.java | 8 ------- .../cloud/SegmentTerminateEarlyTestState.java | 4 +--- .../apache/solr/cloud/TestSegmentSorting.java | 2 -- 8 files changed, 29 insertions(+), 24 deletions(-) diff --git a/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java b/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java index deff25bf232..08a0e842e08 100644 --- a/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java +++ b/solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java @@ -49,6 +49,7 @@ import org.apache.lucene.search.grouping.GroupDocs; import org.apache.lucene.search.grouping.SearchGroup; import org.apache.lucene.search.grouping.TopGroups; import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.BytesRefBuilder; import org.apache.lucene.util.InPlaceMergeSorter; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.common.SolrDocument; @@ -445,7 +446,9 @@ public class QueryComponent extends SearchComponent for (String topGroup : topGroupsParam) { SearchGroup searchGroup = new SearchGroup<>(); if (!topGroup.equals(TopGroupsShardRequestFactory.GROUP_NULL_VALUE)) { - searchGroup.groupValue = new BytesRef(schemaField.getType().readableToIndexed(topGroup)); + BytesRefBuilder builder = new BytesRefBuilder(); + schemaField.getType().readableToIndexed(topGroup, builder); + searchGroup.groupValue = builder.get(); } topGroups.add(searchGroup); } diff --git a/solr/core/src/java/org/apache/solr/search/grouping/distributed/requestfactory/TopGroupsShardRequestFactory.java b/solr/core/src/java/org/apache/solr/search/grouping/distributed/requestfactory/TopGroupsShardRequestFactory.java index ca73a30e164..5067c518ed5 100644 --- a/solr/core/src/java/org/apache/solr/search/grouping/distributed/requestfactory/TopGroupsShardRequestFactory.java +++ b/solr/core/src/java/org/apache/solr/search/grouping/distributed/requestfactory/TopGroupsShardRequestFactory.java @@ -19,6 +19,7 @@ package org.apache.solr.search.grouping.distributed.requestfactory; import org.apache.lucene.analysis.reverse.ReverseStringFilter; import org.apache.lucene.search.grouping.SearchGroup; import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.CharsRefBuilder; import org.apache.solr.common.params.CommonParams; import org.apache.solr.common.params.GroupParams; import org.apache.solr.common.params.ModifiableSolrParams; @@ -117,9 +118,8 @@ public class TopGroupsShardRequestFactory implements ShardRequestFactory { for (SearchGroup searchGroup : entry.getValue()) { String groupValue; if (searchGroup.groupValue != null) { - String rawGroupValue = searchGroup.groupValue.utf8ToString(); FieldType fieldType = schema.getField(entry.getKey()).getType(); - groupValue = fieldType.indexedToReadable(rawGroupValue); + groupValue = fieldType.indexedToReadable(searchGroup.groupValue, new CharsRefBuilder()).toString(); } else { groupValue = GROUP_NULL_VALUE; } 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 c5c1e811970..2602221a654 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 @@ -19,6 +19,8 @@ package org.apache.solr.search.grouping.distributed.shardresultserializer; import org.apache.lucene.search.Sort; import org.apache.lucene.search.grouping.SearchGroup; import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.BytesRefBuilder; +import org.apache.lucene.util.CharsRefBuilder; import org.apache.solr.common.util.NamedList; import org.apache.solr.schema.SchemaField; import org.apache.solr.search.SolrIndexSearcher; @@ -56,7 +58,7 @@ public class SearchGroupsResultTransformer implements ShardResultTransformer> searchGroups = fieldCommandResult.getSearchGroups(); if (searchGroups != null) { - commandResult.add(TOP_GROUPS, serializeSearchGroup(searchGroups, fieldCommand.getGroupSort())); + commandResult.add(TOP_GROUPS, serializeSearchGroup(searchGroups, fieldCommand)); } final Integer groupedCount = fieldCommandResult.getGroupCount(); if (groupedCount != null) { @@ -85,7 +87,17 @@ public class SearchGroupsResultTransformer implements ShardResultTransformer> rawSearchGroup : rawSearchGroups){ SearchGroup searchGroup = new SearchGroup<>(); - searchGroup.groupValue = rawSearchGroup.getKey() != null ? new BytesRef(rawSearchGroup.getKey()) : null; + SchemaField groupField = rawSearchGroup.getKey() != null? searcher.getSchema().getFieldOrNull(command.getKey()) : null; + searchGroup.groupValue = null; + if (rawSearchGroup.getKey() != null) { + if (groupField != null) { + BytesRefBuilder builder = new BytesRefBuilder(); + groupField.getType().readableToIndexed(rawSearchGroup.getKey(), builder); + searchGroup.groupValue = builder.get(); + } else { + searchGroup.groupValue = new BytesRef(rawSearchGroup.getKey()); + } + } 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; @@ -101,17 +113,19 @@ public class SearchGroupsResultTransformer implements ShardResultTransformer> data, Sort groupSort) { + private NamedList serializeSearchGroup(Collection> data, SearchGroupsFieldCommand command) { final NamedList result = new NamedList<>(data.size()); for (SearchGroup searchGroup : data) { Object[] convertedSortValues = new Object[searchGroup.sortValues.length]; for (int i = 0; i < searchGroup.sortValues.length; i++) { Object sortValue = searchGroup.sortValues[i]; - SchemaField field = groupSort.getSort()[i].getField() != null ? searcher.getSchema().getFieldOrNull(groupSort.getSort()[i].getField()) : null; + SchemaField field = command.getGroupSort().getSort()[i].getField() != null ? + searcher.getSchema().getFieldOrNull(command.getGroupSort().getSort()[i].getField()) : null; convertedSortValues[i] = ShardResultTransformerUtils.marshalSortValue(sortValue, field); } - String groupValue = searchGroup.groupValue != null ? searchGroup.groupValue.utf8ToString() : null; + SchemaField field = searcher.getSchema().getFieldOrNull(command.getKey()); + String groupValue = searchGroup.groupValue != null ? field.getType().indexedToReadable(searchGroup.groupValue, new CharsRefBuilder()).toString() : null; result.add(groupValue, convertedSortValues); } 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 3ff87bd83ed..415e513d8a8 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 @@ -33,6 +33,7 @@ import org.apache.lucene.search.TopFieldDocs; import org.apache.lucene.search.grouping.GroupDocs; import org.apache.lucene.search.grouping.TopGroups; import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.CharsRefBuilder; import org.apache.solr.common.util.NamedList; import org.apache.solr.handler.component.ResponseBuilder; import org.apache.solr.handler.component.ShardDoc; @@ -232,7 +233,8 @@ public class TopGroupsResultTransformer implements ShardResultTransformer