This is an automated email from the ASF dual-hosted git repository.
epugh pushed a commit to branch branch_10x
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/branch_10x by this push:
new f00adae954e SOLR-11574: Use executable java code in ref guide
examples. (#3915)
f00adae954e is described below
commit f00adae954e6f1da6c7fb92718d0206046cfc96f
Author: Eric Pugh <[email protected]>
AuthorDate: Thu Dec 18 09:12:07 2025 -0500
SOLR-11574: Use executable java code in ref guide examples. (#3915)
Introduces some more sophisticated ways of verifying the output of the
sample java code.
Establishes some consistency in formatting and how we output.
Update to more modern Java practices.
---
solr/solr-ref-guide/build.gradle | 2 +
solr/solr-ref-guide/gradle.lockfile | 17 ++-
.../examples/IndexingNestedDocuments.java | 3 +-
.../examples/SolrCellRefGuideExamplesTest.java | 91 +++++++++++++++
.../indexing-guide/pages/indexing-with-tika.adoc | 11 +-
.../examples/ExpectedOutputVerifier.java | 57 +++++++++
.../examples/PaginationRefGuideExamplesTest.java | 129 +++++++++++++++++++++
.../examples/ParallelSqlRefGuideExamplesTest.java | 121 +++++++++++++++++++
.../TermComponentRefGuideExamplesTest.java | 128 ++++++++++++++++++++
.../query-guide/pages/pagination-of-results.adoc | 14 +--
.../modules/query-guide/pages/sql-query.adoc | 16 +--
.../modules/query-guide/pages/terms-component.adoc | 12 +-
12 files changed, 549 insertions(+), 52 deletions(-)
diff --git a/solr/solr-ref-guide/build.gradle b/solr/solr-ref-guide/build.gradle
index 4dced1fdacc..2893ab70b4d 100644
--- a/solr/solr-ref-guide/build.gradle
+++ b/solr/solr-ref-guide/build.gradle
@@ -546,8 +546,10 @@ dependencies {
// For the SolrJ examples
testImplementation project(":solr:core")
testImplementation project(":solr:solrj")
+ testRuntimeOnly project(":solr:modules:sql")
testImplementation project(":solr:test-framework")
testImplementation libs.junit.junit
+ testImplementation libs.apache.lucene.testframework
}
// Copy all the test resource files from SolrJ to the build/resources/test
directory
diff --git a/solr/solr-ref-guide/gradle.lockfile
b/solr/solr-ref-guide/gradle.lockfile
index dd92b13ba95..b4fc8293e4f 100644
--- a/solr/solr-ref-guide/gradle.lockfile
+++ b/solr/solr-ref-guide/gradle.lockfile
@@ -25,7 +25,8 @@
com.google.guava:failureaccess:1.0.3=annotationProcessor,errorprone,testAnnotati
com.google.guava:guava:33.4.8-jre=annotationProcessor,errorprone,testAnnotationProcessor,testCompileClasspath,testRuntimeClasspath
com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava=annotationProcessor,errorprone,testAnnotationProcessor,testCompileClasspath,testRuntimeClasspath
com.google.j2objc:j2objc-annotations:3.1=annotationProcessor,errorprone,testAnnotationProcessor,testCompileClasspath,testRuntimeClasspath
-com.google.protobuf:protobuf-java:3.25.8=annotationProcessor,errorprone,testAnnotationProcessor
+com.google.protobuf:protobuf-java:3.25.8=annotationProcessor,errorprone,testAnnotationProcessor,testRuntimeClasspath
+com.googlecode.json-simple:json-simple:1.1.1=testRuntimeClasspath
com.j256.simplemagic:simplemagic:1.17=testRuntimeClasspath
com.jayway.jsonpath:json-path:2.9.0=testRuntimeClasspath
com.tdunning:t-digest:3.3=testRuntimeClasspath
@@ -75,12 +76,20 @@ jakarta.ws.rs:jakarta.ws.rs-api:3.1.0=testRuntimeClasspath
javax.inject:javax.inject:1=annotationProcessor,errorprone,testAnnotationProcessor
junit:junit:4.13.2=testCompileClasspath,testRuntimeClasspath
org.antlr:antlr4-runtime:4.13.2=testRuntimeClasspath
+org.apache.calcite.avatica:avatica-core:1.25.0=testRuntimeClasspath
+org.apache.calcite.avatica:avatica-metrics:1.25.0=testRuntimeClasspath
+org.apache.calcite:calcite-core:1.37.0=testRuntimeClasspath
+org.apache.calcite:calcite-linq4j:1.37.0=testRuntimeClasspath
org.apache.commons:commons-exec:1.5.0=testRuntimeClasspath
org.apache.commons:commons-lang3:3.20.0=testRuntimeClasspath
org.apache.commons:commons-math3:3.6.1=testRuntimeClasspath
+org.apache.commons:commons-text:1.15.0=testRuntimeClasspath
org.apache.curator:curator-client:5.9.0=testCompileClasspath,testRuntimeClasspath
org.apache.curator:curator-framework:5.9.0=testCompileClasspath,testRuntimeClasspath
org.apache.curator:curator-test:5.9.0=testRuntimeClasspath
+org.apache.httpcomponents.client5:httpclient5:5.2.1=testRuntimeClasspath
+org.apache.httpcomponents.core5:httpcore5-h2:5.2=testRuntimeClasspath
+org.apache.httpcomponents.core5:httpcore5:5.2.3=testRuntimeClasspath
org.apache.httpcomponents:httpclient:4.5.14=testRuntimeClasspath
org.apache.httpcomponents:httpcore:4.4.16=testRuntimeClasspath
org.apache.httpcomponents:httpmime:4.5.14=testRuntimeClasspath
@@ -113,6 +122,9 @@
org.apache.lucene:lucene-test-framework:10.3.2=testCompileClasspath,testRuntimeC
org.apache.zookeeper:zookeeper-jute:3.9.4=testCompileClasspath,testRuntimeClasspath
org.apache.zookeeper:zookeeper:3.9.4=testCompileClasspath,testRuntimeClasspath
org.apiguardian:apiguardian-api:1.1.2=testRuntimeClasspath
+org.checkerframework:checker-qual:3.44.0=testRuntimeClasspath
+org.codehaus.janino:commons-compiler:3.1.11=testRuntimeClasspath
+org.codehaus.janino:janino:3.1.11=testRuntimeClasspath
org.codehaus.woodstox:stax2-api:4.2.2=testRuntimeClasspath
org.eclipse.jetty.ee10:jetty-ee10-servlet:12.0.27=testCompileClasspath,testRuntimeClasspath
org.eclipse.jetty.http2:jetty-http2-client-transport:12.0.27=testRuntimeClasspath
@@ -150,6 +162,9 @@
org.jspecify:jspecify:1.0.0=annotationProcessor,errorprone,testAnnotationProcess
org.junit.jupiter:junit-jupiter-api:5.6.2=testRuntimeClasspath
org.junit.platform:junit-platform-commons:1.6.2=testRuntimeClasspath
org.junit:junit-bom:5.6.2=testRuntimeClasspath
+org.locationtech.jts.io:jts-io-common:1.19.0=testRuntimeClasspath
+org.locationtech.jts:jts-core:1.19.0=testRuntimeClasspath
+org.locationtech.proj4j:proj4j:1.2.2=testRuntimeClasspath
org.locationtech.spatial4j:spatial4j:0.8=testRuntimeClasspath
org.opentest4j:opentest4j:1.2.0=testRuntimeClasspath
org.ow2.asm:asm-commons:9.8=testRuntimeClasspath
diff --git
a/solr/solr-ref-guide/modules/indexing-guide/examples/IndexingNestedDocuments.java
b/solr/solr-ref-guide/modules/indexing-guide/examples/IndexingNestedDocuments.java
index 21bc9bb3f9e..1310d65185d 100644
---
a/solr/solr-ref-guide/modules/indexing-guide/examples/IndexingNestedDocuments.java
+++
b/solr/solr-ref-guide/modules/indexing-guide/examples/IndexingNestedDocuments.java
@@ -144,8 +144,7 @@ public class IndexingNestedDocuments extends
SolrCloudTestCase {
assertEquals(1, docs.getNumFound());
assertEquals("P11!prod", docs.get(0).getFieldValue("id"));
- // [child] returns a flat list of all (anon) descendents
- assertEquals(5, docs.get(0).getChildDocumentCount());
+ // [child] returns a flat list of all (anon) descendants
assertEquals(5, docs.get(0).getChildDocuments().size());
// flat list is depth first...
diff --git
a/solr/solr-ref-guide/modules/indexing-guide/examples/SolrCellRefGuideExamplesTest.java
b/solr/solr-ref-guide/modules/indexing-guide/examples/SolrCellRefGuideExamplesTest.java
new file mode 100644
index 00000000000..14d6b40de29
--- /dev/null
+++
b/solr/solr-ref-guide/modules/indexing-guide/examples/SolrCellRefGuideExamplesTest.java
@@ -0,0 +1,91 @@
+/*
+ * 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.client.ref_guide_examples;
+
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.clear;
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.ensureNoLeftoverOutputExpectations;
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.print;
+
+import java.nio.file.Path;
+import java.util.List;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.SolrException;
+import org.apache.solr.common.util.NamedList;
+import org.apache.solr.util.ExternalPaths;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Example SolrJ usage for Solr Cell (Apache Tika) PDF extraction.
+ *
+ * <p>Snippets surrounded by "tag" and "end" comments are extracted and used
in the Solr Reference
+ * Guide.
+ */
+public class SolrCellRefGuideExamplesTest extends SolrCloudTestCase {
+ private static final String PDF_DOC_PATH =
+ ExternalPaths.TECHPRODUCTS_CONFIGSET +
"/../../../../../example/exampledocs/solr-word.pdf";
+ private static final String COLLECTION_NAME = "techproducts";
+ private static final String CONFIG_NAME = "extraction_config";
+
+ @BeforeClass
+ public static void setupCluster() throws Exception {
+ configureCluster(1).addConfig(CONFIG_NAME,
ExternalPaths.TECHPRODUCTS_CONFIGSET).configure();
+
+ CollectionAdminRequest.createCollection(COLLECTION_NAME, CONFIG_NAME, 1, 1)
+ .process(cluster.getSolrClient());
+ }
+
+ @Before
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+
+ clear();
+ }
+
+ @Test
+ public void testSolrCellPdfExtraction() throws Exception {
+ // expectLine("Update status was: 0");
+
+ // Best we can do without including extraction module
+ assertExceptionThrownWithMessageContaining(
+ SolrException.class,
+ List.of("Error loading class
'solr.extraction.ExtractingRequestHandler'"),
+ () -> {
+ // tag::solr-cell-pdf-extraction[]
+ ContentStreamUpdateRequest extractRequest =
+ new ContentStreamUpdateRequest("/update/extract");
+ extractRequest.addFile(Path.of(PDF_DOC_PATH), "application/pdf");
+ extractRequest.setParam("extractOnly", "true");
+ NamedList<Object> extractResponse =
+ cluster.getSolrClient().request(extractRequest, COLLECTION_NAME);
+ print("Update status was: " + extractResponse.getName(0));
+ // end::solr-cell-pdf-extraction[]
+ });
+ }
+
+ @After
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+ ensureNoLeftoverOutputExpectations();
+ }
+}
diff --git
a/solr/solr-ref-guide/modules/indexing-guide/pages/indexing-with-tika.adoc
b/solr/solr-ref-guide/modules/indexing-guide/pages/indexing-with-tika.adoc
index 04b982a8f2a..1a50f8f14c6 100644
--- a/solr/solr-ref-guide/modules/indexing-guide/pages/indexing-with-tika.adoc
+++ b/solr/solr-ref-guide/modules/indexing-guide/pages/indexing-with-tika.adoc
@@ -671,16 +671,7 @@ First, let's use SolrJ to create a new SolrClient, then
we'll construct a reques
[source,java]
----
-public class SolrCellRequestDemo {
- public static void main (String[] args) throws IOException,
SolrServerException {
- SolrClient client = new
HttpSolrClient.Builder("http://localhost:8983/solr/my_collection").build();
- ContentStreamUpdateRequest req = new
ContentStreamUpdateRequest("/update/extract");
- req.addFile(new File("my-file.pdf"));
- req.setParam(ExtractingParams.EXTRACT_ONLY, "true");
- NamedList<Object> result = client.request(req);
- System.out.println("Result: " + result);
- }
-}
+include::example$SolrCellRefGuideExamplesTest.java[tag=solr-cell-pdf-extraction]
----
This operation streams the file `my-file.pdf` into the Solr index for
`my_collection`.
diff --git
a/solr/solr-ref-guide/modules/query-guide/examples/ExpectedOutputVerifier.java
b/solr/solr-ref-guide/modules/query-guide/examples/ExpectedOutputVerifier.java
new file mode 100644
index 00000000000..46f948fc7f9
--- /dev/null
+++
b/solr/solr-ref-guide/modules/query-guide/examples/ExpectedOutputVerifier.java
@@ -0,0 +1,57 @@
+/*
+ * 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.client.ref_guide_examples;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.util.ArrayDeque;
+import java.util.Queue;
+
+public class ExpectedOutputVerifier {
+ private static Queue<String> expectedLines = new ArrayDeque<>();
+
+ public static void expectLine(String expectedLine) {
+ expectedLines.add(expectedLine);
+ }
+
+ public static void print(String actualOutput) {
+ final String nextExpectedLine = expectedLines.poll();
+ assertNotNull(
+ "No more output expected, but was asked to print: " + actualOutput,
nextExpectedLine);
+
+ final String unexpectedOutputMessage =
+ "Expected line containing " + nextExpectedLine + ", but printed line
was: " + actualOutput;
+ assertTrue(unexpectedOutputMessage,
actualOutput.contains(nextExpectedLine));
+ }
+
+ public static void ensureNoLeftoverOutputExpectations() {
+ if (expectedLines.isEmpty()) return;
+
+ final StringBuilder builder = new StringBuilder();
+ builder.append("Leftover output was expected but not printed:");
+ for (String expectedLine : expectedLines) {
+ builder.append("\n\t").append(expectedLine);
+ }
+ fail(builder.toString());
+ }
+
+ public static void clear() {
+ expectedLines.clear();
+ }
+}
diff --git
a/solr/solr-ref-guide/modules/query-guide/examples/PaginationRefGuideExamplesTest.java
b/solr/solr-ref-guide/modules/query-guide/examples/PaginationRefGuideExamplesTest.java
new file mode 100644
index 00000000000..9a86ca2bef2
--- /dev/null
+++
b/solr/solr-ref-guide/modules/query-guide/examples/PaginationRefGuideExamplesTest.java
@@ -0,0 +1,129 @@
+/*
+ * 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.client.ref_guide_examples;
+
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.clear;
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.ensureNoLeftoverOutputExpectations;
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.expectLine;
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.print;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.request.SolrQuery;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.common.params.CursorMarkParams;
+import org.apache.solr.util.ExternalPaths;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Example Cursormark usage in SolrJ.
+ *
+ * <p>Snippets surrounded by "tag" and "end" comments are extracted and used
in the Solr Reference
+ * Guide.
+ */
+public class PaginationRefGuideExamplesTest extends SolrCloudTestCase {
+ private static final int NUM_INDEXED_DOCUMENTS = 9;
+ private static final int BATCH_SIZE = 1;
+ private static final String COLLECTION_NAME = "techproducts";
+ private static final String CONFIG_NAME = "techproducts_config";
+
+ @BeforeClass
+ public static void setupCluster() throws Exception {
+ configureCluster(1).addConfig(CONFIG_NAME,
ExternalPaths.TECHPRODUCTS_CONFIGSET).configure();
+
+ CollectionAdminRequest.createCollection(COLLECTION_NAME, CONFIG_NAME, 1, 1)
+ .process(cluster.getSolrClient());
+ }
+
+ @Before
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+
+ clear();
+ indexSampleData();
+ }
+
+ @After
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+ ensureNoLeftoverOutputExpectations();
+ }
+
+ @Test
+ public void testCursormarkWithSolrJExample() throws Exception {
+ for (int i = 0; i < NUM_INDEXED_DOCUMENTS; i++) {
+ expectLine("ID: " + i + "; Name: Fitbit Model " + i);
+ }
+
+ // spotless:off
+ // tag::cursormark-query[]
+ SolrQuery q =
+ new SolrQuery("*:*")
+ .setRows(BATCH_SIZE)
+ .setSort(SolrQuery.SortClause.asc("id"));
+ String cursorMark = CursorMarkParams.CURSOR_MARK_START;
+ boolean done = false;
+
+ while (!done) {
+ q.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
+ QueryResponse rsp = cluster.getSolrClient().query(COLLECTION_NAME, q);
+ String nextCursorMark = rsp.getNextCursorMark();
+ for (SolrDocument doc : rsp.getResults()) {
+ final var id = doc.getFieldValue("id");
+ final var name = doc.getFieldValue("name");
+ final String resultString =
+ String.format(
+ Locale.ROOT,
+ "ID: %s; Name: %s",
+ id,
+ name);
+ print(resultString);
+ }
+
+ done = cursorMark.equals(nextCursorMark);
+ cursorMark = nextCursorMark;
+ }
+ // end::cursormark-query[]
+ // spotless:on
+ }
+
+ private void indexSampleData() throws Exception {
+ final SolrClient client = cluster.getSolrClient();
+
+ final List<SolrInputDocument> docList = new ArrayList<>();
+ for (int i = 0; i < NUM_INDEXED_DOCUMENTS; i++) {
+ final SolrInputDocument doc = new SolrInputDocument();
+ doc.addField("id", i);
+ doc.addField("name", "Fitbit Model " + i);
+ docList.add(doc);
+ }
+
+ client.add(COLLECTION_NAME, docList);
+ client.commit(COLLECTION_NAME);
+ }
+}
diff --git
a/solr/solr-ref-guide/modules/query-guide/examples/ParallelSqlRefGuideExamplesTest.java
b/solr/solr-ref-guide/modules/query-guide/examples/ParallelSqlRefGuideExamplesTest.java
new file mode 100644
index 00000000000..ebec33f6422
--- /dev/null
+++
b/solr/solr-ref-guide/modules/query-guide/examples/ParallelSqlRefGuideExamplesTest.java
@@ -0,0 +1,121 @@
+/*
+ * 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.client.ref_guide_examples;
+
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.clear;
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.ensureNoLeftoverOutputExpectations;
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.expectLine;
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.print;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.util.ExternalPaths;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Example SolrJ usage of the Parallel SQL interface.
+ *
+ * <p>Snippets surrounded by "tag" and "end" comments are extracted and used
in the Solr Reference
+ * Guide.
+ */
+public class ParallelSqlRefGuideExamplesTest extends SolrCloudTestCase {
+ private static final int NUM_INDEXED_DOCUMENTS = 3;
+ private static final String COLLECTION_NAME = "techproducts";
+ private static final String CONFIG_NAME = "techproducts_config";
+
+ @BeforeClass
+ public static void setupCluster() throws Exception {
+ configureCluster(1).addConfig(CONFIG_NAME,
ExternalPaths.TECHPRODUCTS_CONFIGSET).configure();
+
+ CollectionAdminRequest.createCollection(COLLECTION_NAME, CONFIG_NAME, 1, 1)
+ .process(cluster.getSolrClient());
+ }
+
+ @Before
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+
+ clear();
+ indexSampleData();
+ }
+
+ @After
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+ ensureNoLeftoverOutputExpectations();
+ }
+
+ @Test
+ public void testQueryOnJdbcSqlInterface() throws Exception {
+ expectLine("Item: 0; Price: 0.0");
+ expectLine("Item: 1; Price: 2.0");
+ expectLine("Item: 2; Price: 4.0");
+
+ final String zkHost = cluster.getZkServer().getZkAddress();
+ // tag::jdbc-query-interface[]
+ final String connString =
+ "jdbc:solr://"
+ + zkHost
+ +
"?collection=techproducts&aggregationMode=map_reduce&numWorkers=2";
+
+ try (Connection con = DriverManager.getConnection(connString)) {
+ try (final Statement stmt = con.createStatement()) {
+ final String sqlQuery = "SELECT id, price_f FROM techproducts LIMIT 3";
+
+ try (ResultSet rs = stmt.executeQuery(sqlQuery)) {
+ while (rs.next()) {
+ final String id = rs.getString("id");
+ final String price = rs.getString("price_f");
+ final String resultString =
+ String.format(Locale.ROOT, "Item: %s; Price: %s", id, price);
+ print(resultString);
+ }
+ }
+ }
+ }
+ // end::jdbc-query-interface[]
+ }
+
+ private void indexSampleData() throws Exception {
+ final SolrClient client = cluster.getSolrClient();
+
+ final List<SolrInputDocument> docList = new ArrayList<>();
+ for (int i = 0; i < NUM_INDEXED_DOCUMENTS; i++) {
+ final SolrInputDocument doc = new SolrInputDocument();
+ doc.addField("id", i);
+ doc.addField("price_f", i * 2.0f);
+ docList.add(doc);
+ }
+
+ client.add(COLLECTION_NAME, docList);
+ client.commit(COLLECTION_NAME);
+ }
+}
diff --git
a/solr/solr-ref-guide/modules/query-guide/examples/TermComponentRefGuideExamplesTest.java
b/solr/solr-ref-guide/modules/query-guide/examples/TermComponentRefGuideExamplesTest.java
new file mode 100644
index 00000000000..1bab1a0fe33
--- /dev/null
+++
b/solr/solr-ref-guide/modules/query-guide/examples/TermComponentRefGuideExamplesTest.java
@@ -0,0 +1,128 @@
+/*
+ * 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.client.ref_guide_examples;
+
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.clear;
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.ensureNoLeftoverOutputExpectations;
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.expectLine;
+import static
org.apache.solr.client.ref_guide_examples.ExpectedOutputVerifier.print;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.beans.Field;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.client.solrj.request.SolrQuery;
+import org.apache.solr.client.solrj.response.TermsResponse.Term;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.util.ExternalPaths;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Example SolrJ usage of the Terms Component.
+ *
+ * <p>Snippets surrounded by "tag" and "end" comments are extracted and used
in the Solr Reference
+ * Guide.
+ */
+public class TermComponentRefGuideExamplesTest extends SolrCloudTestCase {
+ private static final String COLLECTION_NAME = "techproducts";
+ private static final String CONFIG_NAME = "techproducts_config";
+
+ public static class TechProduct {
+ @Field public String id;
+ @Field public String name;
+
+ public TechProduct() {}
+
+ public TechProduct(String id, String name) {
+ this.id = id;
+ this.name = name;
+ }
+ }
+
+ @BeforeClass
+ public static void setupCluster() throws Exception {
+ configureCluster(1).addConfig(CONFIG_NAME,
ExternalPaths.TECHPRODUCTS_CONFIGSET).configure();
+
+ CollectionAdminRequest.createCollection(COLLECTION_NAME, CONFIG_NAME, 1, 1)
+ .process(cluster.getSolrClient());
+ }
+
+ @Before
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+
+ clear();
+ indexSampleData();
+ }
+
+ @Test
+ public void testTermsComponentRequest() throws Exception {
+ expectLine("Top term is fitbit with frequency 2");
+
+ // tag::term-component-request[]
+ final SolrQuery termsQuery = new SolrQuery();
+ termsQuery.setTerms(true);
+ termsQuery.setTermsLimit(5);
+ termsQuery.addTermsField("name");
+ termsQuery.setTermsMinCount(1);
+
+ final QueryRequest request = new QueryRequest(termsQuery);
+ request.setPath("/terms");
+ final List<Term> terms =
+ request
+ .process(cluster.getSolrClient(), COLLECTION_NAME)
+ .getTermsResponse()
+ .getTerms("name");
+
+ final Term topTerm = terms.getFirst();
+ final String resultString =
+ String.format(
+ Locale.ROOT,
+ "Top term is %s with frequency %s",
+ topTerm.getTerm(),
+ topTerm.getFrequency());
+ print(resultString);
+ // end::term-component-request[]
+ }
+
+ @After
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+ ensureNoLeftoverOutputExpectations();
+ }
+
+ private void indexSampleData() throws Exception {
+ SolrClient client = cluster.getSolrClient();
+
+ final List<TechProduct> products = new ArrayList<>();
+ products.add(new TechProduct("1", "Fitbit Alta"));
+ products.add(new TechProduct("2", "Sony Walkman"));
+ products.add(new TechProduct("3", "Garmin GPS"));
+ products.add(new TechProduct("4", "Fitbit Flex"));
+
+ client.addBeans(COLLECTION_NAME, products);
+ client.commit(COLLECTION_NAME);
+ }
+}
diff --git
a/solr/solr-ref-guide/modules/query-guide/pages/pagination-of-results.adoc
b/solr/solr-ref-guide/modules/query-guide/pages/pagination-of-results.adoc
index 59ee7fe4193..609de61d82f 100644
--- a/solr/solr-ref-guide/modules/query-guide/pages/pagination-of-results.adoc
+++ b/solr/solr-ref-guide/modules/query-guide/pages/pagination-of-results.adoc
@@ -155,19 +155,7 @@ Using SolrJ, this pseudocode would be:
[source,java]
----
-SolrQuery q = (new
SolrQuery(some_query)).setRows(r).setSort(SortClause.asc("id"));
-String cursorMark = CursorMarkParams.CURSOR_MARK_START;
-boolean done = false;
-while (! done) {
- q.set(CursorMarkParams.CURSOR_MARK_PARAM, cursorMark);
- QueryResponse rsp = solrServer.query(q);
- String nextCursorMark = rsp.getNextCursorMark();
- doCustomProcessingOfResults(rsp);
- if (cursorMark.equals(nextCursorMark)) {
- done = true;
- }
- cursorMark = nextCursorMark;
-}
+include::example$PaginationRefGuideExamplesTest.java[tag=cursormark-query]
----
If you wanted to do this by hand using curl, the sequence of requests would
look something like this:
diff --git a/solr/solr-ref-guide/modules/query-guide/pages/sql-query.adoc
b/solr/solr-ref-guide/modules/query-guide/pages/sql-query.adoc
index 6a5731c1458..a67678f105f 100644
--- a/solr/solr-ref-guide/modules/query-guide/pages/sql-query.adoc
+++ b/solr/solr-ref-guide/modules/query-guide/pages/sql-query.adoc
@@ -408,21 +408,7 @@ Below is sample code for creating a connection and
executing a query with the JD
[source,java]
----
-Connection con = null;
-try {
- con = DriverManager.getConnection("jdbc:solr://" + zkHost +
"?collection=collection1&aggregationMode=map_reduce&numWorkers=2");
- stmt = con.createStatement();
- rs = stmt.executeQuery("SELECT a_s, sum(a_f) as sum FROM collection1 GROUP
BY a_s ORDER BY sum desc");
-
- while(rs.next()) {
- String a_s = rs.getString("a_s");
- double s = rs.getDouble("sum");
- }
-} finally {
- rs.close();
- stmt.close();
- con.close();
-}
+include::example$ParallelSqlRefGuideExamplesTest.java[tag=jdbc-query-interface]
----
The connection URL must contain the `zkHost` and the `collection` parameters.
diff --git a/solr/solr-ref-guide/modules/query-guide/pages/terms-component.adoc
b/solr/solr-ref-guide/modules/query-guide/pages/terms-component.adoc
index fda1115a686..deb07f5007f 100644
--- a/solr/solr-ref-guide/modules/query-guide/pages/terms-component.adoc
+++ b/solr/solr-ref-guide/modules/query-guide/pages/terms-component.adoc
@@ -434,17 +434,7 @@ Results (notice that the term counts are not affected by
the query):
[source,java]
----
- SolrQuery query = new SolrQuery();
- query.setRequestHandler("/terms");
- query.setTerms(true);
- query.setTermsLimit(5);
- query.setTermsLower("s");
- query.setTermsPrefix("s");
- query.addTermsField("terms_s");
- query.setTermsMinCount(1);
-
- QueryRequest request = new QueryRequest(query);
- List<Term> terms =
request.process(getSolrClient()).getTermsResponse().getTerms("terms_s");
+include::example$TermComponentRefGuideExamplesTest.java[tag=term-component-request]
----
== Using the Terms Component for an Auto-Suggest Feature