This is an automated email from the ASF dual-hosted git repository.

dsmiley pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/solr.git

commit b02aab928f38dc170d8e858f1565feb459d7b623
Author: Sonu Sharma <[email protected]>
AuthorDate: Fri Apr 24 00:21:21 2026 -0400

    SOLR-18093: Add 'queries' support to JsonQueryRequest (#4276)
    
    Add top-level "queries" support to JsonQueryRequest in SolrJ.
    
    And hardened a related flaky test.
    SearchHandler: give a clear error if referring to a non-existent 
SearchComponent
    
    Co-authored-by: David Smiley <[email protected]>
    
    (cherry picked from commit 36d8f54969988aa5674af5d38ef3e3241535fe29)
---
 ...R-18093-add-top-level-queries-in-json-solrj.yml |  9 +++
 .../solr/handler/component/SearchHandler.java      |  4 ++
 .../solr/collection1/conf/solrconfig.xml           |  3 +
 .../component/CombinedQuerySolrCloudTest.java      | 22 +++----
 .../conf/solrconfig.xml                            |  9 ++-
 .../query-guide/examples/JsonRequestApiTest.java   | 70 ++++++++++++++++++++++
 .../query-guide/pages/json-combined-query-dsl.adoc | 41 +++++++++++--
 .../modules/query-guide/pages/json-query-dsl.adoc  | 15 +++++
 .../solrj/request/json/JsonQueryRequest.java       | 20 +++++++
 .../json/JsonQueryRequestIntegrationTest.java      | 25 ++++++++
 .../request/json/JsonQueryRequestUnitTest.java     | 23 +++++++
 11 files changed, 223 insertions(+), 18 deletions(-)

diff --git 
a/changelog/unreleased/SOLR-18093-add-top-level-queries-in-json-solrj.yml 
b/changelog/unreleased/SOLR-18093-add-top-level-queries-in-json-solrj.yml
new file mode 100644
index 00000000000..0c91f5aa11b
--- /dev/null
+++ b/changelog/unreleased/SOLR-18093-add-top-level-queries-in-json-solrj.yml
@@ -0,0 +1,9 @@
+# See https://github.com/apache/solr/blob/main/dev-docs/changelog.adoc
+title: Add top-level "queries" support to JsonQueryRequest in SolrJ
+type: added
+authors:
+  - name: Sonu Sharma
+    nick: ercsonusharma
+links:
+  - name: SOLR-18093
+    url: https://issues.apache.org/jira/browse/SOLR-18093
diff --git 
a/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java 
b/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java
index 10ef832d924..ed33c64ec72 100644
--- a/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java
+++ b/solr/core/src/java/org/apache/solr/handler/component/SearchHandler.java
@@ -306,6 +306,10 @@ public class SearchHandler extends RequestHandlerBase
     DebugComponent dbgCmp = null;
     for (String c : list) {
       SearchComponent comp = core.getSearchComponent(c);
+      if (comp == null) {
+        throw new SolrException(
+            SolrException.ErrorCode.SERVER_ERROR, "Unknown search component: " 
+ c);
+      }
       if (comp instanceof DebugComponent && makeDebugLast == true) {
         dbgCmp = (DebugComponent) comp;
       } else {
diff --git a/solr/core/src/test-files/solr/collection1/conf/solrconfig.xml 
b/solr/core/src/test-files/solr/collection1/conf/solrconfig.xml
index 856079f329e..aefe2b43bd7 100644
--- a/solr/core/src/test-files/solr/collection1/conf/solrconfig.xml
+++ b/solr/core/src/test-files/solr/collection1/conf/solrconfig.xml
@@ -386,6 +386,9 @@
       </arr>
  </requestHandler>
 
+  <searchComponent class="solr.CombinedQueryComponent" name="combined_query">
+  </searchComponent>
+
   <requestHandler name="/mltrh" 
class="org.apache.solr.handler.component.SearchHandler">
 
   </requestHandler>
diff --git 
a/solr/core/src/test/org/apache/solr/handler/component/CombinedQuerySolrCloudTest.java
 
b/solr/core/src/test/org/apache/solr/handler/component/CombinedQuerySolrCloudTest.java
index 605a83408ed..1173e2cf51e 100644
--- 
a/solr/core/src/test/org/apache/solr/handler/component/CombinedQuerySolrCloudTest.java
+++ 
b/solr/core/src/test/org/apache/solr/handler/component/CombinedQuerySolrCloudTest.java
@@ -73,7 +73,7 @@ public class CombinedQuerySolrCloudTest extends 
AbstractFullDistribZkTestBase {
     del("*:*");
     List<SolrInputDocument> docs = getSolrDocuments();
     for (SolrInputDocument doc : docs) {
-      doc.setField("id", doc.getFieldValue("mod3_idv") + "!" + 
doc.getField("id").getValue());
+      doc.setField("id", "CO!" + doc.getField("id").getValue());
     }
     for (SolrInputDocument doc : docs) {
       indexDoc(doc);
@@ -280,29 +280,29 @@ public class CombinedQuerySolrCloudTest extends 
AbstractFullDistribZkTestBase {
     prepareIndexDocsColocated();
     String jsonQuery =
         "{\"queries\": {"
-            + "\"lexical1\": {\"lucene\": {\"query\": \"id:(2!2^2 OR 0!3^1 OR 
0!6^2 OR 2!5^1)\"}},"
-            + "\"lexical2\": {\"lucene\": {\"query\": \"id:(2!8^1 OR 2!5^2 OR 
1!7^3 OR 1!10^2)\"}}"
+            + "\"lexical1\": {\"lucene\": {\"query\": \"id:(CO!2^3 OR CO!3^1 
OR CO!6^2 OR CO!5^1)\"}},"
+            + "\"lexical2\": {\"lucene\": {\"query\": \"id:(CO!8^1 OR CO!5^2 
OR CO!7^3 OR CO!10^2)\"}}"
             + "},"
-            + "\"limit\": 1,"
+            + "\"limit\": 3,"
             + "\"fields\": [\"id\", \"score\", \"title\"],"
             + "\"params\": {\"combiner\": true, \"facet\": true, 
\"facet.field\": \"id\","
-            + "\"fq\": [\"{!collapse field=mod3_idv sort='id asc'}\"],"
+            + "\"fq\": [\"{!collapse field=mod3_idv sort='id asc, score 
desc'}\"],"
             + "\"expand\": true, \"expand.q\": \"*:*\","
             + "\"combiner.query\": [\"lexical1\", \"lexical2\"],"
             + "\"hl\": true, \"hl.fl\": \"title\", \"hl.q\": \"test doc\"}}";
     handle.put("expanded", UNORDERED);
     QueryResponse rsp = query(CommonParams.JSON, jsonQuery, CommonParams.QT, 
"/search");
-    assertEquals(1, rsp.getResults().size());
-    assertFieldValues(rsp.getResults(), id, "2!2");
+    assertEquals(3, rsp.getResults().size());
+    assertFieldValues(rsp.getResults(), id, "CO!2", "CO!10", "CO!3");
     assertEquals("id", rsp.getFacetFields().get(0).getName());
     assertEquals(
-        "[0!3 (1), 1!10 (1), 2!2 (1), 0!6 (0), 0!9 (0), 1!1 (0), 1!4 (0), 1!7 
(0), 2!5 (0), 2!8 (0)]",
+        "[CO!10 (1), CO!2 (1), CO!3 (1), CO!1 (0), CO!4 (0), CO!5 (0), CO!6 
(0), CO!7 (0), CO!8 (0), CO!9 (0)]",
         rsp.getFacetFields().get(0).getValues().toString());
-    assertEquals(1, rsp.getHighlighting().size());
+    assertEquals(3, rsp.getHighlighting().size());
     assertEquals(
         "title <em>test</em> for <em>doc</em> 2",
-        rsp.getHighlighting().get("2!2").get("title").get(0));
-    assertEquals(1, rsp.getExpandedResults().size());
+        rsp.getHighlighting().get("CO!2").get("title").get(0));
+    assertEquals(3, rsp.getExpandedResults().size());
   }
 
   /** To test that we can force distrib */
diff --git 
a/solr/server/solr/configsets/sample_techproducts_configs/conf/solrconfig.xml 
b/solr/server/solr/configsets/sample_techproducts_configs/conf/solrconfig.xml
index 3f90003e635..55e979b7a72 100644
--- 
a/solr/server/solr/configsets/sample_techproducts_configs/conf/solrconfig.xml
+++ 
b/solr/server/solr/configsets/sample_techproducts_configs/conf/solrconfig.xml
@@ -654,7 +654,14 @@
      </lst>
   </requestHandler>
 
-  <initParams path="/update/**,/query,/select,/tvrh,/elevate,/spell,update">
+  <requestHandler name="/rrf" class="solr.CombinedQuerySearchHandler">
+  </requestHandler>
+
+  <searchComponent class="solr.CombinedQueryComponent" name="combined_query">
+    <int name="maxCombinerQueries">2</int>
+  </searchComponent>
+
+  <initParams 
path="/update/**,/query,/search,/select,/tvrh,/elevate,/spell,update">
     <lst name="defaults">
       <str name="df">text</str>
     </lst>
diff --git 
a/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiTest.java 
b/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiTest.java
index ee338ffa2dd..892ce478d36 100644
--- a/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiTest.java
+++ b/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiTest.java
@@ -87,6 +87,76 @@ public class JsonRequestApiTest extends SolrCloudTestCase {
     assertResponseFoundNumDocs(queryResponse, expectedResults);
   }
 
+  /**
+   * Test json query behaviour in case of multiple query to be executed using 
Combined Query.
+   *
+   * @throws Exception the exception
+   */
+  @Test
+  public void testSimpleJsonQueryWithQueriesParams() throws Exception {
+    SolrClient solrClient = cluster.getSolrClient();
+    final int expectedResults = 2;
+    final Map<String, Object> queriesMap = new HashMap<>();
+    queriesMap.put(
+        "query1",
+        Map.of(
+            "lucene",
+            Map.of(
+                "query", "apache",
+                "df", "manu")));
+    queriesMap.put(
+        "query2",
+        Map.of(
+            "edismax",
+            Map.of(
+                "query", "solr",
+                "df", "name")));
+    final JsonQueryRequest query =
+        new JsonQueryRequest()
+            .setQueries(queriesMap)
+            .withFilter("inStock:true")
+            .withParam("fl", "name")
+            .withParam("combiner", "true")
+            .withParam("combiner.query", List.of("query1", "query2"));
+    query.setPath("/rrf");
+    QueryResponse queryResponse = query.process(solrClient, COLLECTION_NAME);
+    assertResponseFoundNumDocs(queryResponse, expectedResults);
+  }
+
+  /**
+   * Test json query behaviour in case of multiple query to be executed using 
Additional Queries.
+   *
+   * @throws Exception the exception
+   */
+  @Test
+  public void testAdditionalJsonQueries() throws Exception {
+    SolrClient solrClient = cluster.getSolrClient();
+    final int expectedResults = 12;
+    // tag::solrj-json-query-with-queries[]
+    final Map<String, Object> queriesMap = new HashMap<>();
+    queriesMap.put(
+        "electronic",
+        Map.of(
+            "field",
+            Map.of(
+                "query", "electronics",
+                "f", "cat")));
+    queriesMap.put(
+        "manufacturers",
+        List.of(
+            "manu: apple",
+            Map.of(
+                "field",
+                Map.of(
+                    "query", "belkin",
+                    "f", "manu"))));
+    final JsonQueryRequest query =
+        new JsonQueryRequest().setQueries(queriesMap).setQuery(Map.of("param", 
"electronic"));
+    QueryResponse queryResponse = query.process(solrClient, COLLECTION_NAME);
+    // end::solrj-json-query-with-queries[]
+    assertEquals(expectedResults, queryResponse.getResults().getNumFound());
+  }
+
   @Test
   public void testJsonQueryWithJsonQueryParamOverrides() throws Exception {
     SolrClient solrClient = cluster.getSolrClient();
diff --git 
a/solr/solr-ref-guide/modules/query-guide/pages/json-combined-query-dsl.adoc 
b/solr/solr-ref-guide/modules/query-guide/pages/json-combined-query-dsl.adoc
index 98d3c2e96b8..e5649f0cad2 100644
--- a/solr/solr-ref-guide/modules/query-guide/pages/json-combined-query-dsl.adoc
+++ b/solr/solr-ref-guide/modules/query-guide/pages/json-combined-query-dsl.adoc
@@ -42,14 +42,21 @@ The query structure is similar to JSON Query DSL except for 
how multiple queries
 
 === Example
 
-Below is a sample JSON query payload:
+Below is an example with sample JSON query payload:
 
-```
+[tabs#json-query-with-queries]
+======
+curl::
++
+====
+[source,bash]
+----
+curl -X POST http://localhost:8983/solr/techproducts/query -d '
 {
     "queries": {
         "lexical1": {
             "lucene": {
-                "query": "title:sales"
+                "query": "name:apache"
             }
         },
         "vector": {
@@ -61,15 +68,37 @@ Below is a sample JSON query payload:
         }
     },
     "limit": 5,
-    "fields": ["id", "score", "title"],
+    "fields": ["id", "score", "name"],
     "params": {
         "combiner": true,
         "combiner.query": ["lexical1", "vector"],
         "combiner.algorithm": "rrf",
         "combiner.rrf.k": "15"
     }
-}
-```
+}'
+----
+====
+SolrJ::
++
+====
+[source,java,indent=0]
+----
+final Map<String, Object> queriesMap = new HashMap<>();
+queriesMap.put("lexical1", Map.of("lucene", Map.of("query", "apache", "df", 
"name")));
+queriesMap.put("vector", Map.of("knn", Map.of("query", [0.1,-0.34,0.89,0.02], 
"f", "vector", "topK", 5)));
+final JsonQueryRequest query =
+    new JsonQueryRequest()
+        .setQueries(queriesMap)
+        .withParam("id", "score", "name")
+        .setLimit(5)
+        .withParam("combiner", "true")
+        .withParam("combiner.query", List.of("lexical1", "vector"))
+        .withParam("combiner.algorithm", "rrf")
+        .withParam("combiner.rrf.k", "15");
+QueryResponse queryResponse = query.process(solrClient, COLLECTION_NAME);
+----
+====
+======
 
 == Search Handler Configuration
 
diff --git a/solr/solr-ref-guide/modules/query-guide/pages/json-query-dsl.adoc 
b/solr/solr-ref-guide/modules/query-guide/pages/json-query-dsl.adoc
index 104fba52eec..a52300aac34 100644
--- a/solr/solr-ref-guide/modules/query-guide/pages/json-query-dsl.adoc
+++ b/solr/solr-ref-guide/modules/query-guide/pages/json-query-dsl.adoc
@@ -388,6 +388,11 @@ Beware of arity for these references.
 Depending on the context, a reference might be resolved into the first element 
in the array ignoring the later elements, e.g., if one changes the reference 
below from `{"param":"electronic"}` to `{"param":"manufacturers"}`, it's 
equivalent to querying for `manu:apple`, ignoring the later query.
 These queries don't impact the query result until explicit referencing.
 
+[tabs#json-additional-queries]
+======
+curl::
++
+====
 [source,bash]
 ----
 curl -X POST http://localhost:8983/solr/techproducts/query -d '
@@ -402,6 +407,16 @@ curl -X POST http://localhost:8983/solr/techproducts/query 
-d '
     "query":{"param":"electronic"}
 }'
 ----
+====
+SolrJ::
++
+====
+[source,java,indent=0]
+----
+include::example$JsonRequestApiTest.java[tag=solrj-json-query-with-queries]
+----
+====
+======
 
 Overall this example doesn't make much sense, but just demonstrates the syntax.
 This feature is useful in 
xref:json-faceting-domain-changes.adoc#adding-domain-filters[filtering domain] 
in JSON Facet API xref:json-facet-api.adoc#changing-the-domain[domain changes].
diff --git 
a/solr/solrj/src/java/org/apache/solr/client/solrj/request/json/JsonQueryRequest.java
 
b/solr/solrj/src/java/org/apache/solr/client/solrj/request/json/JsonQueryRequest.java
index 421edc72bc8..29ef7d84539 100644
--- 
a/solr/solrj/src/java/org/apache/solr/client/solrj/request/json/JsonQueryRequest.java
+++ 
b/solr/solrj/src/java/org/apache/solr/client/solrj/request/json/JsonQueryRequest.java
@@ -101,6 +101,26 @@ public class JsonQueryRequest extends QueryRequest {
     return this;
   }
 
+  /**
+   * Specify the queries parameter sent as a part of JSON request.
+   *
+   * <p>This method would be helpful in setting the queries parameter 
specially for Combined Query
+   * Component {@code CombinedQueryComponent} use case.
+   *
+   * <p><b>Example:</b> You wish to send the JSON request:
+   *
+   * <pre>{@code {'limit': 5, 'queries': {'query1': {'lucene':
+   * {'df':'genre_s', 'query': 'scifi'}}}, 'query2': {'knn': {'f': 'vector', 
'query': [0.1, 0.43]}}}}
+   * </pre>
+   *
+   * @param queriesJson a Map of values representing the query subtree of the 
JSON request you wish
+   *     to send.
+   */
+  public JsonQueryRequest setQueries(Map<String, Object> queriesJson) {
+    jsonRequestMap.put("queries", queriesJson);
+    return this;
+  }
+
   /**
    * Specify the query sent as a part of this JSON request.
    *
diff --git 
a/solr/solrj/src/test/org/apache/solr/client/solrj/request/json/JsonQueryRequestIntegrationTest.java
 
b/solr/solrj/src/test/org/apache/solr/client/solrj/request/json/JsonQueryRequestIntegrationTest.java
index 203f69189ce..f481c262049 100644
--- 
a/solr/solrj/src/test/org/apache/solr/client/solrj/request/json/JsonQueryRequestIntegrationTest.java
+++ 
b/solr/solrj/src/test/org/apache/solr/client/solrj/request/json/JsonQueryRequestIntegrationTest.java
@@ -21,6 +21,7 @@ import java.io.File;
 import java.io.IOException;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
 import org.apache.solr.client.solrj.request.CollectionAdminRequest;
@@ -46,6 +47,7 @@ public class JsonQueryRequestIntegrationTest extends 
SolrCloudTestCase {
   private static final int NUM_SCIFI_BOOKS = 2;
   private static final int NUM_IN_STOCK = 8;
   private static final int NUM_IN_STOCK_AND_FIRST_IN_SERIES = 5;
+  private static final int NUM_MARTIN_BOOKS = 3;
 
   @BeforeClass
   public static void setupCluster() throws Exception {
@@ -91,6 +93,29 @@ public class JsonQueryRequestIntegrationTest extends 
SolrCloudTestCase {
     assertEquals(NUM_SCIFI_BOOKS, queryResponse.getResults().getNumFound());
   }
 
+  /**
+   * Test multiple queries can use local params syntax with Combined Query 
Component.
+   *
+   * @throws Exception the exception
+   */
+  @Test
+  public void testMultipleQueriesCanUseLocalParamsSyntax() throws Exception {
+    final Map<String, Object> queriesMap = new HashMap<>();
+    queriesMap.put("query1", "{!lucene df=genre_s v='scifi'}");
+    queriesMap.put("query2", "{!edismax df=author_t v='martin'}");
+    final JsonQueryRequest query =
+        new JsonQueryRequest()
+            .setQueries(queriesMap)
+            .withFilter("inStock:true")
+            .withParam("fl", "name")
+            .withParam("combiner", "true")
+            .withParam("combiner.query", List.of("query1", "query2"));
+    query.setPath("/rrf");
+    QueryResponse queryResponse = query.process(cluster.getSolrClient(), 
COLLECTION_NAME);
+    assertEquals(0, queryResponse.getStatus());
+    assertEquals(NUM_SCIFI_BOOKS + NUM_MARTIN_BOOKS, 
queryResponse.getResults().size());
+  }
+
   @Test
   public void testQueriesCanUseExpandedSyntax() throws Exception {
     // Construct a tree representing the JSON: {lucene: {df:'genre_s', 
'query': 'scifi'}}
diff --git 
a/solr/solrj/src/test/org/apache/solr/client/solrj/request/json/JsonQueryRequestUnitTest.java
 
b/solr/solrj/src/test/org/apache/solr/client/solrj/request/json/JsonQueryRequestUnitTest.java
index 6297c56cd6c..51a407ce526 100644
--- 
a/solr/solrj/src/test/org/apache/solr/client/solrj/request/json/JsonQueryRequestUnitTest.java
+++ 
b/solr/solrj/src/test/org/apache/solr/client/solrj/request/json/JsonQueryRequestUnitTest.java
@@ -88,6 +88,29 @@ public class JsonQueryRequestUnitTest extends SolrTestCase {
         requestBody, 
containsString("\"query\":{\"lucene\":{\"q\":\"*:*\",\"df\":\"text\"}}"));
   }
 
+  @Test
+  public void testWritesProvidedQueriesMapToJsonCorrectly() {
+    final Map<String, Object> queriesMap = new HashMap<>();
+    queriesMap.put(
+        "query1",
+        Map.of(
+            "lucene",
+            Map.of(
+                "query", "*:*",
+                "df", "text")));
+    queriesMap.put(
+        "query2",
+        Map.of(
+            "edismax",
+            Map.of(
+                "query", "solr",
+                "df", "text")));
+    final JsonQueryRequest request = new 
JsonQueryRequest().setQueries(queriesMap);
+    final String requestBody = writeRequestToJson(request);
+    assertThat(requestBody, containsString("\"query1\":{\"lucene\":"));
+    assertThat(requestBody, containsString("\"query2\":{\"edismax\":"));
+  }
+
   @Test
   public void testWritesProvidedQueryMapWriterToJsonCorrectly() {
     final MapWriter queryWriter =

Reply via email to