Skip to content

Commit 710c66b

Browse files
authored
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.
1 parent 5c69a1e commit 710c66b

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed

solr/core/src/java/org/apache/solr/cli/PostTool.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ private void doFilesMode() {
338338
info("Entering auto mode. File endings considered are " + fileTypes);
339339
}
340340
if (recursive > 0) {
341-
info("Entering recursive mode, max depth=" + recursive + ", delay=" + delay + "s");
341+
if (recursionPossible(args)) {
342+
info("Entering recursive mode, max depth=" + recursive + ", delay=" + delay + "s");
343+
}
342344
}
343345
fileFilter = getFileFilterFromFileTypes(fileTypes);
344346
int numFilesPosted = postFiles(args, 0, out, type);
@@ -417,6 +419,24 @@ private boolean checkIsValidPath(File srcFile) {
417419
return Files.exists(srcFile.toPath());
418420
}
419421

422+
/**
423+
* Check all the arguments looking to see if any are directories, and if so then we can recurse
424+
* into them.
425+
*
426+
* @param args array of file names
427+
* @return if we have a directory to recurse into
428+
*/
429+
boolean recursionPossible(String[] args) {
430+
boolean recursionPossible = false;
431+
for (String arg : args) {
432+
File f = new File(arg);
433+
if (f.isDirectory()) {
434+
recursionPossible = true;
435+
}
436+
}
437+
return recursionPossible;
438+
}
439+
420440
/**
421441
* Post all filenames provided in args
422442
*

solr/core/src/java/org/apache/solr/cli/RunExampleTool.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,7 @@ protected void runExample(CommandLine cli, String exampleName) throws Exception
420420
updateUrl,
421421
"-type",
422422
"application/json",
423-
"-filetypes",
424-
"json",
425-
exampleDir.toString()
423+
filmsJsonFile.getAbsolutePath()
426424
};
427425
PostTool postTool = new PostTool();
428426
CommandLine postToolCli =

solr/core/src/test/org/apache/solr/cli/PostToolTest.java

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
import org.apache.solr.client.solrj.SolrRequest;
4545
import org.apache.solr.client.solrj.SolrResponse;
4646
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
47+
import org.apache.solr.client.solrj.request.QueryRequest;
48+
import org.apache.solr.client.solrj.response.QueryResponse;
4749
import org.apache.solr.cloud.SolrCloudTestCase;
4850
import org.apache.solr.common.util.EnvUtils;
4951
import org.apache.solr.common.util.Utils;
@@ -106,7 +108,8 @@ public void testBasicRun() throws Exception {
106108
File jsonDoc = File.createTempFile("temp", ".json");
107109

108110
FileWriter fw = new FileWriter(jsonDoc, StandardCharsets.UTF_8);
109-
Utils.writeJson(Utils.toJSONString(Map.of("id", "1", "title", "mytitle")), fw, true);
111+
Utils.writeJson(Map.of("id", "1", "title_s", "mytitle"), fw, true);
112+
fw.flush();
110113

111114
String[] args = {
112115
"post",
@@ -117,6 +120,21 @@ public void testBasicRun() throws Exception {
117120
jsonDoc.getAbsolutePath()
118121
};
119122
assertEquals(0, runTool(args));
123+
124+
int numFound = 0;
125+
int expectedDocCount = 1;
126+
127+
for (int idx = 0; idx < 100; ++idx) {
128+
QueryRequest req = withBasicAuth(new QueryRequest(params("q", "*:*")));
129+
QueryResponse rsp = req.process(cluster.getSolrClient(), collection);
130+
131+
numFound = (int) rsp.getResults().getNumFound();
132+
if (numFound == expectedDocCount) {
133+
break;
134+
}
135+
Thread.sleep(100);
136+
}
137+
assertEquals("*:* found unexpected number of documents", expectedDocCount, numFound);
120138
}
121139

122140
@Test
@@ -129,15 +147,31 @@ public void testRunWithCollectionParam() throws Exception {
129147
withBasicAuth(CollectionAdminRequest.createCollection(collection, "conf1", 1, 1, 0, 0))
130148
.processAndWait(cluster.getSolrClient(), 10);
131149

132-
File jsonDoc = File.createTempFile("temp", "json");
150+
File jsonDoc = File.createTempFile("temp", ".json");
133151

134152
FileWriter fw = new FileWriter(jsonDoc, StandardCharsets.UTF_8);
135-
Utils.writeJson(Utils.toJSONString(Map.of("id", "1", "title", "mytitle")), fw, true);
153+
Utils.writeJson(Map.of("id", "1", "title_s", "mytitle"), fw, true);
154+
fw.flush();
136155

137156
String[] args = {
138157
"post", "-c", collection, "-credentials", USER + ":" + PASS, jsonDoc.getAbsolutePath()
139158
};
140159
assertEquals(0, runTool(args));
160+
161+
int numFound = 0;
162+
int expectedDocCount = 1;
163+
164+
for (int idx = 0; idx < 100; ++idx) {
165+
QueryRequest req = withBasicAuth(new QueryRequest(params("q", "*:*")));
166+
QueryResponse rsp = req.process(cluster.getSolrClient(), collection);
167+
168+
numFound = (int) rsp.getResults().getNumFound();
169+
if (numFound == expectedDocCount) {
170+
break;
171+
}
172+
Thread.sleep(100);
173+
}
174+
assertEquals("*:* found unexpected number of documents", expectedDocCount, numFound);
141175
}
142176

143177
private int runTool(String[] args) throws Exception {
@@ -230,6 +264,28 @@ public void testDoFilesMode() throws MalformedURLException {
230264
assertEquals(2, num);
231265
}
232266

267+
@Test
268+
public void testDetectingIfRecursionPossibleInFilesMode() throws IOException {
269+
PostTool postTool = new PostTool();
270+
postTool.recursive = 1; // This is the default
271+
File dir = getFile("exampledocs");
272+
File doc = File.createTempFile("temp", ".json");
273+
assertTrue(postTool.recursionPossible(new String[] {dir.toString()}));
274+
assertFalse(postTool.recursionPossible(new String[] {doc.toString()}));
275+
assertTrue(postTool.recursionPossible(new String[] {doc.toString(), dir.toString()}));
276+
}
277+
278+
@Test
279+
public void testRecursionAppliesToFilesMode() throws MalformedURLException {
280+
PostTool postTool = new PostTool();
281+
postTool.recursive = 1; // This is the default
282+
postTool.dryRun = true;
283+
postTool.solrUpdateUrl = new URL("http://localhost:8983/solr/fake/update");
284+
File dir = getFile("exampledocs");
285+
int num = postTool.postFiles(new String[] {dir.toString()}, 0, null, null);
286+
assertEquals(2, num);
287+
}
288+
233289
@Test
234290
public void testDoWebMode() throws IOException, URISyntaxException {
235291
PostTool postTool = new PostTool();

0 commit comments

Comments
 (0)