epugh commented on code in PR #4921:
URL: https://github.com/apache/solr/pull/4921#discussion_r4578602958


##########
changelog/unreleased/SOLR-18466.yml:
##########
@@ -0,0 +1,8 @@
+title: Support _route_ in the streaming expression function to prune shard 
fan-out

Review Comment:
   maybe be specific on which streaming expressoins?  This is for an end user 
to read over!



##########
solr/solrj-streaming/src/test/org/apache/solr/client/solrj/io/stream/StreamExpressionTest.java:
##########
@@ -428,6 +434,223 @@ public void testSqlStream() throws Exception {
     }
   }
 
+  @Test
+  public void testCloudSolrStreamWithRouteParam() throws Exception {
+    final String routeCollection = "routeCollection";
+    CollectionAdminRequest.createCollectionWithImplicitRouter(routeCollection, 
"conf", "a,b,c", 1)
+        .process(cluster.getSolrClient());
+    cluster.waitForActiveCollection(routeCollection, 3, 3);
+
+    StreamContext streamContext = new StreamContext();
+    SolrClientCache solrClientCache = new SolrClientCache();
+    streamContext.setSolrClientCache(solrClientCache);
+    try {
+      new UpdateRequest()
+          .add(id, "0", "a_i", "0")
+          .add(id, "1", "a_i", "1")
+          .add(id, "2", "a_i", "2")
+          .withRoute("a")
+          .process(cluster.getSolrClient(), routeCollection);
+      new UpdateRequest()
+          .add(id, "3", "a_i", "3")
+          .add(id, "4", "a_i", "4")
+          .withRoute("b")
+          .process(cluster.getSolrClient(), routeCollection);
+      new UpdateRequest()
+          .add(id, "5", "a_i", "5")
+          .withRoute("c")
+          .commit(cluster.getSolrClient(), routeCollection);
+
+      CloudSolrClient cloudSolrClient = cluster.getSolrClient();
+
+      assertEquals(Set.of("a"), routedSliceNames(routeCollection, 
cloudSolrClient, "a"));
+      assertEquals(Set.of("a", "c"), routedSliceNames(routeCollection, 
cloudSolrClient, "a,c"));
+      assertEquals(
+          3,
+          CloudSolrStream.getSlices(
+                  routeCollection, cloudSolrClient, true, new 
ModifiableSolrParams())
+              .size());
+
+      StreamFactory factory =
+          new StreamFactory()
+              .withDefaultSolrConnection(solrConnection)
+              .withCollectionUseThisConnection(routeCollection, 
solrConnection);
+
+      assertRouteQuery(routeCollection, factory, streamContext, "a", 1, new 
int[] {0, 1, 2});
+      assertRouteQuery(routeCollection, factory, streamContext, "a,c", 2, new 
int[] {0, 1, 2, 5});
+      assertRouteQuery(
+          routeCollection, factory, streamContext, null, 3, new int[] {0, 1, 
2, 3, 4, 5});
+    } finally {
+      
CollectionAdminRequest.deleteCollection(routeCollection).process(cluster.getSolrClient());
+      solrClientCache.close();
+    }
+  }
+
+  private Set<String> routedSliceNames(
+      String collection, CloudSolrClient cloudSolrClient, String routeKey) 
throws IOException {
+    ModifiableSolrParams params = new ModifiableSolrParams();
+    params.add(ShardParams._ROUTE_, routeKey);
+    return CloudSolrStream.getSlices(collection, cloudSolrClient, true, 
params).stream()
+        .map(Slice::getName)
+        .collect(Collectors.toSet());
+  }
+
+  private void assertRouteQuery(
+      String collection,
+      StreamFactory factory,
+      StreamContext streamContext,
+      String routeKey,
+      int expectedStreamCount,
+      int[] expectedIds)
+      throws Exception {
+    CloudSolrStream stream = routeStream(collection, factory, streamContext, 
routeKey);
+    List<Tuple> tuples = getTuples(stream);
+    assertEquals(expectedStreamCount, stream.children().size());
+    assertEquals(expectedIds.length, tuples.size());
+    assertOrderOf(tuples, "a_i", expectedIds);
+  }
+
+  private CloudSolrStream routeStream(
+      String collection, StreamFactory factory, StreamContext streamContext, 
String routeKey)
+      throws IOException {
+    String routeClause = routeKey == null ? "" : ", _route_=\"" + routeKey + 
"\"";
+    StreamExpression expression =
+        StreamExpressionParser.parse(
+            "search("
+                + collection
+                + ", q=*:*, fl=\"id,a_i\", sort=\"a_i asc\""
+                + routeClause
+                + ")");
+    CloudSolrStream stream = new CloudSolrStream(expression, factory);
+    stream.setStreamContext(streamContext);
+    return stream;
+  }
+
+  @Test
+  public void testCloudSolrStreamWithCompositeIdRoute() throws Exception {
+    new UpdateRequest()
+        .add(id, "user1!0", "a_i", "0")
+        .add(id, "user1!1", "a_i", "1")
+        .add(id, "user1!2", "a_i", "2")
+        .add(id, "user2!3", "a_i", "3")
+        .add(id, "user2!4", "a_i", "4")
+        .commit(cluster.getSolrClient(), COLLECTIONORALIAS);
+
+    CloudSolrClient cloudSolrClient = cluster.getSolrClient();
+    String realCollection =
+        
cloudSolrClient.getClusterStateProvider().resolveAlias(COLLECTIONORALIAS).get(0);
+    DocCollection docCollection = 
cloudSolrClient.getClusterState().getCollection(realCollection);
+    assertEquals(
+        "This test requires the compositeId router",
+        CompositeIdRouter.NAME,
+        docCollection.getRouter().getName());
+
+    ModifiableSolrParams routeParams = new ModifiableSolrParams();
+    routeParams.add(ShardParams._ROUTE_, "user1!");
+    Set<String> expected =
+        docCollection.getRouter().getSearchSlices("user1!", routeParams, 
docCollection).stream()
+            .map(Slice::getName)
+            .collect(Collectors.toSet());
+    Set<String> routedSliceNames =
+        CloudSolrStream.getSlices(COLLECTIONORALIAS, cloudSolrClient, true, 
routeParams).stream()
+            .map(Slice::getName)
+            .collect(Collectors.toSet());
+    assertEquals(
+        "getSlices should match the compositeId router's slice selection",
+        expected,
+        routedSliceNames);
+
+    StreamFactory factory =
+        new StreamFactory()
+            .withDefaultSolrConnection(solrConnection)
+            .withCollectionUseThisConnection(COLLECTIONORALIAS, 
solrConnection);
+    StreamContext streamContext = new StreamContext();
+    SolrClientCache solrClientCache = new SolrClientCache();
+    streamContext.setSolrClientCache(solrClientCache);
+    try {
+      StreamExpression expression =
+          StreamExpressionParser.parse(
+              "search("
+                  + COLLECTIONORALIAS
+                  + ", q=*:*, fq=\"id:user1!*\", fl=\"id,a_i\", sort=\"a_i 
asc\", _route_=\"user1!\")");

Review Comment:
   Ironically, in https://github.com/apache/solr/pull/5319 we are discussing if 
`_route_` should just be `route`....!    



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