epotyom commented on code in PR #13568:
URL: https://github.com/apache/lucene/pull/13568#discussion_r1710265556


##########
lucene/facet/src/java/org/apache/lucene/facet/DrillSideways.java:
##########
@@ -349,45 +344,142 @@ private DrillDownQuery getDrillDownQuery(
   public <R> ConcurrentDrillSidewaysResult<R> search(
       final DrillDownQuery query, final CollectorManager<?, R> 
hitCollectorManager)
       throws IOException {
+    // Main query
+    FacetsCollectorManager drillDownFacetsCollectorManager =
+        createDrillDownFacetsCollectorManager();
+    final CollectorOwner<?, ?> mainCollectorOwner;
+    if (drillDownFacetsCollectorManager != null) {
+      // Make sure we populate a facet collector corresponding to the base 
query if desired:
+      mainCollectorOwner =
+          new CollectorOwner<>(
+              new MultiCollectorManager(drillDownFacetsCollectorManager, 
hitCollectorManager));
+    } else {
+      mainCollectorOwner = new CollectorOwner<>(hitCollectorManager);
+    }
+    // Drill sideways dimensions
+    final List<CollectorOwner<?, ?>> drillSidewaysCollectorOwners;
+    if (query.getDims().isEmpty() == false) {
+      drillSidewaysCollectorOwners = new ArrayList<>(query.getDims().size());
+      for (int i = 0; i < query.getDims().size(); i++) {
+        drillSidewaysCollectorOwners.add(
+            new CollectorOwner<>(createDrillSidewaysFacetsCollectorManager()));
+      }
+    } else {
+      drillSidewaysCollectorOwners = null;
+    }
+    // Execute query
     if (executor != null) {
-      return searchConcurrently(query, hitCollectorManager);
+      searchConcurrently(query, mainCollectorOwner, 
drillSidewaysCollectorOwners);
     } else {
-      return searchSequentially(query, hitCollectorManager);
+      searchSequentially(query, mainCollectorOwner, 
drillSidewaysCollectorOwners);
     }
+
+    // Collect results
+    final FacetsCollector facetsCollectorResult;
+    final R hitCollectorResult;
+    if (drillDownFacetsCollectorManager != null) {
+      // drill down collected using MultiCollector
+      // Extract the results:
+      Object[] drillDownResult = (Object[]) mainCollectorOwner.getResult();
+      facetsCollectorResult = (FacetsCollector) drillDownResult[0];
+      hitCollectorResult = (R) drillDownResult[1];
+    } else {
+      facetsCollectorResult = null;
+      hitCollectorResult = (R) mainCollectorOwner.getResult();
+    }
+
+    // Getting results for drill sideways dimensions (if any)
+    final String[] drillSidewaysDims;
+    final FacetsCollector[] drillSidewaysCollectors;
+    if (query.getDims().isEmpty() == false) {
+      drillSidewaysDims = query.getDims().keySet().toArray(new String[0]);
+      int numDims = query.getDims().size();
+      assert drillSidewaysCollectorOwners != null;
+      assert drillSidewaysCollectorOwners.size() == numDims;
+      drillSidewaysCollectors = new FacetsCollector[numDims];
+      for (int dim = 0; dim < numDims; dim++) {
+        drillSidewaysCollectors[dim] =
+            (FacetsCollector) 
drillSidewaysCollectorOwners.get(dim).getResult();
+      }
+    } else {
+      drillSidewaysDims = null;
+      drillSidewaysCollectors = null;
+    }
+
+    return new ConcurrentDrillSidewaysResult<>(
+        buildFacetsResult(facetsCollectorResult, drillSidewaysCollectors, 
drillSidewaysDims),
+        null,
+        hitCollectorResult,
+        facetsCollectorResult,
+        drillSidewaysCollectors,
+        drillSidewaysDims);
   }
 
-  @SuppressWarnings("unchecked")
-  private <R> ConcurrentDrillSidewaysResult<R> searchSequentially(
-      final DrillDownQuery query, final CollectorManager<?, R> 
hitCollectorManager)
+  /**
+   * Search using DrillDownQuery with custom collectors. This method can be 
used with any {@link
+   * CollectorOwner}s. It doesn't return anything because it is expected that 
you read results from
+   * provided {@link CollectorOwner}s.
+   *
+   * <p>To read the results, run {@link CollectorOwner#getResult()} for drill 
down and all drill
+   * sideways dimensions.
+   *
+   * <p>Note: use {@link Collections#unmodifiableList(List)} to wrap {@code
+   * drillSidewaysCollectorOwners} to convince compiler that it is safe to use 
List here.
+   *
+   * <p>TODO: Class CollectorOwner was created so that we can ignore 
CollectorManager type C,
+   * because we want each dimensions to be able to use their own types. 
Alternatively, we can use
+   * typesafe heterogeneous container and provide CollectorManager type for 
each dimension to this
+   * method? I do like CollectorOwner approach as it seems more intuitive?
+   */
+  public void search(
+      final DrillDownQuery query,
+      CollectorOwner<?, ?> drillDownCollectorOwner,

Review Comment:
   I'll add a note to the javadoc!



-- 
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: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to