in solr/core/src/java/org/apache/solr/cli/RunExampleTool.java [300:514]
protected void runExample(CommandLine cli, String exampleName) throws Exception {
String collectionName = "schemaless".equals(exampleName) ? "gettingstarted" : exampleName;
String configSet =
"techproducts".equals(exampleName) ? "sample_techproducts_configs" : "_default";
boolean isCloudMode = !cli.hasOption(USER_MANAGED_OPTION);
String zkHost = cli.getOptionValue(CommonCLIOptions.ZK_HOST_OPTION);
int port =
Integer.parseInt(
cli.getOptionValue(PORT_OPTION, System.getenv().getOrDefault("SOLR_PORT", "8983")));
Map<String, Object> nodeStatus = startSolr(solrHomeDir, isCloudMode, cli, port, zkHost, 30);
String solrUrl = CLIUtils.normalizeSolrUrl((String) nodeStatus.get("baseUrl"), false);
// If the example already exists then let the user know they should delete it, or
// they may get unusual behaviors.
boolean alreadyExists = false;
boolean cloudMode = nodeStatus.get("cloud") != null;
if (cloudMode) {
if (CLIUtils.safeCheckCollectionExists(
solrUrl, collectionName, cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION))) {
alreadyExists = true;
echo(
"\nWARNING: Collection '"
+ collectionName
+ "' already exists, which may make starting this example not work well!");
}
} else {
String coreName = collectionName;
if (CLIUtils.safeCheckCoreExists(
solrUrl, coreName, cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION))) {
alreadyExists = true;
echo(
"\nWARNING: Core '"
+ coreName
+ "' already exists, which may make starting this example not work well!");
}
}
if (alreadyExists) {
echo(
"You may want to run 'bin/solr delete -c "
+ collectionName
+ " --delete-config' first before running the example to ensure a fresh state.");
}
if (!alreadyExists) {
// invoke the CreateTool
String[] createArgs =
new String[] {
"--name", collectionName,
"--shards", "1",
"--replication-factor", "1",
"--conf-name", collectionName,
"--conf-dir", configSet,
"--solr-url", solrUrl
};
CreateTool createTool = new CreateTool(runtime);
int createCode = createTool.runTool(SolrCLI.processCommandLineArgs(createTool, createArgs));
if (createCode != 0)
throw new Exception(
"Failed to create " + collectionName + " using command: " + Arrays.asList(createArgs));
}
if ("techproducts".equals(exampleName) && !alreadyExists) {
Path exampledocsDir = this.exampleDir.resolve("exampledocs");
if (!Files.isDirectory(exampledocsDir)) {
Path readOnlyExampleDir = serverDir.resolveSibling("example");
if (Files.isDirectory(readOnlyExampleDir)) {
exampledocsDir = readOnlyExampleDir.resolve("exampledocs");
}
}
if (Files.isDirectory(exampledocsDir)) {
echo("Indexing tech product example docs from " + exampledocsDir.toAbsolutePath());
String[] args =
new String[] {
"post",
"--solr-url",
solrUrl,
"--name",
collectionName,
"--type",
"application/xml",
"--filetypes",
"xml",
exampledocsDir.toAbsolutePath().toString()
};
PostTool postTool = new PostTool(runtime);
CommandLine postToolCli = SolrCLI.parseCmdLine(postTool, args);
postTool.runTool(postToolCli);
} else {
echo(
"exampledocs directory not found, skipping indexing step for the techproducts example");
}
} else if ("films".equals(exampleName) && !alreadyExists) {
try (SolrClient solrClient =
CLIUtils.getSolrClient(
solrUrl, cli.getOptionValue(CommonCLIOptions.CREDENTIALS_OPTION))) {
echo("Adding dense vector field type to films schema");
SolrCLI.postJsonToSolr(
solrClient,
"/" + collectionName + "/schema",
"{\n"
+ " \"add-field-type\" : {\n"
+ " \"name\":\"knn_vector_10\",\n"
+ " \"class\":\"solr.DenseVectorField\",\n"
+ " \"vectorDimension\":10,\n"
+ " \"similarityFunction\":cosine\n"
+ " \"knnAlgorithm\":hnsw\n"
+ " }\n"
+ " }");
echo(
"Adding name, genre, directed_by, initial_release_date, and film_vector fields to films schema");
SolrCLI.postJsonToSolr(
solrClient,
"/" + collectionName + "/schema",
"{\n"
+ " \"add-field\" : {\n"
+ " \"name\":\"name\",\n"
+ " \"type\":\"text_general\",\n"
+ " \"multiValued\":false,\n"
+ " \"stored\":true\n"
+ " },\n"
+ " \"add-field\" : {\n"
+ " \"name\":\"genre\",\n"
+ " \"type\":\"text_general\",\n"
+ " \"multiValued\":true,\n"
+ " \"stored\":true\n"
+ " },\n"
+ " \"add-field\" : {\n"
+ " \"name\":\"directed_by\",\n"
+ " \"type\":\"text_general\",\n"
+ " \"multiValued\":true,\n"
+ " \"stored\":true\n"
+ " },\n"
+ " \"add-field\" : {\n"
+ " \"name\":\"initial_release_date\",\n"
+ " \"type\":\"pdate\",\n"
+ " \"stored\":true\n"
+ " },\n"
+ " \"add-field\" : {\n"
+ " \"name\":\"film_vector\",\n"
+ " \"type\":\"knn_vector_10\",\n"
+ " \"indexed\":true\n"
+ " \"stored\":true\n"
+ " },\n"
+ " \"add-copy-field\" : {\n"
+ " \"source\":\"genre\",\n"
+ " \"dest\":\"_text_\"\n"
+ " },\n"
+ " \"add-copy-field\" : {\n"
+ " \"source\":\"name\",\n"
+ " \"dest\":\"_text_\"\n"
+ " },\n"
+ " \"add-copy-field\" : {\n"
+ " \"source\":\"directed_by\",\n"
+ " \"dest\":\"_text_\"\n"
+ " }\n"
+ " }");
echo(
"Adding paramsets \"algo\" and \"algo_b\" to films configuration for relevancy tuning");
SolrCLI.postJsonToSolr(
solrClient,
"/" + collectionName + "/config/params",
"{\n"
+ " \"set\": {\n"
+ " \"algo_a\":{\n"
+ " \"defType\":\"dismax\",\n"
+ " \"qf\":\"name\"\n"
+ " }\n"
+ " },\n"
+ " \"set\": {\n"
+ " \"algo_b\":{\n"
+ " \"defType\":\"dismax\",\n"
+ " \"qf\":\"name\",\n"
+ " \"mm\":\"100%\"\n"
+ " }\n"
+ " }\n"
+ " }\n");
Path filmsJsonFile = this.exampleDir.resolve("films").resolve("films.json");
echo("Indexing films example docs from " + filmsJsonFile.toAbsolutePath());
String[] args =
new String[] {
"post",
"--solr-url",
solrUrl,
"--name",
collectionName,
"--type",
"application/json",
filmsJsonFile.toAbsolutePath().toString()
};
PostTool postTool = new PostTool(runtime);
CommandLine postToolCli = SolrCLI.parseCmdLine(postTool, args);
postTool.runTool(postToolCli);
} catch (Exception ex) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, ex);
}
echo(
"\nSolr "
+ exampleName
+ " example launched successfully. Direct your Web browser to "
+ solrUrl
+ " to visit the Solr Admin UI");
}
}