dsmiley commented on code in PR #3418:
URL: https://github.com/apache/solr/pull/3418#discussion_r2316087608


##########
solr/core/src/java/org/apache/solr/handler/component/QueryComponent.java:
##########
@@ -905,7 +906,84 @@ protected boolean addFL(StringBuilder fl, String field, 
boolean additionalAdded)
     return true;
   }
 
+  protected abstract static class ShardDocQueue {
+    public abstract boolean push(ShardDoc shardDoc);
+
+    public abstract Map<Object, ShardDoc> resultIds(int offset);
+  }
+  ;
+
+  protected static class ShardDocQueueFactory
+      implements BiFunction<SortField[], Integer, ShardDocQueue> {
+
+    private final SolrIndexSearcher searcher;
+
+    public ShardDocQueueFactory(SolrIndexSearcher searcher) {
+      this.searcher = searcher;
+    }
+
+    @Override
+    public ShardDocQueue apply(SortField[] sortFields, Integer size) {
+      return new ShardDocQueue() {
+
+        // id to shard mapping, to eliminate any accidental dups
+        private final HashMap<Object, String> uniqueDoc = new HashMap<>();
+
+        private final ShardFieldSortedHitQueue queue =
+            new ShardFieldSortedHitQueue(sortFields, size, searcher);
+
+        @Override
+        public boolean push(ShardDoc shardDoc) {
+          final String prevShard = uniqueDoc.put(shardDoc.id, shardDoc.shard);
+          if (prevShard != null) {
+            // duplicate detected
+
+            // For now, just always use the first encountered since we can't 
currently
+            // remove the previous one added to the priority queue.  If we 
switched
+            // to the Java5 PriorityQueue, this would be easier.
+            return false;
+            // make which duplicate is used deterministic based on shard
+            // if (prevShard.compareTo(shardDoc.shard) >= 0) {
+            //  TODO: remove previous from priority queue
+            //  return false;
+            // }
+          }
+
+          queue.insertWithOverflow(shardDoc);
+          return true;
+        }
+
+        @Override
+        public Map<Object, ShardDoc> resultIds(int offset) {
+          final Map<Object, ShardDoc> resultIds = new HashMap<>();
+
+          // The queue now has 0 -> queuesize docs, where queuesize <= start + 
rows
+          // So we want to pop the last documents off the queue to get
+          // the docs offset -> queuesize
+          int resultSize = queue.size() - offset;
+          resultSize = Math.max(0, resultSize); // there may not be any docs 
in range
+
+          for (int i = resultSize - 1; i >= 0; i--) {
+            ShardDoc shardDoc = queue.pop();
+            shardDoc.positionInResponse = i;
+            // Need the toString() for correlation with other lists that must
+            // be strings (like keys in highlighting, explain, etc)
+            resultIds.put(shardDoc.id.toString(), shardDoc);
+          }
+
+          return resultIds;
+        }
+      };
+    }
+  }
+  ;
+
   protected void mergeIds(ResponseBuilder rb, ShardRequest sreq) {
+    implementMergeIds(rb, sreq, new 
ShardDocQueueFactory(rb.req.getSearcher()));
+  }
+
+  private void implementMergeIds(

Review Comment:
   I don't think I get the point of the "implementMergeIds" method separation 
unless it could get called in more situations (that I don't see in the PR 
interface).



##########
solr/core/src/java/org/apache/solr/handler/component/CombinedQueryComponent.java:
##########
@@ -0,0 +1,590 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.solr.handler.component;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.lang.invoke.MethodHandles;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.lucene.search.Explanation;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.Sort;
+import org.apache.lucene.search.SortField;
+import org.apache.solr.client.solrj.SolrServerException;
+import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.SolrDocumentList;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.params.CombinerParams;
+import org.apache.solr.common.params.CursorMarkParams;
+import org.apache.solr.common.params.GroupParams;
+import org.apache.solr.common.params.ShardParams;
+import org.apache.solr.common.params.SolrParams;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.common.util.SimpleOrderedMap;
+import org.apache.solr.common.util.StrUtils;
+import org.apache.solr.core.SolrCore;
+import org.apache.solr.response.BasicResultContext;
+import org.apache.solr.response.ResultContext;
+import org.apache.solr.response.SolrQueryResponse;
+import org.apache.solr.schema.IndexSchema;
+import org.apache.solr.schema.SchemaField;
+import org.apache.solr.search.DocListAndSet;
+import org.apache.solr.search.QueryResult;
+import org.apache.solr.search.SolrReturnFields;
+import org.apache.solr.search.SortSpec;
+import org.apache.solr.search.combine.QueryAndResponseCombiner;
+import org.apache.solr.search.combine.ReciprocalRankFusion;
+import org.apache.solr.util.SolrResponseUtil;
+import org.apache.solr.util.plugin.SolrCoreAware;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The CombinedQueryComponent class extends QueryComponent and provides 
support for executing
+ * multiple queries and combining their results.
+ */
+public class CombinedQueryComponent extends QueryComponent implements 
SolrCoreAware {
+
+  private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+  public static final String COMPONENT_NAME = "combined_query";
+  protected NamedList<?> initParams;
+  private final Map<String, QueryAndResponseCombiner> combiners = new 
HashMap<>();
+  private int maxCombinerQueries;
+
+  @Override
+  public void init(NamedList<?> args) {
+    super.init(args);
+    this.initParams = args;
+    this.maxCombinerQueries = CombinerParams.DEFAULT_MAX_COMBINER_QUERIES;
+  }
+
+  @Override
+  public void inform(SolrCore core) {
+    if (initParams != null && initParams.size() > 0) {
+      for (Map.Entry<String, ?> initEntry : initParams) {
+        if ("combiners".equals(initEntry.getKey())
+            && initEntry.getValue() instanceof NamedList<?> all) {
+          for (int i = 0; i < all.size(); i++) {
+            String name = all.getName(i);
+            NamedList<?> combinerConfig = (NamedList<?>) all.getVal(i);
+            String className = (String) combinerConfig.get("class");
+            QueryAndResponseCombiner combiner =
+                core.getResourceLoader().newInstance(className, 
QueryAndResponseCombiner.class);
+            combiner.init(combinerConfig);
+            combiners.compute(
+                name,
+                (k, existingCombiner) -> {
+                  if (existingCombiner == null) {
+                    return combiner;
+                  }
+                  throw new SolrException(
+                      SolrException.ErrorCode.BAD_REQUEST,
+                      "Found more than one combiner with same name");
+                });
+          }
+        }
+      }
+      Object maxQueries = initParams.get("maxCombinerQueries");
+      if (maxQueries != null) {
+        this.maxCombinerQueries = Integer.parseInt(maxQueries.toString());
+      }
+    }
+    combiners.computeIfAbsent(
+        CombinerParams.RECIPROCAL_RANK_FUSION,
+        key -> {
+          ReciprocalRankFusion reciprocalRankFusion = new 
ReciprocalRankFusion();
+          reciprocalRankFusion.init(initParams);
+          return reciprocalRankFusion;
+        });
+  }
+
+  /**
+   * Overrides the prepare method to handle combined queries.
+   *
+   * @param rb the ResponseBuilder to prepare
+   * @throws IOException if an I/O error occurs during preparation
+   */
+  @Override
+  public void prepare(ResponseBuilder rb) throws IOException {
+    if (rb instanceof CombinedQueryResponseBuilder crb) {
+      SolrParams params = crb.req.getParams();
+      if (params.get(CursorMarkParams.CURSOR_MARK_PARAM) != null
+          || params.getBool(GroupParams.GROUP, false)) {
+        throw new SolrException(
+            SolrException.ErrorCode.BAD_REQUEST, "Unsupported functionality 
for Combined Queries.");
+      }
+      String[] queriesToCombineKeys = 
params.getParams(CombinerParams.COMBINER_QUERY);
+      if (queriesToCombineKeys.length > maxCombinerQueries) {
+        throw new SolrException(
+            SolrException.ErrorCode.BAD_REQUEST,
+            "Too many queries to combine: limit is " + maxCombinerQueries);
+      }
+      for (String queryKey : queriesToCombineKeys) {
+        final var unparsedQuery = params.get(queryKey);
+        ResponseBuilder rbNew = new ResponseBuilder(rb.req, new 
SolrQueryResponse(), rb.components);
+        rbNew.setQueryString(unparsedQuery);
+        super.prepare(rbNew);
+        crb.responseBuilders.add(rbNew);
+      }
+    }
+    super.prepare(rb);
+  }
+
+  /**
+   * Overrides the process method to handle CombinedQueryResponseBuilder 
instances. This method
+   * processes the responses from multiple shards, combines them using the 
specified
+   * QueryAndResponseCombiner strategy, and sets the appropriate results and 
metadata in the
+   * CombinedQueryResponseBuilder.
+   *
+   * @param rb the ResponseBuilder object to process
+   * @throws IOException if an I/O error occurs during processing
+   */
+  @Override
+  public void process(ResponseBuilder rb) throws IOException {
+    if (rb instanceof CombinedQueryResponseBuilder crb) {
+      boolean partialResults = false;
+      boolean segmentTerminatedEarly = false;
+      boolean setMaxHitsTerminatedEarly = false;
+      List<QueryResult> queryResults = new ArrayList<>();
+      for (ResponseBuilder rbNow : crb.responseBuilders) {

Review Comment:
   I recommend only using "Now" for a temporal meaning.  Here, I suggest one of 
these possible names: `iRb` `thisRb`, or maybe rename the `rb` in scope 
allowing this one here to use simply `rb`.



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to