You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by ho...@apache.org on 2022/07/19 21:47:27 UTC
[solr] branch main updated: SOLR-16299: Remove symlinks for ref-guide examples (#946)
This is an automated email from the ASF dual-hosted git repository.
houston pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/solr.git
The following commit(s) were added to refs/heads/main by this push:
new 0a6960fc7bd SOLR-16299: Remove symlinks for ref-guide examples (#946)
0a6960fc7bd is described below
commit 0a6960fc7bd069b5ce19c5b970cc0967c5db7fbc
Author: Houston Putman <ho...@apache.org>
AuthorDate: Tue Jul 19 17:47:22 2022 -0400
SOLR-16299: Remove symlinks for ref-guide examples (#946)
The example classes now live in the ref-guide module
---
gradle/validation/spotless.gradle | 4 +
solr/CHANGES.txt | 2 +
solr/solr-ref-guide/build.gradle | 40 ++
.../examples/UsingSolrJRefGuideExamplesTest.java | 326 ++++++++-
.../examples/ZkConfigFilesTest.java | 92 ++-
.../examples/IndexingNestedDocuments.java | 286 +++++++-
.../JsonRequestApiHeatmapFacetingTest.java | 121 +++-
.../query-guide/examples/JsonRequestApiTest.java | 786 ++++++++++++++++++++-
.../IndexingNestedDocuments.java | 285 --------
.../JsonRequestApiHeatmapFacetingTest.java | 120 ----
.../ref_guide_examples/JsonRequestApiTest.java | 785 --------------------
.../UsingSolrJRefGuideExamplesTest.java | 325 ---------
.../ref_guide_examples/ZkConfigFilesTest.java | 91 ---
13 files changed, 1652 insertions(+), 1611 deletions(-)
diff --git a/gradle/validation/spotless.gradle b/gradle/validation/spotless.gradle
index b028fe00168..af14da98bd7 100644
--- a/gradle/validation/spotless.gradle
+++ b/gradle/validation/spotless.gradle
@@ -57,6 +57,10 @@ configure(project(":solr").subprojects) { prj ->
// Exclude Hadoop copied files to make upgrades easier
targetExclude "src/**/org/apache/hadoop/**"
break
+ case ":solr:solr-ref-guide":
+ // Apply to all example Java sources in the ref-guide
+ target "modules/**/examples/*.java"
+ break
}
}
}
diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt
index ce3d825633f..2b781c93d74 100644
--- a/solr/CHANGES.txt
+++ b/solr/CHANGES.txt
@@ -146,6 +146,8 @@ Build
* SOLR-16264: Set versions for all Antora build tools (Houston Putman)
+* SOLR-16299: Ref-guide examples are no longer symlinked from SolrJ, they live in the ref-guide module (Houston Putman)
+
================== 9.0.0 ==================
New Features
diff --git a/solr/solr-ref-guide/build.gradle b/solr/solr-ref-guide/build.gradle
index 22193e38166..a56b9c2677b 100644
--- a/solr/solr-ref-guide/build.gradle
+++ b/solr/solr-ref-guide/build.gradle
@@ -17,8 +17,14 @@
import org.apache.tools.ant.util.TeeOutputStream
+apply plugin: 'java'
+
description = 'Solr Reference Guide'
+/*
+ Antora Logic - Building the ref-guide
+ */
+
def officialSiteIncludePrerelease = propertyOrEnvOrDefault("refguide.official.includePrereleaseVersions", "SOLR_REF_GUIDE_OFFICIAL_INCLUDE_PRERELEASE", "false").toBoolean()
def buildLocalUI = propertyOrEnvOrDefault("refguide.buildLocalUI", "SOLR_REF_GUIDE_BUILD_LOCAL_UI", "false").toBoolean()
@@ -503,3 +509,37 @@ task buildOfficialSite(type: NpxTask) {
project.logger.lifecycle("\t${project.ext.siteDir}")
}
}
+
+/*
+ Compiling, Testing and Validation for the java examples in the Solr Ref Guide
+ */
+
+sourceSets {
+ test {
+ java {
+ srcDirs = ['modules/configuration-guide/examples', 'modules/deployment-guide/examples', 'modules/getting-started/examples', 'modules/indexing-guide/examples', 'modules/query-guide/examples']
+ }
+ }
+}
+
+dependencies {
+ // For the SolrJ examples
+ testImplementation project(":solr:core")
+ testImplementation project(":solr:solrj")
+ testImplementation project(":solr:test-framework")
+ testImplementation 'junit:junit'
+}
+
+// Copy all the test resource files from SolrJ to the build/resources/test directory
+// of the examples so we can avoid duplication of the test resource files like
+// schemas and SolrConfigs.
+task copySolrjTestResources(type: Copy) {
+ from(project(':solr:solrj').sourceSets.test.resources.srcDirs) {
+ exclude '**/*.java'
+ }
+ into sourceSets.test.output.resourcesDir
+}
+
+tasks.processTestResources.configure {
+ dependsOn copySolrjTestResources
+}
diff --git a/solr/solr-ref-guide/modules/deployment-guide/examples/UsingSolrJRefGuideExamplesTest.java b/solr/solr-ref-guide/modules/deployment-guide/examples/UsingSolrJRefGuideExamplesTest.java
deleted file mode 120000
index 04f64eb3d0f..00000000000
--- a/solr/solr-ref-guide/modules/deployment-guide/examples/UsingSolrJRefGuideExamplesTest.java
+++ /dev/null
@@ -1 +0,0 @@
-../../../../solrj/src/test/org/apache/solr/client/ref_guide_examples/UsingSolrJRefGuideExamplesTest.java
\ No newline at end of file
diff --git a/solr/solr-ref-guide/modules/deployment-guide/examples/UsingSolrJRefGuideExamplesTest.java b/solr/solr-ref-guide/modules/deployment-guide/examples/UsingSolrJRefGuideExamplesTest.java
new file mode 100644
index 00000000000..fee64cef9df
--- /dev/null
+++ b/solr/solr-ref-guide/modules/deployment-guide/examples/UsingSolrJRefGuideExamplesTest.java
@@ -0,0 +1,325 @@
+/*
+ * 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 java.io.File;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Queue;
+import java.util.UUID;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.SolrQuery.ORDER;
+import org.apache.solr.client.solrj.SolrRequest;
+import org.apache.solr.client.solrj.beans.Field;
+import org.apache.solr.client.solrj.impl.CloudSolrClient;
+import org.apache.solr.client.solrj.impl.HttpSolrClient;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.response.CollectionAdminResponse;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.client.solrj.response.UpdateResponse;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.SolrDocumentList;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.common.params.MapSolrParams;
+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.
+ *
+ * <p>Snippets surrounded by "tag" and "end" comments are extracted and used in the Solr Reference
+ * Guide.
+ */
+public class UsingSolrJRefGuideExamplesTest extends SolrCloudTestCase {
+
+ private static final int NUM_INDEXED_DOCUMENTS = 3;
+ private static final int NUM_LIVE_NODES = 1;
+
+ private Queue<String> expectedLines = new ArrayDeque<>();
+
+ @BeforeClass
+ public static void setUpCluster() throws Exception {
+ configureCluster(NUM_LIVE_NODES)
+ .addConfig("conf", new File(ExternalPaths.TECHPRODUCTS_CONFIGSET).toPath())
+ .configure();
+
+ CollectionAdminResponse response =
+ CollectionAdminRequest.createCollection("techproducts", "conf", 1, 1)
+ .setPerReplicaState(SolrCloudTestCase.USE_PER_REPLICA_STATE)
+ .process(cluster.getSolrClient());
+ cluster.waitForActiveCollection("techproducts", 1, 1);
+ }
+
+ @Before
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ expectedLines.clear();
+ final SolrClient client = getSolrClient();
+
+ final List<TechProduct> products = new ArrayList<TechProduct>();
+ products.add(new TechProduct("1", "Fitbit Alta"));
+ products.add(new TechProduct("2", "Sony Walkman"));
+ products.add(new TechProduct("3", "Garmin GPS"));
+
+ client.addBeans("techproducts", products);
+ client.commit("techproducts");
+ }
+
+ @After
+ @Override
+ public void tearDown() throws Exception {
+ super.tearDown();
+ ensureNoLeftoverOutputExpectations();
+
+ final SolrClient client = getSolrClient();
+ client.deleteByQuery("techproducts", "*:*");
+ client.commit("techproducts");
+ }
+
+ @Test
+ public void queryWithRawSolrParamsExample() throws Exception {
+ expectLine("Found 3 documents");
+ expectLine("id: 1; name: Fitbit Alta");
+ expectLine("id: 2; name: Sony Walkman");
+ expectLine("id: 3; name: Garmin GPS");
+
+ // tag::solrj-query-with-raw-solrparams[]
+ final SolrClient client = getSolrClient();
+
+ final Map<String, String> queryParamMap = new HashMap<String, String>();
+ queryParamMap.put("q", "*:*");
+ queryParamMap.put("fl", "id, name");
+ queryParamMap.put("sort", "id asc");
+ MapSolrParams queryParams = new MapSolrParams(queryParamMap);
+
+ final QueryResponse response = client.query("techproducts", queryParams);
+ final SolrDocumentList documents = response.getResults();
+
+ print("Found " + documents.getNumFound() + " documents");
+ for (SolrDocument document : documents) {
+ final String id = (String) document.getFirstValue("id");
+ final String name = (String) document.getFirstValue("name");
+
+ print("id: " + id + "; name: " + name);
+ }
+ // end::solrj-query-with-raw-solrparams[]
+ }
+
+ @Test
+ public void queryWithSolrQueryExample() throws Exception {
+ final int numResultsToReturn = 3;
+ expectLine("Found 3 documents");
+ expectLine("id: 1; name: Fitbit Alta");
+ expectLine("id: 2; name: Sony Walkman");
+ expectLine("id: 3; name: Garmin GPS");
+ final SolrClient client = getSolrClient();
+
+ // tag::solrj-query-with-solrquery[]
+ final SolrQuery query = new SolrQuery("*:*");
+ query.addField("id");
+ query.addField("name");
+ query.setSort("id", ORDER.asc);
+ query.setRows(numResultsToReturn);
+ // end::solrj-query-with-solrquery[]
+
+ final QueryResponse response = client.query("techproducts", query);
+ final SolrDocumentList documents = response.getResults();
+
+ print("Found " + documents.getNumFound() + " documents");
+ assertEquals(numResultsToReturn, documents.size());
+ for (SolrDocument document : documents) {
+ final String id = (String) document.getFirstValue("id");
+ final String name = (String) document.getFirstValue("name");
+
+ print("id: " + id + "; name: " + name);
+ }
+ }
+
+ @Test
+ public void indexWithSolrInputDocumentExample() throws Exception {
+ // tag::solrj-index-with-raw-solrinputdoc[]
+ final SolrClient client = getSolrClient();
+
+ final SolrInputDocument doc = new SolrInputDocument();
+ doc.addField("id", UUID.randomUUID().toString());
+ doc.addField("name", "Amazon Kindle Paperwhite");
+
+ final UpdateResponse updateResponse = client.add("techproducts", doc);
+ // Indexed documents must be committed
+ client.commit("techproducts");
+ // end::solrj-index-with-raw-solrinputdoc[]
+
+ assertNumDocuments(NUM_INDEXED_DOCUMENTS + 1);
+ }
+
+ @Test
+ public void indexBeanValueTypeExample() throws Exception {
+ // tag::solrj-index-bean-value-type[]
+ final SolrClient client = getSolrClient();
+
+ final TechProduct kindle = new TechProduct("kindle-id-4", "Amazon Kindle Paperwhite");
+ final UpdateResponse response = client.addBean("techproducts", kindle);
+
+ client.commit("techproducts");
+ // end::solrj-index-bean-value-type[]
+
+ assertNumDocuments(NUM_INDEXED_DOCUMENTS + 1);
+ }
+
+ @Test
+ public void queryBeanValueTypeExample() throws Exception {
+ expectLine("Found 3 documents");
+ expectLine("id: 1; name: Fitbit Alta");
+ expectLine("id: 2; name: Sony Walkman");
+ expectLine("id: 3; name: Garmin GPS");
+
+ // tag::solrj-query-bean-value-type[]
+ final SolrClient client = getSolrClient();
+
+ final SolrQuery query = new SolrQuery("*:*");
+ query.addField("id");
+ query.addField("name");
+ query.setSort("id", ORDER.asc);
+
+ final QueryResponse response = client.query("techproducts", query);
+ final List<TechProduct> products = response.getBeans(TechProduct.class);
+ // end::solrj-query-bean-value-type[]
+
+ print("Found " + products.size() + " documents");
+ for (TechProduct product : products) {
+ print("id: " + product.id + "; name: " + product.name);
+ }
+ }
+
+ @Test
+ public void otherSolrApisExample() throws Exception {
+ expectLine("Found " + NUM_LIVE_NODES + " live nodes");
+ // tag::solrj-other-apis[]
+ final SolrClient client = getSolrClient();
+
+ @SuppressWarnings({"rawtypes"})
+ final SolrRequest request = new CollectionAdminRequest.ClusterStatus();
+
+ final NamedList<Object> response = client.request(request);
+ @SuppressWarnings({"unchecked"})
+ final NamedList<Object> cluster = (NamedList<Object>) response.get("cluster");
+ @SuppressWarnings({"unchecked"})
+ final List<String> liveNodes = (List<String>) cluster.get("live_nodes");
+
+ print("Found " + liveNodes.size() + " live nodes");
+ // end::solrj-other-apis[]
+ }
+
+ private SolrClient getSolrClient() {
+ return cluster.getSolrClient();
+ }
+
+ private SolrClient getTechProductSolrClient() {
+ // tag::solrj-solrclient-timeouts[]
+ final String solrUrl = "http://localhost:8983/solr";
+ return new HttpSolrClient.Builder(solrUrl)
+ .withConnectionTimeout(10000)
+ .withSocketTimeout(60000)
+ .build();
+ // end::solrj-solrclient-timeouts[]
+ }
+
+ private SolrClient getBaseURLCloudSolrClient() {
+ // tag::solrj-cloudsolrclient-baseurl[]
+ final List<String> solrUrls = new ArrayList<>();
+ solrUrls.add("http://solr1:8983/solr");
+ solrUrls.add("http://solr2:8983/solr");
+ return new CloudSolrClient.Builder(solrUrls).build();
+ // end::solrj-cloudsolrclient-baseurl[]
+ }
+
+ private SolrClient getZookeeperNoRootCloudSolrClient() {
+ // tag::solrj-cloudsolrclient-zookeepernoroot[]
+ final List<String> zkServers = new ArrayList<>();
+ zkServers.add("zookeeper1:2181");
+ zkServers.add("zookeeper2:2181");
+ zkServers.add("zookeeper3:2181");
+ return new CloudSolrClient.Builder(zkServers, Optional.empty()).build();
+ // end::solrj-cloudsolrclient-zookeepernoroot[]
+ }
+
+ private SolrClient getZookeeperRootCloudSolrClient() {
+ // tag::solrj-cloudsolrclient-zookeeperroot[]
+ final List<String> zkServers = new ArrayList<>();
+ zkServers.add("zookeeper1:2181");
+ zkServers.add("zookeeper2:2181");
+ zkServers.add("zookeeper3:2181");
+ return new CloudSolrClient.Builder(zkServers, Optional.of("/solr")).build();
+ // end::solrj-cloudsolrclient-zookeeperroot[]
+ }
+
+ private void assertNumDocuments(int expectedNumResults) throws Exception {
+ final QueryResponse queryResponse = getSolrClient().query("techproducts", new SolrQuery("*:*"));
+ assertEquals(expectedNumResults, queryResponse.getResults().getNumFound());
+ }
+
+ // tag::solrj-techproduct-value-type[]
+ public static class TechProduct {
+ @Field public String id;
+ @Field public String name;
+
+ public TechProduct(String id, String name) {
+ this.id = id;
+ this.name = name;
+ }
+
+ public TechProduct() {}
+ }
+ // end::solrj-techproduct-value-type[]
+
+ private void expectLine(String expectedLine) {
+ expectedLines.add(expectedLine);
+ }
+
+ private 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));
+ }
+
+ private 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" + expectedLine);
+ }
+ fail(builder.toString());
+ }
+}
diff --git a/solr/solr-ref-guide/modules/deployment-guide/examples/ZkConfigFilesTest.java b/solr/solr-ref-guide/modules/deployment-guide/examples/ZkConfigFilesTest.java
deleted file mode 120000
index 3179a82978a..00000000000
--- a/solr/solr-ref-guide/modules/deployment-guide/examples/ZkConfigFilesTest.java
+++ /dev/null
@@ -1 +0,0 @@
-../../../../solrj/src/test/org/apache/solr/client/ref_guide_examples/ZkConfigFilesTest.java
\ No newline at end of file
diff --git a/solr/solr-ref-guide/modules/deployment-guide/examples/ZkConfigFilesTest.java b/solr/solr-ref-guide/modules/deployment-guide/examples/ZkConfigFilesTest.java
new file mode 100644
index 00000000000..f3d9c41fb1e
--- /dev/null
+++ b/solr/solr-ref-guide/modules/deployment-guide/examples/ZkConfigFilesTest.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 java.io.File;
+import java.nio.file.Paths;
+import java.util.List;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.core.ConfigSetService;
+import org.apache.solr.util.ExternalPaths;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Examples showing how to manipulate configsets in ZK.
+ *
+ * <p>Snippets surrounded by "tag" and "end" comments are extracted and used in the Solr Reference
+ * Guide.
+ */
+public class ZkConfigFilesTest extends SolrCloudTestCase {
+
+ @BeforeClass
+ public static void setUpCluster() throws Exception {
+ configureCluster(1).configure();
+ }
+
+ private static ConfigSetService getConfigSetService() {
+ return cluster.getOpenOverseer().getCoreContainer().getConfigSetService();
+ }
+
+ @Before
+ public void clearConfigsBefore() throws Exception {
+ clearConfigs();
+ }
+
+ @After
+ public void clearConfigsAfter() throws Exception {
+ clearConfigs();
+ }
+
+ private void clearConfigs() throws Exception {
+ List<String> configs = getConfigSetService().listConfigs();
+ for (String config : configs) {
+ getConfigSetService().deleteConfig(config);
+ }
+ }
+
+ @Test
+ public void testCanUploadConfigToZk() throws Exception {
+ final String localConfigSetDirectory =
+ new File(ExternalPaths.TECHPRODUCTS_CONFIGSET).getAbsolutePath();
+
+ assertConfigsContainOnly();
+
+ // tag::zk-configset-upload[]
+ getConfigSetService().uploadConfig("nameForConfigset", Paths.get(localConfigSetDirectory));
+ // end::zk-configset-upload[]
+
+ assertConfigsContainOnly("nameForConfigset");
+ }
+
+ private void assertConfigsContainOnly(String... expectedConfigs) throws Exception {
+ final int expectedSize = expectedConfigs.length;
+
+ List<String> actualConfigs = getConfigSetService().listConfigs();
+
+ assertEquals(expectedSize, actualConfigs.size());
+ for (String expectedConfig : expectedConfigs) {
+ assertTrue(
+ "Expected ZK to contain " + expectedConfig + ", but it didn't. Actual configs: ",
+ actualConfigs.contains(expectedConfig));
+ }
+ }
+}
diff --git a/solr/solr-ref-guide/modules/indexing-guide/examples/IndexingNestedDocuments.java b/solr/solr-ref-guide/modules/indexing-guide/examples/IndexingNestedDocuments.java
deleted file mode 120000
index 05cde5492f7..00000000000
--- a/solr/solr-ref-guide/modules/indexing-guide/examples/IndexingNestedDocuments.java
+++ /dev/null
@@ -1 +0,0 @@
-../../../../solrj/src/test/org/apache/solr/client/ref_guide_examples/IndexingNestedDocuments.java
\ No newline at end of file
diff --git a/solr/solr-ref-guide/modules/indexing-guide/examples/IndexingNestedDocuments.java b/solr/solr-ref-guide/modules/indexing-guide/examples/IndexingNestedDocuments.java
new file mode 100644
index 00000000000..efecb4ed928
--- /dev/null
+++ b/solr/solr-ref-guide/modules/indexing-guide/examples/IndexingNestedDocuments.java
@@ -0,0 +1,285 @@
+/*
+ * 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 java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.SolrDocumentList;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.util.ExternalPaths;
+import org.junit.After;
+import org.junit.BeforeClass;
+
+/**
+ * Example SolrJ usage for indexing nested documents
+ *
+ * <p>Snippets surrounded by "tag" and "end" comments are extracted and used in the Solr Reference
+ * Guide.
+ */
+public class IndexingNestedDocuments extends SolrCloudTestCase {
+ public static final String ANON_KIDS_CONFIG = "anon_kids_configset";
+
+ @BeforeClass
+ public static void setupCluster() throws Exception {
+ configureCluster(1)
+ // when indexing 'anonymous' kids, we need a schema that doesn't use _nest_path_ so
+ // that we can use [child] transformer with a parentFilter...
+ .addConfig(ANON_KIDS_CONFIG, new File(ExternalPaths.TECHPRODUCTS_CONFIGSET).toPath())
+ .configure();
+ }
+
+ @After
+ public void cleanCollections() throws Exception {
+ cluster.deleteAllCollections();
+ }
+
+ /** Syntactic sugar so code snippet doesn't refer to test-framework specific method name */
+ public static SolrClient getSolrClient() {
+ return cluster.getSolrClient();
+ }
+
+ /**
+ * Demo of using anonymous children when indexing hierarchical documents. This test code is used
+ * as an 'include' from the ref-guide
+ */
+ public void testIndexingAnonKids() throws Exception {
+ final String collection = "test_anon";
+ CollectionAdminRequest.createCollection(collection, ANON_KIDS_CONFIG, 1, 1)
+ .setPerReplicaState(SolrCloudTestCase.USE_PER_REPLICA_STATE)
+ .process(cluster.getSolrClient());
+ cluster.getSolrClient().setDefaultCollection(collection);
+
+ //
+ // DO NOT MODIFY THESE EXAMPLE DOCS WITH OUT MAKING THE SAME CHANGES TO THE JSON AND XML
+ // EQUIVILENT EXAMPLES IN 'indexing-nested-documents.adoc'
+ //
+
+ // tag::anon-kids[]
+ final SolrClient client = getSolrClient();
+
+ final SolrInputDocument p1 = new SolrInputDocument();
+ p1.setField("id", "P11!prod");
+ p1.setField("type_s", "PRODUCT");
+ p1.setField("name_s", "Swingline Stapler");
+ p1.setField("description_t", "The Cadillac of office staplers ...");
+ {
+ final SolrInputDocument s1 = new SolrInputDocument();
+ s1.setField("id", "P11!S21");
+ s1.setField("type_s", "SKU");
+ s1.setField("color_s", "RED");
+ s1.setField("price_i", 42);
+ {
+ final SolrInputDocument m1 = new SolrInputDocument();
+ m1.setField("id", "P11!D41");
+ m1.setField("type_s", "MANUAL");
+ m1.setField("name_s", "Red Swingline Brochure");
+ m1.setField("pages_i", 1);
+ m1.setField("content_t", "...");
+
+ s1.addChildDocument(m1);
+ }
+
+ final SolrInputDocument s2 = new SolrInputDocument();
+ s2.setField("id", "P11!S31");
+ s2.setField("type_s", "SKU");
+ s2.setField("color_s", "BLACK");
+ s2.setField("price_i", 3);
+
+ final SolrInputDocument m1 = new SolrInputDocument();
+ m1.setField("id", "P11!D51");
+ m1.setField("type_s", "MANUAL");
+ m1.setField("name_s", "Quick Reference Guide");
+ m1.setField("pages_i", 1);
+ m1.setField("content_t", "How to use your stapler ...");
+
+ final SolrInputDocument m2 = new SolrInputDocument();
+ m2.setField("id", "P11!D61");
+ m2.setField("type_s", "MANUAL");
+ m2.setField("name_s", "Warranty Details");
+ m2.setField("pages_i", 42);
+ m2.setField("content_t", "... lifetime guarantee ...");
+
+ p1.addChildDocuments(Arrays.asList(s1, s2, m1, m2));
+ }
+
+ client.add(p1);
+ // end::anon-kids[]
+
+ client.commit();
+
+ final SolrDocumentList docs =
+ getSolrClient()
+ .query(
+ new SolrQuery("description_t:Cadillac")
+ .set("fl", "*,[child parentFilter='type_s:PRODUCT']"))
+ .getResults();
+
+ 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());
+ assertEquals(5, docs.get(0).getChildDocuments().size());
+
+ // flat list is depth first...
+ final SolrDocument red_stapler_brochure = docs.get(0).getChildDocuments().get(0);
+ assertEquals("P11!D41", red_stapler_brochure.getFieldValue("id"));
+
+ final SolrDocument red_stapler = docs.get(0).getChildDocuments().get(1);
+ assertEquals("P11!S21", red_stapler.getFieldValue("id"));
+ }
+
+ /**
+ * Demo of using <code>NestPath</code> related pseudo-fields when indexing hierarchical documents.
+ * This test code is used as an 'include' from the ref-guide
+ */
+ public void testIndexingUsingNestPath() throws Exception {
+ final String collection = "test_anon";
+ CollectionAdminRequest.createCollection(collection, 1, 1).process(cluster.getSolrClient());
+ cluster.getSolrClient().setDefaultCollection(collection);
+
+ //
+ // DO NOT MODIFY THESE EXAMPLE DOCS WITH OUT MAKING THE SAME CHANGES TO THE JSON AND XML
+ // EQUIVILENT EXAMPLES IN 'indexing-nested-documents.adoc'
+ //
+
+ // tag::nest-path[]
+ final SolrClient client = getSolrClient();
+
+ final SolrInputDocument p1 = new SolrInputDocument();
+ p1.setField("id", "P11!prod");
+ p1.setField("name_s", "Swingline Stapler");
+ p1.setField("description_t", "The Cadillac of office staplers ...");
+ {
+ final SolrInputDocument s1 = new SolrInputDocument();
+ s1.setField("id", "P11!S21");
+ s1.setField("color_s", "RED");
+ s1.setField("price_i", 42);
+ {
+ final SolrInputDocument m1 = new SolrInputDocument();
+ m1.setField("id", "P11!D41");
+ m1.setField("name_s", "Red Swingline Brochure");
+ m1.setField("pages_i", 1);
+ m1.setField("content_t", "...");
+
+ s1.setField("manuals", m1);
+ }
+
+ final SolrInputDocument s2 = new SolrInputDocument();
+ s2.setField("id", "P11!S31");
+ s2.setField("color_s", "BLACK");
+ s2.setField("price_i", 3);
+
+ p1.setField("skus", Arrays.asList(s1, s2));
+ }
+ {
+ final SolrInputDocument m1 = new SolrInputDocument();
+ m1.setField("id", "P11!D51");
+ m1.setField("name_s", "Quick Reference Guide");
+ m1.setField("pages_i", 1);
+ m1.setField("content_t", "How to use your stapler ...");
+
+ final SolrInputDocument m2 = new SolrInputDocument();
+ m2.setField("id", "P11!D61");
+ m2.setField("name_s", "Warranty Details");
+ m2.setField("pages_i", 42);
+ m2.setField("content_t", "... lifetime guarantee ...");
+
+ p1.setField("manuals", Arrays.asList(m1, m2));
+ }
+
+ final SolrInputDocument p2 = new SolrInputDocument();
+ p2.setField("id", "P22!prod");
+ p2.setField("name_s", "Mont Blanc Fountain Pen");
+ p2.setField("description_t", "A Premium Writing Instrument ...");
+ {
+ final SolrInputDocument s1 = new SolrInputDocument();
+ s1.setField("id", "P22!S22");
+ s1.setField("color_s", "RED");
+ s1.setField("price_i", 89);
+ {
+ final SolrInputDocument m1 = new SolrInputDocument();
+ m1.setField("id", "P22!D42");
+ m1.setField("name_s", "Red Mont Blanc Brochure");
+ m1.setField("pages_i", 1);
+ m1.setField("content_t", "...");
+
+ s1.setField("manuals", m1);
+ }
+
+ final SolrInputDocument s2 = new SolrInputDocument();
+ s2.setField("id", "P22!S32");
+ s2.setField("color_s", "BLACK");
+ s2.setField("price_i", 67);
+
+ p2.setField("skus", Arrays.asList(s1, s2));
+ }
+ {
+ final SolrInputDocument m1 = new SolrInputDocument();
+ m1.setField("id", "P22!D52");
+ m1.setField("name_s", "How To Use A Pen");
+ m1.setField("pages_i", 42);
+ m1.setField("content_t", "Start by removing the cap ...");
+
+ p2.setField("manuals", m1);
+ }
+
+ client.add(Arrays.asList(p1, p2));
+ // end::nest-path[]
+
+ client.commit();
+
+ // Now a quick sanity check that the nest path is working properly...
+
+ final SolrDocumentList docs =
+ getSolrClient()
+ .query(new SolrQuery("description_t:Writing").set("fl", "*,[child]"))
+ .getResults();
+
+ assertEquals(1, docs.getNumFound());
+ assertEquals("P22!prod", docs.get(0).getFieldValue("id"));
+
+ assertEquals(1, docs.get(0).getFieldValues("manuals").size());
+ assertEquals(
+ SolrDocument.class, docs.get(0).getFieldValues("manuals").iterator().next().getClass());
+
+ assertEquals(2, docs.get(0).getFieldValues("skus").size());
+ final List<Object> skus = new ArrayList<>(docs.get(0).getFieldValues("skus"));
+
+ assertEquals(SolrDocument.class, skus.get(0).getClass());
+ assertEquals(SolrDocument.class, skus.get(1).getClass());
+
+ final SolrDocument red_pen = (SolrDocument) skus.get(0);
+ assertEquals("P22!S22", red_pen.getFieldValue("id"));
+
+ assertEquals(1, red_pen.getFieldValues("manuals").size());
+ assertEquals(
+ SolrDocument.class, red_pen.getFieldValues("manuals").iterator().next().getClass());
+
+ final SolrDocument red_pen_brochure =
+ (SolrDocument) red_pen.getFieldValues("manuals").iterator().next();
+ assertEquals("P22!D42", red_pen_brochure.getFieldValue("id"));
+ }
+}
diff --git a/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiHeatmapFacetingTest.java b/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiHeatmapFacetingTest.java
deleted file mode 120000
index a2ae1728ca0..00000000000
--- a/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiHeatmapFacetingTest.java
+++ /dev/null
@@ -1 +0,0 @@
-../../../../solrj/src/test/org/apache/solr/client/ref_guide_examples/JsonRequestApiHeatmapFacetingTest.java
\ No newline at end of file
diff --git a/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiHeatmapFacetingTest.java b/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiHeatmapFacetingTest.java
new file mode 100644
index 00000000000..db749b94d05
--- /dev/null
+++ b/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiHeatmapFacetingTest.java
@@ -0,0 +1,120 @@
+/*
+ * 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 java.io.File;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.request.json.HeatmapFacetMap;
+import org.apache.solr.client.solrj.request.json.JsonQueryRequest;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.client.solrj.response.json.HeatmapJsonFacet;
+import org.apache.solr.client.solrj.response.json.NestableJsonFacet;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.SolrInputDocument;
+import org.apache.solr.util.ExternalPaths;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Example SolrJ usage Heatmap facets in the JSON Request API.
+ *
+ * <p>Snippets surrounded by "tag" and "end" comments are extracted and used in the Solr Reference
+ * Guide.
+ *
+ * <p>This class is mostly copied from {@code
+ * org.apache.solr.client.solrj.request.json.JsonQueryRequestHeatmapFacetingTest}. The test was
+ * duplicated here as the community has previously decided that it's best to keep all buildable
+ * ref-guide snippets together in the same package.
+ */
+public class JsonRequestApiHeatmapFacetingTest extends SolrCloudTestCase {
+ private static final String COLLECTION_NAME = "spatialdata";
+ private static final String CONFIG_NAME = "spatialdata_config";
+ private static final String FIELD = "location_srpt";
+
+ @BeforeClass
+ public static void setupCluster() throws Exception {
+ configureCluster(1)
+ .addConfig(
+ CONFIG_NAME,
+ new File(
+ ExternalPaths.SOURCE_HOME,
+ "solrj/src/test-files/solrj/solr/configsets/spatial/conf")
+ .toPath())
+ .configure();
+
+ final List<String> solrUrls = new ArrayList<>();
+ solrUrls.add(cluster.getJettySolrRunner(0).getBaseUrl().toString());
+
+ CollectionAdminRequest.createCollection(COLLECTION_NAME, CONFIG_NAME, 1, 1)
+ .setPerReplicaState(SolrCloudTestCase.USE_PER_REPLICA_STATE)
+ .process(cluster.getSolrClient());
+
+ indexSpatialData();
+ }
+
+ private static void indexSpatialData() throws Exception {
+ final SolrInputDocument doc1 =
+ new SolrInputDocument("id", "0", FIELD, "ENVELOPE(100, 120, 80, 40)");
+ final SolrInputDocument doc2 =
+ new SolrInputDocument("id", "1", FIELD, "ENVELOPE(-120, -110, 80, 20)");
+ final SolrInputDocument doc3 = new SolrInputDocument("id", "3", FIELD, "POINT(70 60)");
+ final SolrInputDocument doc4 = new SolrInputDocument("id", "4", FIELD, "POINT(91 89)");
+ final List<SolrInputDocument> docs = new ArrayList<>();
+ docs.add(doc1);
+ docs.add(doc2);
+ docs.add(doc3);
+ docs.add(doc4);
+
+ cluster.getSolrClient().add(COLLECTION_NAME, docs);
+ cluster.getSolrClient().commit(COLLECTION_NAME);
+ }
+
+ @Test
+ public void testHeatmapFacet() throws Exception {
+ final List<List<Integer>> expectedHeatmapGrid =
+ Arrays.asList(
+ Arrays.asList(0, 0, 2, 1, 0, 0),
+ Arrays.asList(0, 0, 1, 1, 0, 0),
+ Arrays.asList(0, 1, 1, 1, 0, 0),
+ Arrays.asList(0, 0, 1, 1, 0, 0),
+ Arrays.asList(0, 0, 1, 1, 0, 0),
+ null,
+ null);
+ // tag::solrj-json-heatmap-facet-1[]
+ final JsonQueryRequest request =
+ new JsonQueryRequest()
+ .setQuery("*:*")
+ .setLimit(0)
+ .withFacet(
+ "locations",
+ new HeatmapFacetMap("location_srpt")
+ .setHeatmapFormat(HeatmapFacetMap.HeatmapFormat.INTS2D)
+ .setRegionQuery("[\"50 20\" TO \"180 90\"]")
+ .setGridLevel(4));
+ // end::solrj-json-heatmap-facet-1[]
+
+ QueryResponse response = request.process(cluster.getSolrClient(), COLLECTION_NAME);
+ final NestableJsonFacet topLevelFacet = response.getJsonFacetingResponse();
+ final HeatmapJsonFacet heatmap = topLevelFacet.getHeatmapFacetByName("locations");
+ final List<List<Integer>> actualHeatmapGrid = heatmap.getCountGrid();
+ assertEquals(expectedHeatmapGrid, actualHeatmapGrid);
+ }
+}
diff --git a/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiTest.java b/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiTest.java
deleted file mode 120000
index 3f61155f448..00000000000
--- a/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiTest.java
+++ /dev/null
@@ -1 +0,0 @@
-../../../../solrj/src/test/org/apache/solr/client/ref_guide_examples/JsonRequestApiTest.java
\ No newline at end of file
diff --git a/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiTest.java b/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiTest.java
new file mode 100644
index 00000000000..716231ff62f
--- /dev/null
+++ b/solr/solr-ref-guide/modules/query-guide/examples/JsonRequestApiTest.java
@@ -0,0 +1,785 @@
+/*
+ * 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 java.io.File;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.solr.client.solrj.SolrClient;
+import org.apache.solr.client.solrj.SolrQuery;
+import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
+import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
+import org.apache.solr.client.solrj.request.json.DomainMap;
+import org.apache.solr.client.solrj.request.json.JsonQueryRequest;
+import org.apache.solr.client.solrj.request.json.QueryFacetMap;
+import org.apache.solr.client.solrj.request.json.RangeFacetMap;
+import org.apache.solr.client.solrj.request.json.TermsFacetMap;
+import org.apache.solr.client.solrj.response.QueryResponse;
+import org.apache.solr.client.solrj.response.UpdateResponse;
+import org.apache.solr.client.solrj.response.json.BucketJsonFacet;
+import org.apache.solr.client.solrj.response.json.NestableJsonFacet;
+import org.apache.solr.cloud.SolrCloudTestCase;
+import org.apache.solr.common.SolrDocument;
+import org.apache.solr.common.params.ModifiableSolrParams;
+import org.apache.solr.util.ExternalPaths;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Example SolrJ usage of the JSON Request API.
+ *
+ * <p>Snippets surrounded by "tag" and "end" comments are extracted and used in the Solr Reference
+ * Guide.
+ */
+public class JsonRequestApiTest extends SolrCloudTestCase {
+ 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, new File(ExternalPaths.TECHPRODUCTS_CONFIGSET).toPath())
+ .configure();
+
+ final List<String> solrUrls = new ArrayList<>();
+ solrUrls.add(cluster.getJettySolrRunner(0).getBaseUrl().toString());
+
+ CollectionAdminRequest.createCollection(COLLECTION_NAME, CONFIG_NAME, 1, 1)
+ .setPerReplicaState(SolrCloudTestCase.USE_PER_REPLICA_STATE)
+ .process(cluster.getSolrClient());
+
+ ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update");
+ up.setParam("collection", COLLECTION_NAME);
+ up.addFile(getFile("solrj/techproducts.xml"), "application/xml");
+ up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
+ UpdateResponse updateResponse = up.process(cluster.getSolrClient());
+ assertEquals(0, updateResponse.getStatus());
+ }
+
+ @Test
+ public void testSimpleJsonQuery() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+ final int expectedResults = 4;
+
+ // tag::solrj-json-query-simple[]
+ final JsonQueryRequest simpleQuery =
+ new JsonQueryRequest().setQuery("memory").withFilter("inStock:true");
+ QueryResponse queryResponse = simpleQuery.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-query-simple[]
+
+ assertResponseFoundNumDocs(queryResponse, expectedResults);
+ }
+
+ @Test
+ public void testJsonQueryWithJsonQueryParamOverrides() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+ final int expectedResults = 4;
+
+ // This subtest has its own scope so that it and its twin below can can have identical variable
+ // declarations (as they appear as separate snippets in the ref-guide)
+ {
+ // tag::solrj-json-query-param-overrides[]
+ final ModifiableSolrParams overrideParams = new ModifiableSolrParams();
+ final JsonQueryRequest queryWithParamOverrides =
+ new JsonQueryRequest(overrideParams)
+ .setQuery("memory")
+ .setLimit(10)
+ .withFilter("inStock:true");
+ overrideParams.set("json.limit", 5);
+ overrideParams.add("json.filter", "\"cat:electronics\"");
+ QueryResponse queryResponse = queryWithParamOverrides.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-query-param-overrides[]
+
+ assertResponseFoundNumDocs(queryResponse, expectedResults);
+ }
+
+ // tag::solrj-json-query-param-overrides-equivalent[]
+ final JsonQueryRequest query =
+ new JsonQueryRequest()
+ .setQuery("memory")
+ .setLimit(5)
+ .withFilter("inStock:true")
+ .withFilter("cat:electronics");
+ QueryResponse queryResponse = query.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-query-param-overrides-equivalent[]
+
+ assertResponseFoundNumDocs(queryResponse, expectedResults);
+ }
+
+ @Test
+ public void testJsonFacetWithAllQueryParams() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // This subtest has its own scope so that it and its twin below can can have identical variable
+ // declarations (as they appear as separate snippets in the ref-guide)
+ {
+ // tag::solrj-json-facet-all-query-params[]
+ final ModifiableSolrParams params = new ModifiableSolrParams();
+ final SolrQuery query = new SolrQuery("*:*");
+ query.setRows(1);
+ query.setParam("json.facet.avg_price", "\"avg(price)\"");
+ query.setParam("json.facet.top_cats", "{type:terms,field:\"cat\",limit:3}");
+ QueryResponse queryResponse = solrClient.query(COLLECTION_NAME, query);
+ // end::solrj-json-facet-all-query-params[]
+
+ NestableJsonFacet topLevelFacet = queryResponse.getJsonFacetingResponse();
+ assertResponseFoundNumDocs(queryResponse, 1);
+ assertHasFacetWithBucketValues(
+ topLevelFacet,
+ "top_cats",
+ new FacetBucket("electronics", 12),
+ new FacetBucket("currency", 4),
+ new FacetBucket("memory", 3));
+ }
+
+ {
+ // tag::solrj-json-facet-all-query-params-equivalent[]
+ final JsonQueryRequest jsonQueryRequest =
+ new JsonQueryRequest()
+ .setQuery("*:*")
+ .setLimit(1)
+ .withStatFacet("avg_price", "avg(price)")
+ .withFacet("top_cats", new TermsFacetMap("cat").setLimit(3));
+ QueryResponse queryResponse = jsonQueryRequest.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-facet-all-query-params-equivalent[]
+
+ NestableJsonFacet topLevelFacet = queryResponse.getJsonFacetingResponse();
+ assertResponseFoundNumDocs(queryResponse, 1);
+ assertHasFacetWithBucketValues(
+ topLevelFacet,
+ "top_cats",
+ new FacetBucket("electronics", 12),
+ new FacetBucket("currency", 4),
+ new FacetBucket("memory", 3));
+ }
+ }
+
+ @Test
+ public void testJsonQueryUsingParamsBlock() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-query-params-block[]
+ final ModifiableSolrParams params = new ModifiableSolrParams();
+ params.set("fl", "name", "price");
+ final JsonQueryRequest simpleQuery =
+ new JsonQueryRequest(params).withParam("q", "memory").withParam("rows", 1);
+ QueryResponse queryResponse = simpleQuery.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-query-params-block[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(1, queryResponse.getResults().size());
+ final SolrDocument doc = queryResponse.getResults().get(0);
+ final Collection<String> returnedFields = doc.getFieldNames();
+ assertEquals(2, doc.getFieldNames().size());
+ assertTrue("Expected returned field list to include 'name'", returnedFields.contains("name"));
+ assertTrue("Expected returned field list to include 'price'", returnedFields.contains("price"));
+ }
+
+ @Test
+ public void testJsonQueryMacroExpansion() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-query-macro-expansion[]
+ final ModifiableSolrParams params = new ModifiableSolrParams();
+ params.set("FIELD", "text");
+ params.set("TERM", "memory");
+ final JsonQueryRequest simpleQuery = new JsonQueryRequest(params).setQuery("${FIELD}:${TERM}");
+ QueryResponse queryResponse = simpleQuery.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-query-macro-expansion[]
+
+ assertResponseFoundNumDocs(queryResponse, 5);
+ }
+
+ @Test
+ public void testJsonQueryDslBasicEquivalents() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ {
+ // tag::solrj-ipod-query-basic[]
+ final SolrQuery query = new SolrQuery("name:iPod");
+ final QueryResponse response = solrClient.query(COLLECTION_NAME, query);
+ // end::solrj-ipod-query-basic[]
+
+ assertResponseFoundNumDocs(response, 3);
+ }
+
+ {
+ // tag::solrj-ipod-query-dsl-1[]
+ final JsonQueryRequest query = new JsonQueryRequest().setQuery("name:iPod");
+ final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
+ // end::solrj-ipod-query-dsl-1[]
+
+ assertResponseFoundNumDocs(response, 3);
+ }
+
+ {
+ // tag::solrj-ipod-query-dsl-2[]
+ final JsonQueryRequest query = new JsonQueryRequest().setQuery("{!lucene df=name}iPod");
+ final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
+ // end::solrj-ipod-query-dsl-2[]
+
+ assertResponseFoundNumDocs(response, 3);
+ }
+
+ {
+ // tag::solrj-ipod-query-dsl-3[]
+ final Map<String, Object> queryTopLevel = new HashMap<>();
+ final Map<String, Object> luceneQueryProperties = new HashMap<>();
+ queryTopLevel.put("lucene", luceneQueryProperties);
+ luceneQueryProperties.put("df", "name");
+ luceneQueryProperties.put("query", "iPod");
+ final JsonQueryRequest query = new JsonQueryRequest().setQuery(queryTopLevel);
+ final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
+ // end::solrj-ipod-query-dsl-3[]
+
+ assertResponseFoundNumDocs(response, 3);
+ }
+ }
+
+ @Test
+ public void testJsonQueryDslBoostEquivalents() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+ QueryResponse[] responses = new QueryResponse[3];
+
+ {
+ // tag::solrj-ipod-query-boosted-basic[]
+ final SolrQuery query =
+ new SolrQuery("{!boost b=log(popularity) v=\'{!lucene df=name}iPod\'}");
+ final QueryResponse response = solrClient.query(COLLECTION_NAME, query);
+ // end::solrj-ipod-query-boosted-basic[]
+
+ responses[0] = response;
+ }
+
+ {
+ // tag::solrj-ipod-query-boosted-dsl-1[]
+ final Map<String, Object> queryTopLevel = new HashMap<>();
+ final Map<String, Object> boostQuery = new HashMap<>();
+ queryTopLevel.put("boost", boostQuery);
+ boostQuery.put("b", "log(popularity)");
+ boostQuery.put("query", "{!lucene df=name}iPod");
+ final JsonQueryRequest query = new JsonQueryRequest().setQuery(queryTopLevel);
+ final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
+ // end::solrj-ipod-query-boosted-dsl-1[]
+
+ responses[1] = response;
+ }
+
+ {
+ // tag::solrj-ipod-query-boosted-dsl-2[]
+ final Map<String, Object> queryTopLevel = new HashMap<>();
+ final Map<String, Object> boostProperties = new HashMap<>();
+ final Map<String, Object> luceneTopLevel = new HashMap<>();
+ final Map<String, Object> luceneProperties = new HashMap<>();
+ queryTopLevel.put("boost", boostProperties);
+ boostProperties.put("b", "log(popularity)");
+ boostProperties.put("query", luceneTopLevel);
+ luceneTopLevel.put("lucene", luceneProperties);
+ luceneProperties.put("df", "name");
+ luceneProperties.put("query", "iPod");
+ final JsonQueryRequest query = new JsonQueryRequest().setQuery(queryTopLevel);
+ final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
+ // end::solrj-ipod-query-boosted-dsl-2[]
+
+ responses[2] = response;
+ }
+
+ for (QueryResponse response : responses) {
+ assertResponseFoundNumDocs(response, 3);
+ assertEquals("MA147LL/A", response.getResults().get(0).get("id"));
+ assertEquals("F8V7067-APL-KIT", response.getResults().get(1).get("id"));
+ assertEquals("IW-02", response.getResults().get(2).get("id"));
+ }
+ }
+
+ @Test
+ public void testJsonBooleanQuery() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+ QueryResponse[] responses = new QueryResponse[3];
+
+ {
+ // tag::solrj-ipod-query-bool[]
+ final Map<String, Object> queryTopLevel = new HashMap<>();
+ final Map<String, Object> boolProperties = new HashMap<>();
+ final List<Object> mustClauses = new ArrayList<>();
+ final List<Object> mustNotClauses = new ArrayList<>();
+ final Map<String, Object> frangeTopLevel = new HashMap<>();
+ final Map<String, Object> frangeProperties = new HashMap<>();
+
+ queryTopLevel.put("bool", boolProperties);
+ boolProperties.put("must", mustClauses);
+ mustClauses.add("name:iPod");
+
+ boolProperties.put("must_not", mustNotClauses);
+ frangeTopLevel.put("frange", frangeProperties);
+ frangeProperties.put("l", 0);
+ frangeProperties.put("u", 5);
+ frangeProperties.put("query", "popularity");
+ mustNotClauses.add(frangeTopLevel);
+
+ final JsonQueryRequest query = new JsonQueryRequest().setQuery(queryTopLevel);
+ final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
+ // end::solrj-ipod-query-bool[]
+
+ responses[0] = response;
+ }
+
+ {
+ // tag::solrj-ipod-query-bool-condensed[]
+ final Map<String, Object> queryTopLevel = new HashMap<>();
+ final Map<String, Object> boolProperties = new HashMap<>();
+ final List<Object> mustClauses = new ArrayList<>();
+ final List<Object> mustNotClauses = new ArrayList<>();
+ queryTopLevel.put("bool", boolProperties);
+ boolProperties.put("must", "name:iPod");
+ boolProperties.put("must_not", "{!frange l=0 u=5}popularity");
+
+ final JsonQueryRequest query = new JsonQueryRequest().setQuery(queryTopLevel);
+ final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
+ // end::solrj-ipod-query-bool-condensed[]
+
+ responses[1] = response;
+ }
+
+ {
+ // tag::solrj-ipod-query-bool-filter[]
+ final Map<String, Object> queryTopLevel = new HashMap<>();
+ final Map<String, Object> boolProperties = new HashMap<>();
+ queryTopLevel.put("bool", boolProperties);
+ boolProperties.put("must_not", "{!frange l=0 u=5}popularity");
+
+ final JsonQueryRequest query =
+ new JsonQueryRequest().setQuery(queryTopLevel).withFilter("name:iPod");
+ final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
+ // end::solrj-ipod-query-bool-filter[]
+
+ responses[2] = response;
+ }
+
+ for (QueryResponse response : responses) {
+ assertResponseFoundNumDocs(response, 1);
+ assertEquals("MA147LL/A", response.getResults().get(0).get("id"));
+ }
+ }
+
+ @Test
+ public void testJsonTaggedQuery() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-tagged-query[]
+ final Map<String, Object> titleTaggedQuery = new HashMap<>();
+ titleTaggedQuery.put("#titleTag", "name:Solr");
+ final Map<String, Object> inStockTaggedQuery = new HashMap<>();
+ inStockTaggedQuery.put("#inStockTag", "inStock:true");
+ final JsonQueryRequest query =
+ new JsonQueryRequest()
+ .setQuery("*:*")
+ .withFilter(titleTaggedQuery)
+ .withFilter(inStockTaggedQuery);
+ final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
+ // end::solrj-tagged-query[]
+
+ assertResponseFoundNumDocs(response, 1);
+ assertEquals("SOLR1000", response.getResults().get(0).get("id"));
+ }
+
+ @Test
+ public void testSimpleJsonTermsFacet() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-simple-terms-facet[]
+ final TermsFacetMap categoryFacet = new TermsFacetMap("cat").setLimit(3);
+ final JsonQueryRequest request =
+ new JsonQueryRequest().setQuery("*:*").withFacet("categories", categoryFacet);
+ QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-simple-terms-facet[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(32, queryResponse.getResults().getNumFound());
+ assertEquals(10, queryResponse.getResults().size());
+ final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
+ assertHasFacetWithBucketValues(
+ topLevelFacetingData,
+ "categories",
+ new FacetBucket("electronics", 12),
+ new FacetBucket("currency", 4),
+ new FacetBucket("memory", 3));
+ }
+
+ @Test
+ public void testTermsFacet2() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-terms-facet-2[]
+ final TermsFacetMap categoryFacet = new TermsFacetMap("cat").setLimit(5);
+ final JsonQueryRequest request =
+ new JsonQueryRequest().setQuery("*:*").withFacet("categories", categoryFacet);
+ QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-terms-facet-2[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(32, queryResponse.getResults().getNumFound());
+ assertEquals(10, queryResponse.getResults().size());
+ final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
+ assertHasFacetWithBucketValues(
+ topLevelFacetingData,
+ "categories",
+ new FacetBucket("electronics", 12),
+ new FacetBucket("currency", 4),
+ new FacetBucket("memory", 3),
+ new FacetBucket("connector", 2),
+ new FacetBucket("graphics card", 2));
+ }
+
+ @Test
+ public void testStatFacet1() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-metrics-facet-1[]
+ final JsonQueryRequest request =
+ new JsonQueryRequest()
+ .setQuery("memory")
+ .withFilter("inStock:true")
+ .withStatFacet("avg_price", "avg(price)")
+ .withStatFacet("min_manufacturedate_dt", "min(manufacturedate_dt)")
+ .withStatFacet("num_suppliers", "unique(manu_exact)")
+ .withStatFacet("median_weight", "percentile(weight,50)");
+ QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-metrics-facet-1[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(4, queryResponse.getResults().getNumFound());
+ assertEquals(4, queryResponse.getResults().size());
+ final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
+ assertEquals(146.66, (double) topLevelFacetingData.getStatValue("avg_price"), 0.5);
+ assertEquals(3L, topLevelFacetingData.getStatValue("num_suppliers"));
+ assertEquals(352.0, (double) topLevelFacetingData.getStatValue("median_weight"), 0.5);
+
+ Object val = topLevelFacetingData.getStatValue("min_manufacturedate_dt");
+ assertTrue(val instanceof Date);
+ assertEquals("2006-02-13T15:26:37Z", ((Date) val).toInstant().toString());
+ }
+
+ @Test
+ public void testStatFacetSimple() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-metrics-facet-simple[]
+ final JsonQueryRequest request =
+ new JsonQueryRequest()
+ .setQuery("*:*")
+ .withFilter("price:[1.0 TO *]")
+ .withFilter("popularity:[0 TO 10]")
+ .withStatFacet("min_manu_id_s", "min(manu_id_s)")
+ .withStatFacet("avg_value", "avg(div(popularity,price))");
+ QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-metrics-facet-simple[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(13, queryResponse.getResults().getNumFound());
+ assertEquals(10, queryResponse.getResults().size());
+ final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
+ assertEquals(0.036, (double) topLevelFacetingData.getStatValue("avg_value"), 0.1);
+ Object val = topLevelFacetingData.getStatValue("min_manu_id_s");
+ assertTrue(val instanceof String);
+ assertEquals("apple", val.toString());
+ }
+
+ @Test
+ public void testStatFacetExpanded() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-metrics-facet-expanded[]
+ final Map<String, Object> expandedStatFacet = new HashMap<>();
+ expandedStatFacet.put("type", "func");
+ expandedStatFacet.put("func", "avg(div($numer,$denom))");
+ expandedStatFacet.put("numer", "mul(popularity,3.0)");
+ expandedStatFacet.put("denom", "price");
+ final JsonQueryRequest request =
+ new JsonQueryRequest()
+ .setQuery("*:*")
+ .withFilter("price:[1.0 TO *]")
+ .withFilter("popularity:[0 TO 10]")
+ .withFacet("avg_value", expandedStatFacet);
+ QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-metrics-facet-expanded[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(13, queryResponse.getResults().getNumFound());
+ assertEquals(10, queryResponse.getResults().size());
+ final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
+ assertEquals(0.108, (double) topLevelFacetingData.getStatValue("avg_value"), 0.1);
+ }
+
+ @Test
+ public void testQueryFacetSimple() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-query-facet-simple[]
+ QueryFacetMap queryFacet = new QueryFacetMap("popularity:[8 TO 10]");
+ final JsonQueryRequest request =
+ new JsonQueryRequest().setQuery("*:*").withFacet("high_popularity", queryFacet);
+ QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-query-facet-simple[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(32, queryResponse.getResults().getNumFound());
+ assertEquals(10, queryResponse.getResults().size());
+ final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
+ assertEquals(2, topLevelFacetingData.getQueryFacet("high_popularity").getCount());
+ }
+
+ @Test
+ public void testQueryFacetExpanded() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-query-facet-expanded[]
+ QueryFacetMap queryFacet =
+ new QueryFacetMap("popularity:[8 TO 10]").withStatSubFacet("average_price", "avg(price)");
+ final JsonQueryRequest request =
+ new JsonQueryRequest().setQuery("*:*").withFacet("high_popularity", queryFacet);
+ QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-query-facet-expanded[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(32, queryResponse.getResults().getNumFound());
+ assertEquals(10, queryResponse.getResults().size());
+ final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
+ assertEquals(2, topLevelFacetingData.getQueryFacet("high_popularity").getCount());
+ assertEquals(
+ 199.5, topLevelFacetingData.getQueryFacet("high_popularity").getStatValue("average_price"));
+ }
+
+ @Test
+ public void testRangeFacetSimple() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-range-facet-simple[]
+ RangeFacetMap rangeFacet = new RangeFacetMap("price", 0.0, 100.0, 20.0);
+ final JsonQueryRequest request =
+ new JsonQueryRequest().setQuery("*:*").withFacet("prices", rangeFacet);
+ QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-range-facet-simple[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(32, queryResponse.getResults().getNumFound());
+ assertEquals(10, queryResponse.getResults().size());
+ final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
+ assertHasFacetWithBucketValues(
+ topLevelFacetingData,
+ "prices",
+ new FacetBucket(0.0f, 5),
+ new FacetBucket(20.0f, 0),
+ new FacetBucket(40.0f, 0),
+ new FacetBucket(60.0f, 1),
+ new FacetBucket(80.0f, 1));
+ }
+
+ @Test
+ public void testNestedFacetSimple() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-nested-cat-facet[]
+ final TermsFacetMap topCategoriesFacet = new TermsFacetMap("cat").setLimit(3);
+ final TermsFacetMap topManufacturerFacet = new TermsFacetMap("manu_id_s").setLimit(1);
+ topCategoriesFacet.withSubFacet("top_manufacturers", topManufacturerFacet);
+ final JsonQueryRequest request =
+ new JsonQueryRequest().setQuery("*:*").withFacet("categories", topCategoriesFacet);
+ QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-nested-cat-facet[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(32, queryResponse.getResults().getNumFound());
+ assertEquals(10, queryResponse.getResults().size());
+ final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
+
+ assertHasFacetWithBucketValues(
+ topLevelFacetingData,
+ "categories",
+ new FacetBucket("electronics", 12),
+ new FacetBucket("currency", 4),
+ new FacetBucket("memory", 3));
+
+ // Check the top manufacturer for each category
+ List<BucketJsonFacet> catBuckets =
+ topLevelFacetingData.getBucketBasedFacets("categories").getBuckets();
+ assertHasFacetWithBucketValues(
+ catBuckets.get(0), "top_manufacturers", new FacetBucket("corsair", 3));
+ assertHasFacetWithBucketValues(
+ catBuckets.get(1), "top_manufacturers", new FacetBucket("boa", 1));
+ assertHasFacetWithBucketValues(
+ catBuckets.get(2), "top_manufacturers", new FacetBucket("corsair", 3));
+ }
+
+ @Test
+ public void testFacetSortedByNestedMetric() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-nested-cat-facet-sorted[]
+ final TermsFacetMap topCategoriesFacet =
+ new TermsFacetMap("cat")
+ .setLimit(3)
+ .withStatSubFacet("avg_price", "avg(price)")
+ .setSort("avg_price desc");
+ final JsonQueryRequest request =
+ new JsonQueryRequest().setQuery("*:*").withFacet("categories", topCategoriesFacet);
+ QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-nested-cat-facet-sorted[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(32, queryResponse.getResults().getNumFound());
+ assertEquals(10, queryResponse.getResults().size());
+ final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
+ assertHasFacetWithBucketValues(
+ topLevelFacetingData,
+ "categories",
+ new FacetBucket("electronics and computer1", 1),
+ new FacetBucket("graphics card", 2),
+ new FacetBucket("music", 1));
+ }
+
+ @Test
+ public void testFacetFilteredDomain() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-facet-filtered-domain[]
+ final TermsFacetMap categoryFacet =
+ new TermsFacetMap("cat")
+ .setLimit(3)
+ .withDomain(new DomainMap().withFilter("popularity:[5 TO 10]"));
+ final JsonQueryRequest request =
+ new JsonQueryRequest().setQuery("*:*").withFacet("categories", categoryFacet);
+ QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-facet-filtered-domain[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(32, queryResponse.getResults().getNumFound());
+ assertEquals(10, queryResponse.getResults().size());
+ final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
+ assertHasFacetWithBucketValues(
+ topLevelFacetingData,
+ "categories",
+ new FacetBucket("electronics", 9),
+ new FacetBucket("graphics card", 2),
+ new FacetBucket("hard drive", 2));
+ }
+
+ @Test
+ public void testFacetWidenedExcludeTagsDomain() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-facet-excludetags-domain[]
+ final TermsFacetMap inStockFacet = new TermsFacetMap("inStock").setLimit(2);
+ final TermsFacetMap allManufacturersFacet =
+ new TermsFacetMap("manu_id_s")
+ .setLimit(2)
+ .withDomain(new DomainMap().withTagsToExclude("MANU"));
+ final JsonQueryRequest request =
+ new JsonQueryRequest()
+ .setQuery("cat:electronics")
+ .withFilter("{!tag=MANU}manu_id_s:apple")
+ .withFacet("stock", inStockFacet)
+ .withFacet("manufacturers", allManufacturersFacet);
+ QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-facet-excludetags-domain[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(1, queryResponse.getResults().getNumFound());
+ assertEquals(1, queryResponse.getResults().size());
+ final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
+ assertHasFacetWithBucketValues(topLevelFacetingData, "stock", new FacetBucket(true, 1));
+ assertHasFacetWithBucketValues(
+ topLevelFacetingData,
+ "manufacturers",
+ new FacetBucket("corsair", 3),
+ new FacetBucket("belkin", 2));
+ }
+
+ @Test
+ public void testFacetWidenedUsingQueryDomain() throws Exception {
+ SolrClient solrClient = cluster.getSolrClient();
+
+ // tag::solrj-json-facet-query-domain[]
+ final TermsFacetMap inStockFacet = new TermsFacetMap("inStock").setLimit(2);
+ final TermsFacetMap popularCategoriesFacet =
+ new TermsFacetMap("cat")
+ .withDomain(new DomainMap().withQuery("popularity:[8 TO 10]"))
+ .setLimit(3);
+ final JsonQueryRequest request =
+ new JsonQueryRequest()
+ .setQuery("apple")
+ .withFacet("popular_categories", popularCategoriesFacet);
+ QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
+ // end::solrj-json-facet-query-domain[]
+
+ assertEquals(0, queryResponse.getStatus());
+ assertEquals(1, queryResponse.getResults().getNumFound());
+ assertEquals(1, queryResponse.getResults().size());
+ final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
+ assertHasFacetWithBucketValues(
+ topLevelFacetingData,
+ "popular_categories",
+ new FacetBucket("electronics", 1),
+ new FacetBucket("music", 1),
+ new FacetBucket("search", 1));
+ }
+
+ private class FacetBucket {
+ private final Object val;
+ private final int count;
+
+ FacetBucket(Object val, int count) {
+ this.val = val;
+ this.count = count;
+ }
+
+ public Object getVal() {
+ return val;
+ }
+
+ public int getCount() {
+ return count;
+ }
+ }
+
+ private void assertHasFacetWithBucketValues(
+ NestableJsonFacet response, String expectedFacetName, FacetBucket... expectedBuckets) {
+ assertTrue(
+ "Expected response to have facet with name " + expectedFacetName,
+ response.getBucketBasedFacets(expectedFacetName) != null);
+ final List<BucketJsonFacet> buckets =
+ response.getBucketBasedFacets(expectedFacetName).getBuckets();
+ assertEquals(expectedBuckets.length, buckets.size());
+ for (int i = 0; i < expectedBuckets.length; i++) {
+ final FacetBucket expectedBucket = expectedBuckets[i];
+ final BucketJsonFacet actualBucket = buckets.get(i);
+ assertEquals(expectedBucket.getVal(), actualBucket.getVal());
+ assertEquals(expectedBucket.getCount(), actualBucket.getCount());
+ }
+ }
+
+ private void assertResponseFoundNumDocs(QueryResponse response, int expectedNumDocs) {
+ assertEquals(0, response.getStatus());
+ assertEquals(expectedNumDocs, response.getResults().size());
+ }
+}
diff --git a/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/IndexingNestedDocuments.java b/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/IndexingNestedDocuments.java
deleted file mode 100644
index efecb4ed928..00000000000
--- a/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/IndexingNestedDocuments.java
+++ /dev/null
@@ -1,285 +0,0 @@
-/*
- * 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 java.io.File;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import org.apache.solr.client.solrj.SolrClient;
-import org.apache.solr.client.solrj.SolrQuery;
-import org.apache.solr.client.solrj.request.CollectionAdminRequest;
-import org.apache.solr.cloud.SolrCloudTestCase;
-import org.apache.solr.common.SolrDocument;
-import org.apache.solr.common.SolrDocumentList;
-import org.apache.solr.common.SolrInputDocument;
-import org.apache.solr.util.ExternalPaths;
-import org.junit.After;
-import org.junit.BeforeClass;
-
-/**
- * Example SolrJ usage for indexing nested documents
- *
- * <p>Snippets surrounded by "tag" and "end" comments are extracted and used in the Solr Reference
- * Guide.
- */
-public class IndexingNestedDocuments extends SolrCloudTestCase {
- public static final String ANON_KIDS_CONFIG = "anon_kids_configset";
-
- @BeforeClass
- public static void setupCluster() throws Exception {
- configureCluster(1)
- // when indexing 'anonymous' kids, we need a schema that doesn't use _nest_path_ so
- // that we can use [child] transformer with a parentFilter...
- .addConfig(ANON_KIDS_CONFIG, new File(ExternalPaths.TECHPRODUCTS_CONFIGSET).toPath())
- .configure();
- }
-
- @After
- public void cleanCollections() throws Exception {
- cluster.deleteAllCollections();
- }
-
- /** Syntactic sugar so code snippet doesn't refer to test-framework specific method name */
- public static SolrClient getSolrClient() {
- return cluster.getSolrClient();
- }
-
- /**
- * Demo of using anonymous children when indexing hierarchical documents. This test code is used
- * as an 'include' from the ref-guide
- */
- public void testIndexingAnonKids() throws Exception {
- final String collection = "test_anon";
- CollectionAdminRequest.createCollection(collection, ANON_KIDS_CONFIG, 1, 1)
- .setPerReplicaState(SolrCloudTestCase.USE_PER_REPLICA_STATE)
- .process(cluster.getSolrClient());
- cluster.getSolrClient().setDefaultCollection(collection);
-
- //
- // DO NOT MODIFY THESE EXAMPLE DOCS WITH OUT MAKING THE SAME CHANGES TO THE JSON AND XML
- // EQUIVILENT EXAMPLES IN 'indexing-nested-documents.adoc'
- //
-
- // tag::anon-kids[]
- final SolrClient client = getSolrClient();
-
- final SolrInputDocument p1 = new SolrInputDocument();
- p1.setField("id", "P11!prod");
- p1.setField("type_s", "PRODUCT");
- p1.setField("name_s", "Swingline Stapler");
- p1.setField("description_t", "The Cadillac of office staplers ...");
- {
- final SolrInputDocument s1 = new SolrInputDocument();
- s1.setField("id", "P11!S21");
- s1.setField("type_s", "SKU");
- s1.setField("color_s", "RED");
- s1.setField("price_i", 42);
- {
- final SolrInputDocument m1 = new SolrInputDocument();
- m1.setField("id", "P11!D41");
- m1.setField("type_s", "MANUAL");
- m1.setField("name_s", "Red Swingline Brochure");
- m1.setField("pages_i", 1);
- m1.setField("content_t", "...");
-
- s1.addChildDocument(m1);
- }
-
- final SolrInputDocument s2 = new SolrInputDocument();
- s2.setField("id", "P11!S31");
- s2.setField("type_s", "SKU");
- s2.setField("color_s", "BLACK");
- s2.setField("price_i", 3);
-
- final SolrInputDocument m1 = new SolrInputDocument();
- m1.setField("id", "P11!D51");
- m1.setField("type_s", "MANUAL");
- m1.setField("name_s", "Quick Reference Guide");
- m1.setField("pages_i", 1);
- m1.setField("content_t", "How to use your stapler ...");
-
- final SolrInputDocument m2 = new SolrInputDocument();
- m2.setField("id", "P11!D61");
- m2.setField("type_s", "MANUAL");
- m2.setField("name_s", "Warranty Details");
- m2.setField("pages_i", 42);
- m2.setField("content_t", "... lifetime guarantee ...");
-
- p1.addChildDocuments(Arrays.asList(s1, s2, m1, m2));
- }
-
- client.add(p1);
- // end::anon-kids[]
-
- client.commit();
-
- final SolrDocumentList docs =
- getSolrClient()
- .query(
- new SolrQuery("description_t:Cadillac")
- .set("fl", "*,[child parentFilter='type_s:PRODUCT']"))
- .getResults();
-
- 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());
- assertEquals(5, docs.get(0).getChildDocuments().size());
-
- // flat list is depth first...
- final SolrDocument red_stapler_brochure = docs.get(0).getChildDocuments().get(0);
- assertEquals("P11!D41", red_stapler_brochure.getFieldValue("id"));
-
- final SolrDocument red_stapler = docs.get(0).getChildDocuments().get(1);
- assertEquals("P11!S21", red_stapler.getFieldValue("id"));
- }
-
- /**
- * Demo of using <code>NestPath</code> related pseudo-fields when indexing hierarchical documents.
- * This test code is used as an 'include' from the ref-guide
- */
- public void testIndexingUsingNestPath() throws Exception {
- final String collection = "test_anon";
- CollectionAdminRequest.createCollection(collection, 1, 1).process(cluster.getSolrClient());
- cluster.getSolrClient().setDefaultCollection(collection);
-
- //
- // DO NOT MODIFY THESE EXAMPLE DOCS WITH OUT MAKING THE SAME CHANGES TO THE JSON AND XML
- // EQUIVILENT EXAMPLES IN 'indexing-nested-documents.adoc'
- //
-
- // tag::nest-path[]
- final SolrClient client = getSolrClient();
-
- final SolrInputDocument p1 = new SolrInputDocument();
- p1.setField("id", "P11!prod");
- p1.setField("name_s", "Swingline Stapler");
- p1.setField("description_t", "The Cadillac of office staplers ...");
- {
- final SolrInputDocument s1 = new SolrInputDocument();
- s1.setField("id", "P11!S21");
- s1.setField("color_s", "RED");
- s1.setField("price_i", 42);
- {
- final SolrInputDocument m1 = new SolrInputDocument();
- m1.setField("id", "P11!D41");
- m1.setField("name_s", "Red Swingline Brochure");
- m1.setField("pages_i", 1);
- m1.setField("content_t", "...");
-
- s1.setField("manuals", m1);
- }
-
- final SolrInputDocument s2 = new SolrInputDocument();
- s2.setField("id", "P11!S31");
- s2.setField("color_s", "BLACK");
- s2.setField("price_i", 3);
-
- p1.setField("skus", Arrays.asList(s1, s2));
- }
- {
- final SolrInputDocument m1 = new SolrInputDocument();
- m1.setField("id", "P11!D51");
- m1.setField("name_s", "Quick Reference Guide");
- m1.setField("pages_i", 1);
- m1.setField("content_t", "How to use your stapler ...");
-
- final SolrInputDocument m2 = new SolrInputDocument();
- m2.setField("id", "P11!D61");
- m2.setField("name_s", "Warranty Details");
- m2.setField("pages_i", 42);
- m2.setField("content_t", "... lifetime guarantee ...");
-
- p1.setField("manuals", Arrays.asList(m1, m2));
- }
-
- final SolrInputDocument p2 = new SolrInputDocument();
- p2.setField("id", "P22!prod");
- p2.setField("name_s", "Mont Blanc Fountain Pen");
- p2.setField("description_t", "A Premium Writing Instrument ...");
- {
- final SolrInputDocument s1 = new SolrInputDocument();
- s1.setField("id", "P22!S22");
- s1.setField("color_s", "RED");
- s1.setField("price_i", 89);
- {
- final SolrInputDocument m1 = new SolrInputDocument();
- m1.setField("id", "P22!D42");
- m1.setField("name_s", "Red Mont Blanc Brochure");
- m1.setField("pages_i", 1);
- m1.setField("content_t", "...");
-
- s1.setField("manuals", m1);
- }
-
- final SolrInputDocument s2 = new SolrInputDocument();
- s2.setField("id", "P22!S32");
- s2.setField("color_s", "BLACK");
- s2.setField("price_i", 67);
-
- p2.setField("skus", Arrays.asList(s1, s2));
- }
- {
- final SolrInputDocument m1 = new SolrInputDocument();
- m1.setField("id", "P22!D52");
- m1.setField("name_s", "How To Use A Pen");
- m1.setField("pages_i", 42);
- m1.setField("content_t", "Start by removing the cap ...");
-
- p2.setField("manuals", m1);
- }
-
- client.add(Arrays.asList(p1, p2));
- // end::nest-path[]
-
- client.commit();
-
- // Now a quick sanity check that the nest path is working properly...
-
- final SolrDocumentList docs =
- getSolrClient()
- .query(new SolrQuery("description_t:Writing").set("fl", "*,[child]"))
- .getResults();
-
- assertEquals(1, docs.getNumFound());
- assertEquals("P22!prod", docs.get(0).getFieldValue("id"));
-
- assertEquals(1, docs.get(0).getFieldValues("manuals").size());
- assertEquals(
- SolrDocument.class, docs.get(0).getFieldValues("manuals").iterator().next().getClass());
-
- assertEquals(2, docs.get(0).getFieldValues("skus").size());
- final List<Object> skus = new ArrayList<>(docs.get(0).getFieldValues("skus"));
-
- assertEquals(SolrDocument.class, skus.get(0).getClass());
- assertEquals(SolrDocument.class, skus.get(1).getClass());
-
- final SolrDocument red_pen = (SolrDocument) skus.get(0);
- assertEquals("P22!S22", red_pen.getFieldValue("id"));
-
- assertEquals(1, red_pen.getFieldValues("manuals").size());
- assertEquals(
- SolrDocument.class, red_pen.getFieldValues("manuals").iterator().next().getClass());
-
- final SolrDocument red_pen_brochure =
- (SolrDocument) red_pen.getFieldValues("manuals").iterator().next();
- assertEquals("P22!D42", red_pen_brochure.getFieldValue("id"));
- }
-}
diff --git a/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/JsonRequestApiHeatmapFacetingTest.java b/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/JsonRequestApiHeatmapFacetingTest.java
deleted file mode 100644
index 4262fdc6976..00000000000
--- a/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/JsonRequestApiHeatmapFacetingTest.java
+++ /dev/null
@@ -1,120 +0,0 @@
-/*
- * 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 java.io.File;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import org.apache.solr.client.solrj.request.CollectionAdminRequest;
-import org.apache.solr.client.solrj.request.json.HeatmapFacetMap;
-import org.apache.solr.client.solrj.request.json.JsonQueryRequest;
-import org.apache.solr.client.solrj.response.QueryResponse;
-import org.apache.solr.client.solrj.response.json.HeatmapJsonFacet;
-import org.apache.solr.client.solrj.response.json.NestableJsonFacet;
-import org.apache.solr.cloud.SolrCloudTestCase;
-import org.apache.solr.common.SolrInputDocument;
-import org.apache.solr.util.ExternalPaths;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-/**
- * Example SolrJ usage Heatmap facets in the JSON Request API.
- *
- * <p>Snippets surrounded by "tag" and "end" comments are extracted and used in the Solr Reference
- * Guide.
- *
- * <p>This class is mostly copied from {@link
- * org.apache.solr.client.solrj.request.json.JsonQueryRequestHeatmapFacetingTest}. The test was
- * duplicated here as the community has previously decided that it's best to keep all buildable
- * ref-guide snippets together in the same package.
- */
-public class JsonRequestApiHeatmapFacetingTest extends SolrCloudTestCase {
- private static final String COLLECTION_NAME = "spatialdata";
- private static final String CONFIG_NAME = "spatialdata_config";
- private static final String FIELD = "location_srpt";
-
- @BeforeClass
- public static void setupCluster() throws Exception {
- configureCluster(1)
- .addConfig(
- CONFIG_NAME,
- new File(
- ExternalPaths.SOURCE_HOME,
- "solrj/src/test-files/solrj/solr/configsets/spatial/conf")
- .toPath())
- .configure();
-
- final List<String> solrUrls = new ArrayList<>();
- solrUrls.add(cluster.getJettySolrRunner(0).getBaseUrl().toString());
-
- CollectionAdminRequest.createCollection(COLLECTION_NAME, CONFIG_NAME, 1, 1)
- .setPerReplicaState(SolrCloudTestCase.USE_PER_REPLICA_STATE)
- .process(cluster.getSolrClient());
-
- indexSpatialData();
- }
-
- private static void indexSpatialData() throws Exception {
- final SolrInputDocument doc1 =
- new SolrInputDocument("id", "0", FIELD, "ENVELOPE(100, 120, 80, 40)");
- final SolrInputDocument doc2 =
- new SolrInputDocument("id", "1", FIELD, "ENVELOPE(-120, -110, 80, 20)");
- final SolrInputDocument doc3 = new SolrInputDocument("id", "3", FIELD, "POINT(70 60)");
- final SolrInputDocument doc4 = new SolrInputDocument("id", "4", FIELD, "POINT(91 89)");
- final List<SolrInputDocument> docs = new ArrayList<>();
- docs.add(doc1);
- docs.add(doc2);
- docs.add(doc3);
- docs.add(doc4);
-
- cluster.getSolrClient().add(COLLECTION_NAME, docs);
- cluster.getSolrClient().commit(COLLECTION_NAME);
- }
-
- @Test
- public void testHeatmapFacet() throws Exception {
- final List<List<Integer>> expectedHeatmapGrid =
- Arrays.asList(
- Arrays.asList(0, 0, 2, 1, 0, 0),
- Arrays.asList(0, 0, 1, 1, 0, 0),
- Arrays.asList(0, 1, 1, 1, 0, 0),
- Arrays.asList(0, 0, 1, 1, 0, 0),
- Arrays.asList(0, 0, 1, 1, 0, 0),
- null,
- null);
- // tag::solrj-json-heatmap-facet-1[]
- final JsonQueryRequest request =
- new JsonQueryRequest()
- .setQuery("*:*")
- .setLimit(0)
- .withFacet(
- "locations",
- new HeatmapFacetMap("location_srpt")
- .setHeatmapFormat(HeatmapFacetMap.HeatmapFormat.INTS2D)
- .setRegionQuery("[\"50 20\" TO \"180 90\"]")
- .setGridLevel(4));
- // end::solrj-json-heatmap-facet-1[]
-
- QueryResponse response = request.process(cluster.getSolrClient(), COLLECTION_NAME);
- final NestableJsonFacet topLevelFacet = response.getJsonFacetingResponse();
- final HeatmapJsonFacet heatmap = topLevelFacet.getHeatmapFacetByName("locations");
- final List<List<Integer>> actualHeatmapGrid = heatmap.getCountGrid();
- assertEquals(expectedHeatmapGrid, actualHeatmapGrid);
- }
-}
diff --git a/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/JsonRequestApiTest.java b/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/JsonRequestApiTest.java
deleted file mode 100644
index 716231ff62f..00000000000
--- a/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/JsonRequestApiTest.java
+++ /dev/null
@@ -1,785 +0,0 @@
-/*
- * 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 java.io.File;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import org.apache.solr.client.solrj.SolrClient;
-import org.apache.solr.client.solrj.SolrQuery;
-import org.apache.solr.client.solrj.request.AbstractUpdateRequest;
-import org.apache.solr.client.solrj.request.CollectionAdminRequest;
-import org.apache.solr.client.solrj.request.ContentStreamUpdateRequest;
-import org.apache.solr.client.solrj.request.json.DomainMap;
-import org.apache.solr.client.solrj.request.json.JsonQueryRequest;
-import org.apache.solr.client.solrj.request.json.QueryFacetMap;
-import org.apache.solr.client.solrj.request.json.RangeFacetMap;
-import org.apache.solr.client.solrj.request.json.TermsFacetMap;
-import org.apache.solr.client.solrj.response.QueryResponse;
-import org.apache.solr.client.solrj.response.UpdateResponse;
-import org.apache.solr.client.solrj.response.json.BucketJsonFacet;
-import org.apache.solr.client.solrj.response.json.NestableJsonFacet;
-import org.apache.solr.cloud.SolrCloudTestCase;
-import org.apache.solr.common.SolrDocument;
-import org.apache.solr.common.params.ModifiableSolrParams;
-import org.apache.solr.util.ExternalPaths;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-/**
- * Example SolrJ usage of the JSON Request API.
- *
- * <p>Snippets surrounded by "tag" and "end" comments are extracted and used in the Solr Reference
- * Guide.
- */
-public class JsonRequestApiTest extends SolrCloudTestCase {
- 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, new File(ExternalPaths.TECHPRODUCTS_CONFIGSET).toPath())
- .configure();
-
- final List<String> solrUrls = new ArrayList<>();
- solrUrls.add(cluster.getJettySolrRunner(0).getBaseUrl().toString());
-
- CollectionAdminRequest.createCollection(COLLECTION_NAME, CONFIG_NAME, 1, 1)
- .setPerReplicaState(SolrCloudTestCase.USE_PER_REPLICA_STATE)
- .process(cluster.getSolrClient());
-
- ContentStreamUpdateRequest up = new ContentStreamUpdateRequest("/update");
- up.setParam("collection", COLLECTION_NAME);
- up.addFile(getFile("solrj/techproducts.xml"), "application/xml");
- up.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
- UpdateResponse updateResponse = up.process(cluster.getSolrClient());
- assertEquals(0, updateResponse.getStatus());
- }
-
- @Test
- public void testSimpleJsonQuery() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
- final int expectedResults = 4;
-
- // tag::solrj-json-query-simple[]
- final JsonQueryRequest simpleQuery =
- new JsonQueryRequest().setQuery("memory").withFilter("inStock:true");
- QueryResponse queryResponse = simpleQuery.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-query-simple[]
-
- assertResponseFoundNumDocs(queryResponse, expectedResults);
- }
-
- @Test
- public void testJsonQueryWithJsonQueryParamOverrides() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
- final int expectedResults = 4;
-
- // This subtest has its own scope so that it and its twin below can can have identical variable
- // declarations (as they appear as separate snippets in the ref-guide)
- {
- // tag::solrj-json-query-param-overrides[]
- final ModifiableSolrParams overrideParams = new ModifiableSolrParams();
- final JsonQueryRequest queryWithParamOverrides =
- new JsonQueryRequest(overrideParams)
- .setQuery("memory")
- .setLimit(10)
- .withFilter("inStock:true");
- overrideParams.set("json.limit", 5);
- overrideParams.add("json.filter", "\"cat:electronics\"");
- QueryResponse queryResponse = queryWithParamOverrides.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-query-param-overrides[]
-
- assertResponseFoundNumDocs(queryResponse, expectedResults);
- }
-
- // tag::solrj-json-query-param-overrides-equivalent[]
- final JsonQueryRequest query =
- new JsonQueryRequest()
- .setQuery("memory")
- .setLimit(5)
- .withFilter("inStock:true")
- .withFilter("cat:electronics");
- QueryResponse queryResponse = query.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-query-param-overrides-equivalent[]
-
- assertResponseFoundNumDocs(queryResponse, expectedResults);
- }
-
- @Test
- public void testJsonFacetWithAllQueryParams() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // This subtest has its own scope so that it and its twin below can can have identical variable
- // declarations (as they appear as separate snippets in the ref-guide)
- {
- // tag::solrj-json-facet-all-query-params[]
- final ModifiableSolrParams params = new ModifiableSolrParams();
- final SolrQuery query = new SolrQuery("*:*");
- query.setRows(1);
- query.setParam("json.facet.avg_price", "\"avg(price)\"");
- query.setParam("json.facet.top_cats", "{type:terms,field:\"cat\",limit:3}");
- QueryResponse queryResponse = solrClient.query(COLLECTION_NAME, query);
- // end::solrj-json-facet-all-query-params[]
-
- NestableJsonFacet topLevelFacet = queryResponse.getJsonFacetingResponse();
- assertResponseFoundNumDocs(queryResponse, 1);
- assertHasFacetWithBucketValues(
- topLevelFacet,
- "top_cats",
- new FacetBucket("electronics", 12),
- new FacetBucket("currency", 4),
- new FacetBucket("memory", 3));
- }
-
- {
- // tag::solrj-json-facet-all-query-params-equivalent[]
- final JsonQueryRequest jsonQueryRequest =
- new JsonQueryRequest()
- .setQuery("*:*")
- .setLimit(1)
- .withStatFacet("avg_price", "avg(price)")
- .withFacet("top_cats", new TermsFacetMap("cat").setLimit(3));
- QueryResponse queryResponse = jsonQueryRequest.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-facet-all-query-params-equivalent[]
-
- NestableJsonFacet topLevelFacet = queryResponse.getJsonFacetingResponse();
- assertResponseFoundNumDocs(queryResponse, 1);
- assertHasFacetWithBucketValues(
- topLevelFacet,
- "top_cats",
- new FacetBucket("electronics", 12),
- new FacetBucket("currency", 4),
- new FacetBucket("memory", 3));
- }
- }
-
- @Test
- public void testJsonQueryUsingParamsBlock() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-query-params-block[]
- final ModifiableSolrParams params = new ModifiableSolrParams();
- params.set("fl", "name", "price");
- final JsonQueryRequest simpleQuery =
- new JsonQueryRequest(params).withParam("q", "memory").withParam("rows", 1);
- QueryResponse queryResponse = simpleQuery.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-query-params-block[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(1, queryResponse.getResults().size());
- final SolrDocument doc = queryResponse.getResults().get(0);
- final Collection<String> returnedFields = doc.getFieldNames();
- assertEquals(2, doc.getFieldNames().size());
- assertTrue("Expected returned field list to include 'name'", returnedFields.contains("name"));
- assertTrue("Expected returned field list to include 'price'", returnedFields.contains("price"));
- }
-
- @Test
- public void testJsonQueryMacroExpansion() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-query-macro-expansion[]
- final ModifiableSolrParams params = new ModifiableSolrParams();
- params.set("FIELD", "text");
- params.set("TERM", "memory");
- final JsonQueryRequest simpleQuery = new JsonQueryRequest(params).setQuery("${FIELD}:${TERM}");
- QueryResponse queryResponse = simpleQuery.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-query-macro-expansion[]
-
- assertResponseFoundNumDocs(queryResponse, 5);
- }
-
- @Test
- public void testJsonQueryDslBasicEquivalents() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- {
- // tag::solrj-ipod-query-basic[]
- final SolrQuery query = new SolrQuery("name:iPod");
- final QueryResponse response = solrClient.query(COLLECTION_NAME, query);
- // end::solrj-ipod-query-basic[]
-
- assertResponseFoundNumDocs(response, 3);
- }
-
- {
- // tag::solrj-ipod-query-dsl-1[]
- final JsonQueryRequest query = new JsonQueryRequest().setQuery("name:iPod");
- final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
- // end::solrj-ipod-query-dsl-1[]
-
- assertResponseFoundNumDocs(response, 3);
- }
-
- {
- // tag::solrj-ipod-query-dsl-2[]
- final JsonQueryRequest query = new JsonQueryRequest().setQuery("{!lucene df=name}iPod");
- final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
- // end::solrj-ipod-query-dsl-2[]
-
- assertResponseFoundNumDocs(response, 3);
- }
-
- {
- // tag::solrj-ipod-query-dsl-3[]
- final Map<String, Object> queryTopLevel = new HashMap<>();
- final Map<String, Object> luceneQueryProperties = new HashMap<>();
- queryTopLevel.put("lucene", luceneQueryProperties);
- luceneQueryProperties.put("df", "name");
- luceneQueryProperties.put("query", "iPod");
- final JsonQueryRequest query = new JsonQueryRequest().setQuery(queryTopLevel);
- final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
- // end::solrj-ipod-query-dsl-3[]
-
- assertResponseFoundNumDocs(response, 3);
- }
- }
-
- @Test
- public void testJsonQueryDslBoostEquivalents() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
- QueryResponse[] responses = new QueryResponse[3];
-
- {
- // tag::solrj-ipod-query-boosted-basic[]
- final SolrQuery query =
- new SolrQuery("{!boost b=log(popularity) v=\'{!lucene df=name}iPod\'}");
- final QueryResponse response = solrClient.query(COLLECTION_NAME, query);
- // end::solrj-ipod-query-boosted-basic[]
-
- responses[0] = response;
- }
-
- {
- // tag::solrj-ipod-query-boosted-dsl-1[]
- final Map<String, Object> queryTopLevel = new HashMap<>();
- final Map<String, Object> boostQuery = new HashMap<>();
- queryTopLevel.put("boost", boostQuery);
- boostQuery.put("b", "log(popularity)");
- boostQuery.put("query", "{!lucene df=name}iPod");
- final JsonQueryRequest query = new JsonQueryRequest().setQuery(queryTopLevel);
- final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
- // end::solrj-ipod-query-boosted-dsl-1[]
-
- responses[1] = response;
- }
-
- {
- // tag::solrj-ipod-query-boosted-dsl-2[]
- final Map<String, Object> queryTopLevel = new HashMap<>();
- final Map<String, Object> boostProperties = new HashMap<>();
- final Map<String, Object> luceneTopLevel = new HashMap<>();
- final Map<String, Object> luceneProperties = new HashMap<>();
- queryTopLevel.put("boost", boostProperties);
- boostProperties.put("b", "log(popularity)");
- boostProperties.put("query", luceneTopLevel);
- luceneTopLevel.put("lucene", luceneProperties);
- luceneProperties.put("df", "name");
- luceneProperties.put("query", "iPod");
- final JsonQueryRequest query = new JsonQueryRequest().setQuery(queryTopLevel);
- final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
- // end::solrj-ipod-query-boosted-dsl-2[]
-
- responses[2] = response;
- }
-
- for (QueryResponse response : responses) {
- assertResponseFoundNumDocs(response, 3);
- assertEquals("MA147LL/A", response.getResults().get(0).get("id"));
- assertEquals("F8V7067-APL-KIT", response.getResults().get(1).get("id"));
- assertEquals("IW-02", response.getResults().get(2).get("id"));
- }
- }
-
- @Test
- public void testJsonBooleanQuery() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
- QueryResponse[] responses = new QueryResponse[3];
-
- {
- // tag::solrj-ipod-query-bool[]
- final Map<String, Object> queryTopLevel = new HashMap<>();
- final Map<String, Object> boolProperties = new HashMap<>();
- final List<Object> mustClauses = new ArrayList<>();
- final List<Object> mustNotClauses = new ArrayList<>();
- final Map<String, Object> frangeTopLevel = new HashMap<>();
- final Map<String, Object> frangeProperties = new HashMap<>();
-
- queryTopLevel.put("bool", boolProperties);
- boolProperties.put("must", mustClauses);
- mustClauses.add("name:iPod");
-
- boolProperties.put("must_not", mustNotClauses);
- frangeTopLevel.put("frange", frangeProperties);
- frangeProperties.put("l", 0);
- frangeProperties.put("u", 5);
- frangeProperties.put("query", "popularity");
- mustNotClauses.add(frangeTopLevel);
-
- final JsonQueryRequest query = new JsonQueryRequest().setQuery(queryTopLevel);
- final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
- // end::solrj-ipod-query-bool[]
-
- responses[0] = response;
- }
-
- {
- // tag::solrj-ipod-query-bool-condensed[]
- final Map<String, Object> queryTopLevel = new HashMap<>();
- final Map<String, Object> boolProperties = new HashMap<>();
- final List<Object> mustClauses = new ArrayList<>();
- final List<Object> mustNotClauses = new ArrayList<>();
- queryTopLevel.put("bool", boolProperties);
- boolProperties.put("must", "name:iPod");
- boolProperties.put("must_not", "{!frange l=0 u=5}popularity");
-
- final JsonQueryRequest query = new JsonQueryRequest().setQuery(queryTopLevel);
- final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
- // end::solrj-ipod-query-bool-condensed[]
-
- responses[1] = response;
- }
-
- {
- // tag::solrj-ipod-query-bool-filter[]
- final Map<String, Object> queryTopLevel = new HashMap<>();
- final Map<String, Object> boolProperties = new HashMap<>();
- queryTopLevel.put("bool", boolProperties);
- boolProperties.put("must_not", "{!frange l=0 u=5}popularity");
-
- final JsonQueryRequest query =
- new JsonQueryRequest().setQuery(queryTopLevel).withFilter("name:iPod");
- final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
- // end::solrj-ipod-query-bool-filter[]
-
- responses[2] = response;
- }
-
- for (QueryResponse response : responses) {
- assertResponseFoundNumDocs(response, 1);
- assertEquals("MA147LL/A", response.getResults().get(0).get("id"));
- }
- }
-
- @Test
- public void testJsonTaggedQuery() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-tagged-query[]
- final Map<String, Object> titleTaggedQuery = new HashMap<>();
- titleTaggedQuery.put("#titleTag", "name:Solr");
- final Map<String, Object> inStockTaggedQuery = new HashMap<>();
- inStockTaggedQuery.put("#inStockTag", "inStock:true");
- final JsonQueryRequest query =
- new JsonQueryRequest()
- .setQuery("*:*")
- .withFilter(titleTaggedQuery)
- .withFilter(inStockTaggedQuery);
- final QueryResponse response = query.process(solrClient, COLLECTION_NAME);
- // end::solrj-tagged-query[]
-
- assertResponseFoundNumDocs(response, 1);
- assertEquals("SOLR1000", response.getResults().get(0).get("id"));
- }
-
- @Test
- public void testSimpleJsonTermsFacet() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-simple-terms-facet[]
- final TermsFacetMap categoryFacet = new TermsFacetMap("cat").setLimit(3);
- final JsonQueryRequest request =
- new JsonQueryRequest().setQuery("*:*").withFacet("categories", categoryFacet);
- QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-simple-terms-facet[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(32, queryResponse.getResults().getNumFound());
- assertEquals(10, queryResponse.getResults().size());
- final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
- assertHasFacetWithBucketValues(
- topLevelFacetingData,
- "categories",
- new FacetBucket("electronics", 12),
- new FacetBucket("currency", 4),
- new FacetBucket("memory", 3));
- }
-
- @Test
- public void testTermsFacet2() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-terms-facet-2[]
- final TermsFacetMap categoryFacet = new TermsFacetMap("cat").setLimit(5);
- final JsonQueryRequest request =
- new JsonQueryRequest().setQuery("*:*").withFacet("categories", categoryFacet);
- QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-terms-facet-2[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(32, queryResponse.getResults().getNumFound());
- assertEquals(10, queryResponse.getResults().size());
- final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
- assertHasFacetWithBucketValues(
- topLevelFacetingData,
- "categories",
- new FacetBucket("electronics", 12),
- new FacetBucket("currency", 4),
- new FacetBucket("memory", 3),
- new FacetBucket("connector", 2),
- new FacetBucket("graphics card", 2));
- }
-
- @Test
- public void testStatFacet1() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-metrics-facet-1[]
- final JsonQueryRequest request =
- new JsonQueryRequest()
- .setQuery("memory")
- .withFilter("inStock:true")
- .withStatFacet("avg_price", "avg(price)")
- .withStatFacet("min_manufacturedate_dt", "min(manufacturedate_dt)")
- .withStatFacet("num_suppliers", "unique(manu_exact)")
- .withStatFacet("median_weight", "percentile(weight,50)");
- QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-metrics-facet-1[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(4, queryResponse.getResults().getNumFound());
- assertEquals(4, queryResponse.getResults().size());
- final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
- assertEquals(146.66, (double) topLevelFacetingData.getStatValue("avg_price"), 0.5);
- assertEquals(3L, topLevelFacetingData.getStatValue("num_suppliers"));
- assertEquals(352.0, (double) topLevelFacetingData.getStatValue("median_weight"), 0.5);
-
- Object val = topLevelFacetingData.getStatValue("min_manufacturedate_dt");
- assertTrue(val instanceof Date);
- assertEquals("2006-02-13T15:26:37Z", ((Date) val).toInstant().toString());
- }
-
- @Test
- public void testStatFacetSimple() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-metrics-facet-simple[]
- final JsonQueryRequest request =
- new JsonQueryRequest()
- .setQuery("*:*")
- .withFilter("price:[1.0 TO *]")
- .withFilter("popularity:[0 TO 10]")
- .withStatFacet("min_manu_id_s", "min(manu_id_s)")
- .withStatFacet("avg_value", "avg(div(popularity,price))");
- QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-metrics-facet-simple[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(13, queryResponse.getResults().getNumFound());
- assertEquals(10, queryResponse.getResults().size());
- final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
- assertEquals(0.036, (double) topLevelFacetingData.getStatValue("avg_value"), 0.1);
- Object val = topLevelFacetingData.getStatValue("min_manu_id_s");
- assertTrue(val instanceof String);
- assertEquals("apple", val.toString());
- }
-
- @Test
- public void testStatFacetExpanded() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-metrics-facet-expanded[]
- final Map<String, Object> expandedStatFacet = new HashMap<>();
- expandedStatFacet.put("type", "func");
- expandedStatFacet.put("func", "avg(div($numer,$denom))");
- expandedStatFacet.put("numer", "mul(popularity,3.0)");
- expandedStatFacet.put("denom", "price");
- final JsonQueryRequest request =
- new JsonQueryRequest()
- .setQuery("*:*")
- .withFilter("price:[1.0 TO *]")
- .withFilter("popularity:[0 TO 10]")
- .withFacet("avg_value", expandedStatFacet);
- QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-metrics-facet-expanded[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(13, queryResponse.getResults().getNumFound());
- assertEquals(10, queryResponse.getResults().size());
- final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
- assertEquals(0.108, (double) topLevelFacetingData.getStatValue("avg_value"), 0.1);
- }
-
- @Test
- public void testQueryFacetSimple() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-query-facet-simple[]
- QueryFacetMap queryFacet = new QueryFacetMap("popularity:[8 TO 10]");
- final JsonQueryRequest request =
- new JsonQueryRequest().setQuery("*:*").withFacet("high_popularity", queryFacet);
- QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-query-facet-simple[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(32, queryResponse.getResults().getNumFound());
- assertEquals(10, queryResponse.getResults().size());
- final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
- assertEquals(2, topLevelFacetingData.getQueryFacet("high_popularity").getCount());
- }
-
- @Test
- public void testQueryFacetExpanded() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-query-facet-expanded[]
- QueryFacetMap queryFacet =
- new QueryFacetMap("popularity:[8 TO 10]").withStatSubFacet("average_price", "avg(price)");
- final JsonQueryRequest request =
- new JsonQueryRequest().setQuery("*:*").withFacet("high_popularity", queryFacet);
- QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-query-facet-expanded[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(32, queryResponse.getResults().getNumFound());
- assertEquals(10, queryResponse.getResults().size());
- final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
- assertEquals(2, topLevelFacetingData.getQueryFacet("high_popularity").getCount());
- assertEquals(
- 199.5, topLevelFacetingData.getQueryFacet("high_popularity").getStatValue("average_price"));
- }
-
- @Test
- public void testRangeFacetSimple() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-range-facet-simple[]
- RangeFacetMap rangeFacet = new RangeFacetMap("price", 0.0, 100.0, 20.0);
- final JsonQueryRequest request =
- new JsonQueryRequest().setQuery("*:*").withFacet("prices", rangeFacet);
- QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-range-facet-simple[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(32, queryResponse.getResults().getNumFound());
- assertEquals(10, queryResponse.getResults().size());
- final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
- assertHasFacetWithBucketValues(
- topLevelFacetingData,
- "prices",
- new FacetBucket(0.0f, 5),
- new FacetBucket(20.0f, 0),
- new FacetBucket(40.0f, 0),
- new FacetBucket(60.0f, 1),
- new FacetBucket(80.0f, 1));
- }
-
- @Test
- public void testNestedFacetSimple() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-nested-cat-facet[]
- final TermsFacetMap topCategoriesFacet = new TermsFacetMap("cat").setLimit(3);
- final TermsFacetMap topManufacturerFacet = new TermsFacetMap("manu_id_s").setLimit(1);
- topCategoriesFacet.withSubFacet("top_manufacturers", topManufacturerFacet);
- final JsonQueryRequest request =
- new JsonQueryRequest().setQuery("*:*").withFacet("categories", topCategoriesFacet);
- QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-nested-cat-facet[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(32, queryResponse.getResults().getNumFound());
- assertEquals(10, queryResponse.getResults().size());
- final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
-
- assertHasFacetWithBucketValues(
- topLevelFacetingData,
- "categories",
- new FacetBucket("electronics", 12),
- new FacetBucket("currency", 4),
- new FacetBucket("memory", 3));
-
- // Check the top manufacturer for each category
- List<BucketJsonFacet> catBuckets =
- topLevelFacetingData.getBucketBasedFacets("categories").getBuckets();
- assertHasFacetWithBucketValues(
- catBuckets.get(0), "top_manufacturers", new FacetBucket("corsair", 3));
- assertHasFacetWithBucketValues(
- catBuckets.get(1), "top_manufacturers", new FacetBucket("boa", 1));
- assertHasFacetWithBucketValues(
- catBuckets.get(2), "top_manufacturers", new FacetBucket("corsair", 3));
- }
-
- @Test
- public void testFacetSortedByNestedMetric() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-nested-cat-facet-sorted[]
- final TermsFacetMap topCategoriesFacet =
- new TermsFacetMap("cat")
- .setLimit(3)
- .withStatSubFacet("avg_price", "avg(price)")
- .setSort("avg_price desc");
- final JsonQueryRequest request =
- new JsonQueryRequest().setQuery("*:*").withFacet("categories", topCategoriesFacet);
- QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-nested-cat-facet-sorted[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(32, queryResponse.getResults().getNumFound());
- assertEquals(10, queryResponse.getResults().size());
- final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
- assertHasFacetWithBucketValues(
- topLevelFacetingData,
- "categories",
- new FacetBucket("electronics and computer1", 1),
- new FacetBucket("graphics card", 2),
- new FacetBucket("music", 1));
- }
-
- @Test
- public void testFacetFilteredDomain() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-facet-filtered-domain[]
- final TermsFacetMap categoryFacet =
- new TermsFacetMap("cat")
- .setLimit(3)
- .withDomain(new DomainMap().withFilter("popularity:[5 TO 10]"));
- final JsonQueryRequest request =
- new JsonQueryRequest().setQuery("*:*").withFacet("categories", categoryFacet);
- QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-facet-filtered-domain[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(32, queryResponse.getResults().getNumFound());
- assertEquals(10, queryResponse.getResults().size());
- final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
- assertHasFacetWithBucketValues(
- topLevelFacetingData,
- "categories",
- new FacetBucket("electronics", 9),
- new FacetBucket("graphics card", 2),
- new FacetBucket("hard drive", 2));
- }
-
- @Test
- public void testFacetWidenedExcludeTagsDomain() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-facet-excludetags-domain[]
- final TermsFacetMap inStockFacet = new TermsFacetMap("inStock").setLimit(2);
- final TermsFacetMap allManufacturersFacet =
- new TermsFacetMap("manu_id_s")
- .setLimit(2)
- .withDomain(new DomainMap().withTagsToExclude("MANU"));
- final JsonQueryRequest request =
- new JsonQueryRequest()
- .setQuery("cat:electronics")
- .withFilter("{!tag=MANU}manu_id_s:apple")
- .withFacet("stock", inStockFacet)
- .withFacet("manufacturers", allManufacturersFacet);
- QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-facet-excludetags-domain[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(1, queryResponse.getResults().getNumFound());
- assertEquals(1, queryResponse.getResults().size());
- final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
- assertHasFacetWithBucketValues(topLevelFacetingData, "stock", new FacetBucket(true, 1));
- assertHasFacetWithBucketValues(
- topLevelFacetingData,
- "manufacturers",
- new FacetBucket("corsair", 3),
- new FacetBucket("belkin", 2));
- }
-
- @Test
- public void testFacetWidenedUsingQueryDomain() throws Exception {
- SolrClient solrClient = cluster.getSolrClient();
-
- // tag::solrj-json-facet-query-domain[]
- final TermsFacetMap inStockFacet = new TermsFacetMap("inStock").setLimit(2);
- final TermsFacetMap popularCategoriesFacet =
- new TermsFacetMap("cat")
- .withDomain(new DomainMap().withQuery("popularity:[8 TO 10]"))
- .setLimit(3);
- final JsonQueryRequest request =
- new JsonQueryRequest()
- .setQuery("apple")
- .withFacet("popular_categories", popularCategoriesFacet);
- QueryResponse queryResponse = request.process(solrClient, COLLECTION_NAME);
- // end::solrj-json-facet-query-domain[]
-
- assertEquals(0, queryResponse.getStatus());
- assertEquals(1, queryResponse.getResults().getNumFound());
- assertEquals(1, queryResponse.getResults().size());
- final NestableJsonFacet topLevelFacetingData = queryResponse.getJsonFacetingResponse();
- assertHasFacetWithBucketValues(
- topLevelFacetingData,
- "popular_categories",
- new FacetBucket("electronics", 1),
- new FacetBucket("music", 1),
- new FacetBucket("search", 1));
- }
-
- private class FacetBucket {
- private final Object val;
- private final int count;
-
- FacetBucket(Object val, int count) {
- this.val = val;
- this.count = count;
- }
-
- public Object getVal() {
- return val;
- }
-
- public int getCount() {
- return count;
- }
- }
-
- private void assertHasFacetWithBucketValues(
- NestableJsonFacet response, String expectedFacetName, FacetBucket... expectedBuckets) {
- assertTrue(
- "Expected response to have facet with name " + expectedFacetName,
- response.getBucketBasedFacets(expectedFacetName) != null);
- final List<BucketJsonFacet> buckets =
- response.getBucketBasedFacets(expectedFacetName).getBuckets();
- assertEquals(expectedBuckets.length, buckets.size());
- for (int i = 0; i < expectedBuckets.length; i++) {
- final FacetBucket expectedBucket = expectedBuckets[i];
- final BucketJsonFacet actualBucket = buckets.get(i);
- assertEquals(expectedBucket.getVal(), actualBucket.getVal());
- assertEquals(expectedBucket.getCount(), actualBucket.getCount());
- }
- }
-
- private void assertResponseFoundNumDocs(QueryResponse response, int expectedNumDocs) {
- assertEquals(0, response.getStatus());
- assertEquals(expectedNumDocs, response.getResults().size());
- }
-}
diff --git a/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/UsingSolrJRefGuideExamplesTest.java b/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/UsingSolrJRefGuideExamplesTest.java
deleted file mode 100644
index fee64cef9df..00000000000
--- a/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/UsingSolrJRefGuideExamplesTest.java
+++ /dev/null
@@ -1,325 +0,0 @@
-/*
- * 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 java.io.File;
-import java.util.ArrayDeque;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Optional;
-import java.util.Queue;
-import java.util.UUID;
-import org.apache.solr.client.solrj.SolrClient;
-import org.apache.solr.client.solrj.SolrQuery;
-import org.apache.solr.client.solrj.SolrQuery.ORDER;
-import org.apache.solr.client.solrj.SolrRequest;
-import org.apache.solr.client.solrj.beans.Field;
-import org.apache.solr.client.solrj.impl.CloudSolrClient;
-import org.apache.solr.client.solrj.impl.HttpSolrClient;
-import org.apache.solr.client.solrj.request.CollectionAdminRequest;
-import org.apache.solr.client.solrj.response.CollectionAdminResponse;
-import org.apache.solr.client.solrj.response.QueryResponse;
-import org.apache.solr.client.solrj.response.UpdateResponse;
-import org.apache.solr.cloud.SolrCloudTestCase;
-import org.apache.solr.common.SolrDocument;
-import org.apache.solr.common.SolrDocumentList;
-import org.apache.solr.common.SolrInputDocument;
-import org.apache.solr.common.params.MapSolrParams;
-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.
- *
- * <p>Snippets surrounded by "tag" and "end" comments are extracted and used in the Solr Reference
- * Guide.
- */
-public class UsingSolrJRefGuideExamplesTest extends SolrCloudTestCase {
-
- private static final int NUM_INDEXED_DOCUMENTS = 3;
- private static final int NUM_LIVE_NODES = 1;
-
- private Queue<String> expectedLines = new ArrayDeque<>();
-
- @BeforeClass
- public static void setUpCluster() throws Exception {
- configureCluster(NUM_LIVE_NODES)
- .addConfig("conf", new File(ExternalPaths.TECHPRODUCTS_CONFIGSET).toPath())
- .configure();
-
- CollectionAdminResponse response =
- CollectionAdminRequest.createCollection("techproducts", "conf", 1, 1)
- .setPerReplicaState(SolrCloudTestCase.USE_PER_REPLICA_STATE)
- .process(cluster.getSolrClient());
- cluster.waitForActiveCollection("techproducts", 1, 1);
- }
-
- @Before
- @Override
- public void setUp() throws Exception {
- super.setUp();
- expectedLines.clear();
- final SolrClient client = getSolrClient();
-
- final List<TechProduct> products = new ArrayList<TechProduct>();
- products.add(new TechProduct("1", "Fitbit Alta"));
- products.add(new TechProduct("2", "Sony Walkman"));
- products.add(new TechProduct("3", "Garmin GPS"));
-
- client.addBeans("techproducts", products);
- client.commit("techproducts");
- }
-
- @After
- @Override
- public void tearDown() throws Exception {
- super.tearDown();
- ensureNoLeftoverOutputExpectations();
-
- final SolrClient client = getSolrClient();
- client.deleteByQuery("techproducts", "*:*");
- client.commit("techproducts");
- }
-
- @Test
- public void queryWithRawSolrParamsExample() throws Exception {
- expectLine("Found 3 documents");
- expectLine("id: 1; name: Fitbit Alta");
- expectLine("id: 2; name: Sony Walkman");
- expectLine("id: 3; name: Garmin GPS");
-
- // tag::solrj-query-with-raw-solrparams[]
- final SolrClient client = getSolrClient();
-
- final Map<String, String> queryParamMap = new HashMap<String, String>();
- queryParamMap.put("q", "*:*");
- queryParamMap.put("fl", "id, name");
- queryParamMap.put("sort", "id asc");
- MapSolrParams queryParams = new MapSolrParams(queryParamMap);
-
- final QueryResponse response = client.query("techproducts", queryParams);
- final SolrDocumentList documents = response.getResults();
-
- print("Found " + documents.getNumFound() + " documents");
- for (SolrDocument document : documents) {
- final String id = (String) document.getFirstValue("id");
- final String name = (String) document.getFirstValue("name");
-
- print("id: " + id + "; name: " + name);
- }
- // end::solrj-query-with-raw-solrparams[]
- }
-
- @Test
- public void queryWithSolrQueryExample() throws Exception {
- final int numResultsToReturn = 3;
- expectLine("Found 3 documents");
- expectLine("id: 1; name: Fitbit Alta");
- expectLine("id: 2; name: Sony Walkman");
- expectLine("id: 3; name: Garmin GPS");
- final SolrClient client = getSolrClient();
-
- // tag::solrj-query-with-solrquery[]
- final SolrQuery query = new SolrQuery("*:*");
- query.addField("id");
- query.addField("name");
- query.setSort("id", ORDER.asc);
- query.setRows(numResultsToReturn);
- // end::solrj-query-with-solrquery[]
-
- final QueryResponse response = client.query("techproducts", query);
- final SolrDocumentList documents = response.getResults();
-
- print("Found " + documents.getNumFound() + " documents");
- assertEquals(numResultsToReturn, documents.size());
- for (SolrDocument document : documents) {
- final String id = (String) document.getFirstValue("id");
- final String name = (String) document.getFirstValue("name");
-
- print("id: " + id + "; name: " + name);
- }
- }
-
- @Test
- public void indexWithSolrInputDocumentExample() throws Exception {
- // tag::solrj-index-with-raw-solrinputdoc[]
- final SolrClient client = getSolrClient();
-
- final SolrInputDocument doc = new SolrInputDocument();
- doc.addField("id", UUID.randomUUID().toString());
- doc.addField("name", "Amazon Kindle Paperwhite");
-
- final UpdateResponse updateResponse = client.add("techproducts", doc);
- // Indexed documents must be committed
- client.commit("techproducts");
- // end::solrj-index-with-raw-solrinputdoc[]
-
- assertNumDocuments(NUM_INDEXED_DOCUMENTS + 1);
- }
-
- @Test
- public void indexBeanValueTypeExample() throws Exception {
- // tag::solrj-index-bean-value-type[]
- final SolrClient client = getSolrClient();
-
- final TechProduct kindle = new TechProduct("kindle-id-4", "Amazon Kindle Paperwhite");
- final UpdateResponse response = client.addBean("techproducts", kindle);
-
- client.commit("techproducts");
- // end::solrj-index-bean-value-type[]
-
- assertNumDocuments(NUM_INDEXED_DOCUMENTS + 1);
- }
-
- @Test
- public void queryBeanValueTypeExample() throws Exception {
- expectLine("Found 3 documents");
- expectLine("id: 1; name: Fitbit Alta");
- expectLine("id: 2; name: Sony Walkman");
- expectLine("id: 3; name: Garmin GPS");
-
- // tag::solrj-query-bean-value-type[]
- final SolrClient client = getSolrClient();
-
- final SolrQuery query = new SolrQuery("*:*");
- query.addField("id");
- query.addField("name");
- query.setSort("id", ORDER.asc);
-
- final QueryResponse response = client.query("techproducts", query);
- final List<TechProduct> products = response.getBeans(TechProduct.class);
- // end::solrj-query-bean-value-type[]
-
- print("Found " + products.size() + " documents");
- for (TechProduct product : products) {
- print("id: " + product.id + "; name: " + product.name);
- }
- }
-
- @Test
- public void otherSolrApisExample() throws Exception {
- expectLine("Found " + NUM_LIVE_NODES + " live nodes");
- // tag::solrj-other-apis[]
- final SolrClient client = getSolrClient();
-
- @SuppressWarnings({"rawtypes"})
- final SolrRequest request = new CollectionAdminRequest.ClusterStatus();
-
- final NamedList<Object> response = client.request(request);
- @SuppressWarnings({"unchecked"})
- final NamedList<Object> cluster = (NamedList<Object>) response.get("cluster");
- @SuppressWarnings({"unchecked"})
- final List<String> liveNodes = (List<String>) cluster.get("live_nodes");
-
- print("Found " + liveNodes.size() + " live nodes");
- // end::solrj-other-apis[]
- }
-
- private SolrClient getSolrClient() {
- return cluster.getSolrClient();
- }
-
- private SolrClient getTechProductSolrClient() {
- // tag::solrj-solrclient-timeouts[]
- final String solrUrl = "http://localhost:8983/solr";
- return new HttpSolrClient.Builder(solrUrl)
- .withConnectionTimeout(10000)
- .withSocketTimeout(60000)
- .build();
- // end::solrj-solrclient-timeouts[]
- }
-
- private SolrClient getBaseURLCloudSolrClient() {
- // tag::solrj-cloudsolrclient-baseurl[]
- final List<String> solrUrls = new ArrayList<>();
- solrUrls.add("http://solr1:8983/solr");
- solrUrls.add("http://solr2:8983/solr");
- return new CloudSolrClient.Builder(solrUrls).build();
- // end::solrj-cloudsolrclient-baseurl[]
- }
-
- private SolrClient getZookeeperNoRootCloudSolrClient() {
- // tag::solrj-cloudsolrclient-zookeepernoroot[]
- final List<String> zkServers = new ArrayList<>();
- zkServers.add("zookeeper1:2181");
- zkServers.add("zookeeper2:2181");
- zkServers.add("zookeeper3:2181");
- return new CloudSolrClient.Builder(zkServers, Optional.empty()).build();
- // end::solrj-cloudsolrclient-zookeepernoroot[]
- }
-
- private SolrClient getZookeeperRootCloudSolrClient() {
- // tag::solrj-cloudsolrclient-zookeeperroot[]
- final List<String> zkServers = new ArrayList<>();
- zkServers.add("zookeeper1:2181");
- zkServers.add("zookeeper2:2181");
- zkServers.add("zookeeper3:2181");
- return new CloudSolrClient.Builder(zkServers, Optional.of("/solr")).build();
- // end::solrj-cloudsolrclient-zookeeperroot[]
- }
-
- private void assertNumDocuments(int expectedNumResults) throws Exception {
- final QueryResponse queryResponse = getSolrClient().query("techproducts", new SolrQuery("*:*"));
- assertEquals(expectedNumResults, queryResponse.getResults().getNumFound());
- }
-
- // tag::solrj-techproduct-value-type[]
- public static class TechProduct {
- @Field public String id;
- @Field public String name;
-
- public TechProduct(String id, String name) {
- this.id = id;
- this.name = name;
- }
-
- public TechProduct() {}
- }
- // end::solrj-techproduct-value-type[]
-
- private void expectLine(String expectedLine) {
- expectedLines.add(expectedLine);
- }
-
- private 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));
- }
-
- private 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" + expectedLine);
- }
- fail(builder.toString());
- }
-}
diff --git a/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/ZkConfigFilesTest.java b/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/ZkConfigFilesTest.java
deleted file mode 100644
index f3d9c41fb1e..00000000000
--- a/solr/solrj/src/test/org/apache/solr/client/ref_guide_examples/ZkConfigFilesTest.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * 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 java.io.File;
-import java.nio.file.Paths;
-import java.util.List;
-import org.apache.solr.cloud.SolrCloudTestCase;
-import org.apache.solr.core.ConfigSetService;
-import org.apache.solr.util.ExternalPaths;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-/**
- * Examples showing how to manipulate configsets in ZK.
- *
- * <p>Snippets surrounded by "tag" and "end" comments are extracted and used in the Solr Reference
- * Guide.
- */
-public class ZkConfigFilesTest extends SolrCloudTestCase {
-
- @BeforeClass
- public static void setUpCluster() throws Exception {
- configureCluster(1).configure();
- }
-
- private static ConfigSetService getConfigSetService() {
- return cluster.getOpenOverseer().getCoreContainer().getConfigSetService();
- }
-
- @Before
- public void clearConfigsBefore() throws Exception {
- clearConfigs();
- }
-
- @After
- public void clearConfigsAfter() throws Exception {
- clearConfigs();
- }
-
- private void clearConfigs() throws Exception {
- List<String> configs = getConfigSetService().listConfigs();
- for (String config : configs) {
- getConfigSetService().deleteConfig(config);
- }
- }
-
- @Test
- public void testCanUploadConfigToZk() throws Exception {
- final String localConfigSetDirectory =
- new File(ExternalPaths.TECHPRODUCTS_CONFIGSET).getAbsolutePath();
-
- assertConfigsContainOnly();
-
- // tag::zk-configset-upload[]
- getConfigSetService().uploadConfig("nameForConfigset", Paths.get(localConfigSetDirectory));
- // end::zk-configset-upload[]
-
- assertConfigsContainOnly("nameForConfigset");
- }
-
- private void assertConfigsContainOnly(String... expectedConfigs) throws Exception {
- final int expectedSize = expectedConfigs.length;
-
- List<String> actualConfigs = getConfigSetService().listConfigs();
-
- assertEquals(expectedSize, actualConfigs.size());
- for (String expectedConfig : expectedConfigs) {
- assertTrue(
- "Expected ZK to contain " + expectedConfig + ", but it didn't. Actual configs: ",
- actualConfigs.contains(expectedConfig));
- }
- }
-}