cpoerschke commented on a change in pull request #300: SOLR-11831: Skip second 
grouping step if group.limit is 1 (aka Las Vegas Patch)
URL: https://github.com/apache/lucene-solr/pull/300#discussion_r293916042
 
 

 ##########
 File path: 
solr/core/src/java/org/apache/solr/search/grouping/distributed/shardresultserializer/SearchGroupsResultTransformer.java
 ##########
 @@ -80,50 +106,120 @@ public NamedList transform(List<Command> data) throws 
IOException {
       final NamedList<List<Comparable>> rawSearchGroups = 
(NamedList<List<Comparable>>) topGroupsAndGroupCount.get(TOP_GROUPS);
       if (rawSearchGroups != null) {
         for (Map.Entry<String, List<Comparable>> rawSearchGroup : 
rawSearchGroups){
-          SearchGroup<BytesRef> searchGroup = new SearchGroup<>();
-          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;
-            searchGroup.sortValues[i] = 
ShardResultTransformerUtils.unmarshalSortValue(searchGroup.sortValues[i], 
field);
-          }
-          searchGroups.add(searchGroup);
+          searchGroups.add(deserializeOneSearchGroup(command.getKey(), 
groupSort, rawSearchGroup.getKey(), rawSearchGroup.getValue()));
         }
       }
-
       final Integer groupCount = (Integer) 
topGroupsAndGroupCount.get(GROUP_COUNT);
       result.put(command.getKey(), new 
SearchGroupsFieldCommandResult(groupCount, searchGroups));
     }
     return result;
   }
 
-  private NamedList serializeSearchGroup(Collection<SearchGroup<BytesRef>> 
data, SearchGroupsFieldCommand command) {
-    final NamedList<Object[]> result = new NamedList<>(data.size());
+  public static class DefaultSearchResultResultTransformer extends 
SearchGroupsResultTransformer {
 
-    for (SearchGroup<BytesRef> searchGroup : data) {
+    public DefaultSearchResultResultTransformer(SolrIndexSearcher searcher) {
+      super(searcher);
+    }
+
+    // given a raw search group will return its array sort values.
+    protected Object[] getSortValues(Object groupDocs){
+      List docs = (List<Comparable>)groupDocs;
+      return docs.toArray(new Comparable[docs.size()]);
+    }
+
+    @Override
+    protected SearchGroup<BytesRef> deserializeOneSearchGroup(String 
groupField, Sort groupSort, String groupKey, Object rawSearchGroup){
+      SearchGroup<BytesRef> searchGroup = new SearchGroup<>();
+      SchemaField schemaGroupField = groupKey != null? 
searcher.getSchema().getFieldOrNull(groupField) : null;
+      searchGroup.groupValue = null;
+      if (groupKey != null) {
+        if (schemaGroupField != null) {
+          BytesRefBuilder builder = new BytesRefBuilder();
+          schemaGroupField.getType().readableToIndexed(groupKey, builder);
+          searchGroup.groupValue = builder.get();
+        } else {
+          searchGroup.groupValue = new BytesRef(groupKey);
+        }
+      }
+      searchGroup.sortValues = getSortValues(rawSearchGroup);
+      for (int i = 0; i < searchGroup.sortValues.length; i++) {
+        final String sortField = groupSort.getSort()[i].getField();
+        SchemaField field = sortField != null ? 
searcher.getSchema().getFieldOrNull(sortField) : null;
+        searchGroup.sortValues[i] = 
ShardResultTransformerUtils.unmarshalSortValue(searchGroup.sortValues[i], 
field);
+      }
+      return searchGroup;
+    }
+
+    @Override
+    protected Object serializeOneSearchGroup(SearchGroup<BytesRef> 
searchGroup, SearchGroupsFieldCommand command) {
       Object[] convertedSortValues = new Object[searchGroup.sortValues.length];
       for (int i = 0; i < searchGroup.sortValues.length; i++) {
         Object sortValue = searchGroup.sortValues[i];
         SchemaField field = command.getGroupSort().getSort()[i].getField() != 
null ?
             
searcher.getSchema().getFieldOrNull(command.getGroupSort().getSort()[i].getField())
 : null;
         convertedSortValues[i] = 
ShardResultTransformerUtils.marshalSortValue(sortValue, field);
       }
-      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);
+      return convertedSortValues;
     }
-
-    return result;
   }
 
-}
+  public static class SkipSecondStepSearchResultResultTransformer extends 
DefaultSearchResultResultTransformer {
+
+    private static final String TOP_DOC_SOLR_ID_KEY = "topDocSolrId";
+    private static final String TOP_DOC_SCORE_KEY = "topDocScore";
+    private static final String SORTVALUES_KEY  = "sortValues";
+
+    public SkipSecondStepSearchResultResultTransformer(SolrIndexSearcher 
searcher) {
+      super(searcher);
+    }
+
+    @Override
+    protected Object[] getSortValues(Object groupDocs){
+      NamedList<Object> groupInfo = (NamedList) groupDocs;
+      final ArrayList<?> sortValues = 
(ArrayList<?>)groupInfo.get(SORTVALUES_KEY);
+      return sortValues.toArray(new Comparable[sortValues.size()]);
+    }
+
+    @Override
+    protected SearchGroup<BytesRef> deserializeOneSearchGroup(String 
groupField, Sort groupSort, String groupKey, Object groupDocs){
+      SearchGroup<BytesRef> searchGroup = 
super.deserializeOneSearchGroup(groupField, groupSort, groupKey, groupDocs);
+      NamedList<Object> groupInfo = (NamedList) groupDocs;
+      searchGroup.topDocLuceneId = DocIdSetIterator.NO_MORE_DOCS;
+      searchGroup.topDocScore = (float) groupInfo.get(TOP_DOC_SCORE_KEY);
+      searchGroup.topDocSolrId = groupInfo.get(TOP_DOC_SOLR_ID_KEY);
+      return searchGroup;
+    }
+
+    @Override
+    protected Object serializeOneSearchGroup(SearchGroup<BytesRef> 
searchGroup, SearchGroupsFieldCommand command) {
 
 Review comment:
   
https://github.com/cpoerschke/lucene-solr/commit/af1a746d8a11c04a67eeaf41c26ddbbabc7b24ce
 has potential (untested) tweaks for this 
`SkipSecondStepSearchResultResultTransformer.serializeOneSearchGroup` method 
such as making `uniqueField` a class member to avoid per-document 
get-schema-then-get-unique-key-field calls and replacement of the 
`DocumentStoredFieldVisitor`-based document retrieval with the simpler 
equivalent of 
[TopGroupsResultTransformer.retrieveDocument](https://github.com/apache/lucene-solr/blob/releases/lucene-solr/8.1.1/solr/core/src/java/org/apache/solr/search/grouping/distributed/shardresultserializer/TopGroupsResultTransformer.java#L285-L287)
 -- that would be equivalent, at least at a quick glance, unless perhaps the 
`DocumentStoredFieldVisitor` use has intended side effects such as cache 
warning (in which case maybe comments at point of use might be helpful for 
future readers of the code).

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to