Skip to content

Commit 74c5602

Browse files
author
Jim Leether
committed
Added methods for multicore mode
Added methods to check for the existance of and dynamically create new solr cores in multicore mode. Updated included solr.xml to persist new cores.
1 parent 7fb6287 commit 74c5602

File tree

3 files changed

+133
-35
lines changed

3 files changed

+133
-35
lines changed

components/cfsolrlib.cfc

+55
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,61 @@
4949
<cfreturn this/>
5050
</cffunction>
5151

52+
<cffunction name="checkForCore" access="public" output="false" hint="Multicore method. Checks for existance of a Solr core by name">
53+
<cfargument name="coreName" type="string" required="true" hint="Solr core name" />
54+
55+
<cfscript>
56+
h = new http();
57+
h.setMethod("get");
58+
h.setURL("#THIS.solrURL#/#ARGUMENTS.coreName#/admin/ping");
59+
pingResponse = h.send().getPrefix().statusCode;
60+
coreCheckResponse = structNew();
61+
if (pingResponse eq "200 OK"){
62+
coreCheckResponse.success = true;
63+
coreCheckResponse.statusCode = pingResponse;
64+
return coreCheckResponse;
65+
}else{
66+
coreCheckResponse.success = false;
67+
coreCheckResponse.statusCode = pingResponse;
68+
return coreCheckResponse;
69+
}
70+
</cfscript>
71+
</cffunction>
72+
73+
<cffunction name="createNewCore" access="public" output="false" hint="Multicore method. Creates new Solr core" returntype="struct">
74+
<cfargument name="coreName" type="string" required="true" hint="New Solr core name" />
75+
<cfargument name="instanceDir" type="string" required="true" hint="Location of folder containing config and schema files" />
76+
<cfargument name="dataDir" type="string" required="false" hint="Location to store core's index data" />
77+
<cfargument name="configName" type="string" required="false" hint="Name of config file" />
78+
<cfargument name="schemaName" type="string" required="false" hint="Name of schema file" />
79+
80+
<cfscript>
81+
URLString = "#THIS.host#:#THIS.port#/solr/admin/cores?action=CREATE&name=#ARGUMENTS.coreName#&instanceDir=#instanceDir#";
82+
if (structKeyExists(ARGUMENTS, "dataDir")){
83+
URLString = "#URLString#&dataDir=#ARGUMENTS.dataDir#";
84+
}
85+
if (structKeyExists(ARGUMENTS, "configName")){
86+
URLString = "#URLString#&config=#ARGUMENTS.configName#";
87+
}
88+
if (structKeyExists(ARGUMENTS, "schemaName")){
89+
URLString = "#URLString#&schema=#ARGUMENTS.schemaName#";
90+
}
91+
newCoreRequest = new http();
92+
newCoreRequest.setMethod("get");
93+
newCoreRequest.setURL("#URLString#");
94+
response = newCoreRequest.send().getPrefix();
95+
coreCreationResponse = structNew();
96+
if (response.statusCode eq "200 OK"){
97+
coreCreationResponse.success = true;
98+
return coreCreationResponse;
99+
}else{
100+
coreCreationResponse.success = false;
101+
coreCreationResponse.message = response.ErrorDetail;
102+
return coreCreationResponse;
103+
}
104+
</cfscript>
105+
</cffunction>
106+
52107
<cffunction name="search" access="public" output="false" hint="Search for documents in the Solr index">
53108
<cfargument name="q" type="string" required="true" hint="Your query string" />
54109
<cfargument name="start" type="numeric" required="false" default="0" hint="Offset for results, starting with 0" />

coreCreationExample.cfm

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<cfset local.sampleSolrInstance = createObject("component","components.cfsolrlib").init(APPLICATION.javaloader,"localhost","8983","/solr") />
2+
3+
<cfif structKeyExists(form,"coreSubmit")>
4+
<cfset local.coreResponse = local.sampleSolrInstance.checkForCore("#form.coreName#")/>
5+
<cfif local.coreResponse.success eq false>
6+
<cfset local.newCoreResponse = local.sampleSolrInstance.createNewCore("#form.coreName#","core0")/>
7+
</cfif>
8+
</cfif>
9+
10+
<html>
11+
<head>
12+
<title>CFSolrLib 3.0 | Core creation example</title>
13+
</head>
14+
<body>
15+
<h2>Create Solr Core Example</h2>
16+
<p>This will check for the existance of a core and create the core if it does not exist.<br />
17+
Solr must have been started in multicore mode for this example to function correctly.<br />
18+
This is done by adding "-Dsolr.solr.home=multicore" to the start command.</p>
19+
<form action="" method="POST">
20+
New Core Name: <input name="coreName" type="text" /><br />
21+
<input type="submit" name="coreSubmit" value="Create Core" /><br />
22+
</form>
23+
<p>
24+
<cfoutput>
25+
<cfif structKeyExists(local,"coreResponse")>
26+
Core Exists: #local.coreResponse.success#<br />
27+
Status Code: #local.coreResponse.statusCode#<br />
28+
<cfif local.coreResponse.success eq true>
29+
Core Exists. No new core created.<br />
30+
</cfif>
31+
<br />
32+
</cfif>
33+
34+
<cfif structKeyExists(local,"newCoreResponse")>
35+
Core Creation Attempt Success: #local.newCoreResponse.success#<br />
36+
<cfif local.newCoreResponse.success eq false>
37+
Error Message: #local.newCoreResponse.message#<br />
38+
</cfif>
39+
<br />
40+
</cfif>
41+
</cfoutput>
42+
</body>
43+
</html>

solr-server/multicore/solr.xml

+35-35
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
1-
<?xml version="1.0" encoding="UTF-8" ?>
2-
<!--
3-
Licensed to the Apache Software Foundation (ASF) under one or more
4-
contributor license agreements. See the NOTICE file distributed with
5-
this work for additional information regarding copyright ownership.
6-
The ASF licenses this file to You under the Apache License, Version 2.0
7-
(the "License"); you may not use this file except in compliance with
8-
the License. You may obtain a copy of the License at
9-
10-
http://www.apache.org/licenses/LICENSE-2.0
11-
12-
Unless required by applicable law or agreed to in writing, software
13-
distributed under the License is distributed on an "AS IS" BASIS,
14-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15-
See the License for the specific language governing permissions and
16-
limitations under the License.
17-
-->
18-
19-
<!--
20-
All (relative) paths are relative to the installation path
21-
22-
persistent: Save changes made via the API to this file
23-
sharedLib: path to a lib directory that will be shared across all cores
24-
-->
25-
<solr persistent="false">
26-
27-
<!--
28-
adminPath: RequestHandler path to manage cores.
29-
If 'null' (or absent), cores will not be manageable via request handler
30-
-->
31-
<cores adminPath="/admin/cores" host="${host:}" hostPort="${jetty.port:}">
32-
<core name="core0" instanceDir="core0" />
33-
<core name="core1" instanceDir="core1" />
34-
</cores>
35-
</solr>
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
Licensed to the Apache Software Foundation (ASF) under one or more
4+
contributor license agreements. See the NOTICE file distributed with
5+
this work for additional information regarding copyright ownership.
6+
The ASF licenses this file to You under the Apache License, Version 2.0
7+
(the "License"); you may not use this file except in compliance with
8+
the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
<!--
20+
All (relative) paths are relative to the installation path
21+
22+
persistent: Save changes made via the API to this file
23+
sharedLib: path to a lib directory that will be shared across all cores
24+
-->
25+
<solr persistent="true">
26+
27+
<!--
28+
adminPath: RequestHandler path to manage cores.
29+
If 'null' (or absent), cores will not be manageable via request handler
30+
-->
31+
<cores adminPath="/admin/cores" host="${host:}" hostPort="${jetty.port:}">
32+
<core name="core0" instanceDir="core0" />
33+
<core name="core1" instanceDir="core1" />
34+
</cores>
35+
</solr>

0 commit comments

Comments
 (0)