You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@solr.apache.org by ep...@apache.org on 2024/02/24 16:23:54 UTC

(solr) 01/02: SOLR-17163: Only load the films.json file when running the bin/solr start -e films (#2295)

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

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

commit 39970ce246cd3990a7ed32a6d0ec2e252714df68
Author: Eric Pugh <ep...@opensourceconnections.com>
AuthorDate: Sat Feb 24 11:17:50 2024 -0500

    SOLR-17163: Only load the films.json file when running the bin/solr start -e films (#2295)
    
    * Only load the specific films.json file that we want for the example.
    
    * Only mention recursive processing of directories if we have a directory.
---
 .../src/java/org/apache/solr/cli/PostTool.java     | 22 +++++++-
 .../java/org/apache/solr/cli/RunExampleTool.java   |  4 +-
 .../src/test/org/apache/solr/cli/PostToolTest.java | 62 ++++++++++++++++++++--
 3 files changed, 81 insertions(+), 7 deletions(-)

diff --git a/solr/core/src/java/org/apache/solr/cli/PostTool.java b/solr/core/src/java/org/apache/solr/cli/PostTool.java
index b75e839aa7f..c03bb4336ce 100644
--- a/solr/core/src/java/org/apache/solr/cli/PostTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/PostTool.java
@@ -334,7 +334,9 @@ public class PostTool extends ToolBase {
       info("Entering auto mode. File endings considered are " + fileTypes);
     }
     if (recursive > 0) {
-      info("Entering recursive mode, max depth=" + recursive + ", delay=" + delay + "s");
+      if (recursionPossible(args)) {
+        info("Entering recursive mode, max depth=" + recursive + ", delay=" + delay + "s");
+      }
     }
     fileFilter = getFileFilterFromFileTypes(fileTypes);
     int numFilesPosted = postFiles(args, 0, out, type);
@@ -413,6 +415,24 @@ public class PostTool extends ToolBase {
     return Files.exists(srcFile.toPath());
   }
 
+  /**
+   * Check all the arguments looking to see if any are directories, and if so then we can recurse
+   * into them.
+   *
+   * @param args array of file names
+   * @return if we have a directory to recurse into
+   */
+  boolean recursionPossible(String[] args) {
+    boolean recursionPossible = false;
+    for (String arg : args) {
+      File f = new File(arg);
+      if (f.isDirectory()) {
+        recursionPossible = true;
+      }
+    }
+    return recursionPossible;
+  }
+
   /**
    * Post all filenames provided in args
    *
diff --git a/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java b/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
index dd3588ce706..ecf9a913f58 100644
--- a/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
+++ b/solr/core/src/java/org/apache/solr/cli/RunExampleTool.java
@@ -413,9 +413,7 @@ public class RunExampleTool extends ToolBase {
               updateUrl,
               "-type",
               "application/json",
-              "-filetypes",
-              "json",
-              exampleDir.toString()
+              filmsJsonFile.getAbsolutePath()
             };
         PostTool postTool = new PostTool();
         CommandLine postToolCli =
diff --git a/solr/core/src/test/org/apache/solr/cli/PostToolTest.java b/solr/core/src/test/org/apache/solr/cli/PostToolTest.java
index 94f108ea5ac..c6a16e1e83f 100644
--- a/solr/core/src/test/org/apache/solr/cli/PostToolTest.java
+++ b/solr/core/src/test/org/apache/solr/cli/PostToolTest.java
@@ -39,6 +39,8 @@ import java.util.Set;
 import org.apache.commons.cli.CommandLine;
 import org.apache.solr.SolrTestCaseJ4;
 import org.apache.solr.client.solrj.request.CollectionAdminRequest;
+import org.apache.solr.client.solrj.request.QueryRequest;
+import org.apache.solr.client.solrj.response.QueryResponse;
 import org.apache.solr.cloud.SolrCloudTestCase;
 import org.apache.solr.common.util.EnvUtils;
 import org.apache.solr.common.util.Utils;
@@ -68,7 +70,8 @@ public class PostToolTest extends SolrCloudTestCase {
     File jsonDoc = File.createTempFile("temp", ".json");
 
     FileWriter fw = new FileWriter(jsonDoc, StandardCharsets.UTF_8);
-    Utils.writeJson(Utils.toJSONString(Map.of("id", "1", "title", "mytitle")), fw, true);
+    Utils.writeJson(Map.of("id", "1", "title_s", "mytitle"), fw, true);
+    fw.flush();
 
     String[] args = {
       "post",
@@ -77,6 +80,21 @@ public class PostToolTest extends SolrCloudTestCase {
       jsonDoc.getAbsolutePath()
     };
     assertEquals(0, runTool(args));
+
+    int numFound = 0;
+    int expectedDocCount = 1;
+
+    for (int idx = 0; idx < 100; ++idx) {
+      QueryRequest req = withBasicAuth(new QueryRequest(params("q", "*:*")));
+      QueryResponse rsp = req.process(cluster.getSolrClient(), collection);
+
+      numFound = (int) rsp.getResults().getNumFound();
+      if (numFound == expectedDocCount) {
+        break;
+      }
+      Thread.sleep(100);
+    }
+    assertEquals("*:* found unexpected number of documents", expectedDocCount, numFound);
   }
 
   @Test
@@ -89,13 +107,29 @@ public class PostToolTest extends SolrCloudTestCase {
     CollectionAdminRequest.createCollection(collection, "conf1", 1, 1, 0, 0)
         .processAndWait(cluster.getSolrClient(), 10);
 
-    File jsonDoc = File.createTempFile("temp", "json");
+    File jsonDoc = File.createTempFile("temp", ".json");
 
     FileWriter fw = new FileWriter(jsonDoc, StandardCharsets.UTF_8);
-    Utils.writeJson(Utils.toJSONString(Map.of("id", "1", "title", "mytitle")), fw, true);
+    Utils.writeJson(Map.of("id", "1", "title_s", "mytitle"), fw, true);
+    fw.flush();
 
     String[] args = {"post", "-c", collection, jsonDoc.getAbsolutePath()};
     assertEquals(0, runTool(args));
+
+    int numFound = 0;
+    int expectedDocCount = 1;
+
+    for (int idx = 0; idx < 100; ++idx) {
+      QueryRequest req = withBasicAuth(new QueryRequest(params("q", "*:*")));
+      QueryResponse rsp = req.process(cluster.getSolrClient(), collection);
+
+      numFound = (int) rsp.getResults().getNumFound();
+      if (numFound == expectedDocCount) {
+        break;
+      }
+      Thread.sleep(100);
+    }
+    assertEquals("*:* found unexpected number of documents", expectedDocCount, numFound);
   }
 
   private int runTool(String[] args) throws Exception {
@@ -188,6 +222,28 @@ public class PostToolTest extends SolrCloudTestCase {
     assertEquals(2, num);
   }
 
+  @Test
+  public void testDetectingIfRecursionPossibleInFilesMode() throws IOException {
+    PostTool postTool = new PostTool();
+    postTool.recursive = 1; // This is the default
+    File dir = getFile("exampledocs");
+    File doc = File.createTempFile("temp", ".json");
+    assertTrue(postTool.recursionPossible(new String[] {dir.toString()}));
+    assertFalse(postTool.recursionPossible(new String[] {doc.toString()}));
+    assertTrue(postTool.recursionPossible(new String[] {doc.toString(), dir.toString()}));
+  }
+
+  @Test
+  public void testRecursionAppliesToFilesMode() throws MalformedURLException {
+    PostTool postTool = new PostTool();
+    postTool.recursive = 1; // This is the default
+    postTool.dryRun = true;
+    postTool.solrUpdateUrl = new URL("http://localhost:8983/solr/fake/update");
+    File dir = getFile("exampledocs");
+    int num = postTool.postFiles(new String[] {dir.toString()}, 0, null, null);
+    assertEquals(2, num);
+  }
+
   @Test
   public void testDoWebMode() throws IOException, URISyntaxException {
     PostTool postTool = new PostTool();