Skip to content

Instantly share code, notes, and snippets.

@smebberson
Created March 28, 2012 02:06
Show Gist options
  • Save smebberson/2222861 to your computer and use it in GitHub Desktop.
Save smebberson/2222861 to your computer and use it in GitHub Desktop.
Solr document indexing in ColdFusion
.DS_Store
WEB-INF
collections/*
documents/*
.pdf
.doc
.docx
.txt
.xls
.xlsx

Solr document indexing, in ColdFusion

This is a quick example demonstrating how easy it is to index documents stored in a file system. The example should work fine in both Railo (3.2 and up) and ColdFusion (9 and up).

Railo uses Apache Lucene, and ColdFusion uses Solr (which in turn uses Apache Lucene) for document indexing.

To get started

Download the gist and get it running in your CFML engine of choice. Then grab yourself a bunch of documents, and put them in the documents folder. You can create a nested directory structure to store the documents if you wish.

* /collections-list.cfm will list all current collection
* /collections-setup.cfm will setup the collection and index document content (you can customise the values in Application.cfc beforehand if you like)
* /collections-delete.cfm will delete the current collection
* /index.cfm so that you can perform a search

Note:if you can't get a search to work, try searching on *

component {
this.name = "solr";
public boolean function onApplicationStart() {
initCollection();
return true;
}
public boolean function onRequestStart(string page) {
if (structKeyExists(url, 'reinit')) this.initCollection();
return true;
}
private struct function initCollection () {
return application.collection = {
name: 'documents',
content: '#expandPath("./")#documents',
path: '#expandPath("./")#collections/documents',
url: '/documents',
engine: 'solr'
};
}
}
<cfsetting enablecfoutputonly="true" />
<!--- delete a collection and then redirect --->
<cfcollection action="delete" collection="#application.collection.name#" />
<cflocation url="/" addToken="false" />
<cfsetting enablecfoutputonly="true" />
<!--- let's get a list of the collections --->
<cfcollection action="list" name="collections" />
<cfoutput>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ColdFusion Solr Example | List Collections</title>
</head>
<body>
<h1>Collections</h1>
</cfoutput>
<cfif collections.recordCount>
<cfdump var="#collections#" />
<cfelse>
<cfoutput><p>There are no collections.</p></cfoutput>
</cfif>
<cfoutput>
<p><a href="/">Search.</a></p>
</body>
</html>
</cfoutput>
<cfsetting enablecfoutputonly="true" />
<!--- list the collections --->
<cfcollection action="list" name="qCollections" />
<!--- for testing purposes, delete the collection --->
<cfif listFindNoCase(valueList(qCollections.name), application.collection.name)>
<cfcollection action="delete" collection="#application.collection.name#">
</cfif>
<!--- create the collection --->
<cfcollection action="create" engine="solr" collection="#application.collection.name#" path="#application.collection.path#" language="English">
<!--- let's index a few documents --->
<cfindex collection="#application.collection.name#" action="update" type="path" key="#application.collection.content#" extensions=".pdf,.doc,.docx,.txt" recurse="yes" urlPath="#application.collection.url#" status="info" language="English">
<!--- output the page --->
<cfoutput>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ColdFusion Solr Example | Collections Setup</title>
</head>
<body>
<h1>Collections Setup</h1>
<cfdump var="#info#" />
<p><a href="/">Search.</a></p>
</body>
</html>
</cfoutput>
<cfsetting enablecfoutputonly="true" />
<!--- setup defaults --->
<cfparam name="form.searchInput" default="" />
<!--- list the collections --->
<cfcollection action="list" name="qCollections" />
<!--- let's search --->
<cfif len(form.searchInput)>
<cfsearch name="searchResults" collection="documents" criteria="#form.searchInput#" />
</cfif>
<cfoutput>
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ColdFusion Solr Example</title>
<style>
* { font-family: sans-serif; }
form { margin: 0 0 40px 0; }
article { margin: 0 0 10px 0; padding: 10px; border-bottom: 1px solid ##CCC; }
article h1 { font-size: 16px; margin: 0; }
</style>
</head>
<body>
<h1>ColdFusion &amp; Solr</h1>
<p>An example of using ColdFusion w/ Solar to index some documents from a file system.</p>
</cfoutput>
<cfif NOT qCollections.recordCount>
<cfoutput><p>There are no collections to search, <a href="/collections-setup.cfm">set one up now</a>.</p></cfoutput>
<cfelse>
<cfoutput>
<form method="post" action="/">
<label for="searchInput">
Search input:
<input type="text" id="searchInput" name="searchInput" value="#form.searchInput#" />
</label>
<input type="submit" />
</form>
</cfoutput>
</cfif>
<cfif len(form.searchInput)>
<cfloop query="searchResults">
<cfoutput>
<article>
<h1>#rereplace(searchResults.key, '.+/(.+)$', '\1', 'all')#</h1>
<a href="#searchResults.url#">View</a>
</article>
</cfoutput>
</cfloop>
</cfif>
<cfoutput>
</body>
</html>
</cfoutput>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment