Coldfusion Solr Client - SolColdfusion
As I hinted at yesterday, I was close to having some code in the pipeline to abstract using Solr. I've finished the initial code with the following built in. Here's a brief setup guide to start playing with the code.
First, you're going to need to grab the latest release version of Solr (currently 1.2). The only real requirement to run this software is that you have a JRE of 1.5 or higher. Untar/zip the file somewhere convenient and open a command prompt. Get to the example directory in the apache-solr.1.2.x folder (cd
This will start a new instance of the Solr server on your computer on port 8983. You can make sure this is running by navigating to http://localhost:8983/solr (NOTE: this is a link to your computer. If you get an error, it's because your computer isn't running an instance of Solr on port 8983).
At this point, it's probably good to send you over to the Solr website to take a look at their tutorial. Go ahead. I'll wait...
...
Great, you're back.
You've seen some basic inserting, deleting, and querying of Solr index data. You may have also noticed that there are clients for PHP, Ruby, Python, and Java...no ColdFusion. I want to do a little more testing on this before I submit the patch, but I've added the initial code as an encosure here to do updating, deleting, and searching in Coldfusion.
The CFC SolColdfusion should be in the path org/apache/client (at least that's where I'm putting in for the purposes of this initial demonstration). The initialization takes one required parameter (the Solr host) and then has two optional parameters (port and path).
To set this up, create an instance with
Now, there are a lot of different parameters you can send to Solr to perform different queries. And, since some of these key names can repeat, I chose to implement sending these parameters as an array. So, let's set this up.
<cfset params[1][1] = "indent">
<cfset params[1][2] = "on" />
<cfset params[2][1] = "wt">
<cfset params[2][2] = "standard" />
<cfset params[3][1] = "fl" />
<cfset params[3][2] = "*,score" />
<cfset params[4][1] = "qt" />
<cfset params[4][2] = "standard" />
<cfset params[5][1] = "wt" />
<cfset params[5][2] = "standard" />
These parameters are basically what are the defaults that Solr will return back to you. If you want highlighting, you would need to add two additional row vectors with 'hl = on' and 'hl.fl =
Searching is straight forward, taking a query, the start row, number of rows to return, and the array of parameters:
This searches all fields and all content and returns back an XML document with the search results in it.
In the result node, you'll see that Solr returns an xmlAttribute of
<cfxml variable="sample">
<doc>
<field name="id">F8V7067-APL-KIT</field>
<field name="name">Belkin Mobile Power Cord for iPod w/ Dock</field>
<field name="manu">Belkin</field>
<field name="cat">electronics</field>
<field name="cat">connector</field>
<field name="features">car power adapter, white</field>
<field name="weight">4</field>
<field name="price">19.95</field>
<field name="popularity">1</field>
<field name="inStock">false</field>
</doc>
</cfxml>
<!--- add this document to the index --->
<cfset solr.add(sample) />
<cfset solr.commit() />
<cfset solr.optimize() />
<!--- search for the newly added document --->
<cfset results = solr.search("id:F8V7067-APL-KIT", 0, 10, params) />
<cfdump var="#xmlParse(results)#" />
You'll notice I used a commit and optmize statement. Neither of these statements are necessary every time you add a document, but be aware that Solr caches documents and won't flush the new documents to disk unless you either commit the documents or the mergefactor setting you used in your solrconfig.xml file has been reached.
Now, let's delete this document...
<cfset solr.commit() />
Don't forget to commit deletions to the index!
There'll be more soon (add multiple documents, delete by queries). In the mean time, try it out. If you have any comments, questions, concerns, whatever, let me know.

Thanks for this series. Good stuff and very timely (for me).
Solr pretty much needs its own server (or at least a J2EE container like Jetty, Tomcat, JBoss, ...) to run. You can also have multiple JVMs on the same server (just don't point your JVM in CF to the 1.5/6 version of the JDK). For most of my test boxes, I have an Apache server running CF on port 80, and a Jetty server running on 8983 (I used 8983 in case you're running JRun and using port 8080).
You know what, this actually should probably be another post...I'll get more on this up a bit later today.
I tried it out on a tomcat, but the i can't search anything. Adding documents to the index and deleting them works fine but every time i try to search for something i get this cfm error.
" The argument PARAMS passed to function search() is not of type struct."
I think my setup works fine because i can access the solr admin and i can run a cfdump of my component.
Anybody has an idea what i'm missing here? Thanks in advance
i created an array of params end sent that to the search
Sven try this, this should do the trick:
<!--- init --->
<cfset solr1 = createObject("component", "org.apache.solr.client.SolColdfusion").init("http://localhost", "8983", "/solr") />
<!--- create a struct to hold search params and the query - last item in the struct --->
<cfset params= structNew()>
<cfset params.indent="on">
<cfset params.wt="standard">
<cfset params.fl="*,score">
<cfset params.qt="standard">
<cfset params.wt="standard">
<!--- this is the query attrbt --->
<cfset params.q="a">
<!--- call the search method --->
<cfset results = solr1.search(params) />
<!--- dump xml --->
<cfdump var="#results#" />
does SOLcoldfusion search binary files like PDFs?
I'd like to know if the coldfusion client can search binary files like PDFs?
Thanks!