{"id":544,"date":"2003-12-06T22:10:26","date_gmt":"2003-12-07T02:10:26","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=544"},"modified":"2003-12-06T22:10:26","modified_gmt":"2003-12-07T02:10:26","slug":"indexing-database-content-with-lucene-coldfusion","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/12\/06\/indexing-database-content-with-lucene-coldfusion\/","title":{"rendered":"Indexing Database Content with Lucene &amp; ColdFusion"},"content":{"rendered":"<p>Terry emailed me a couple days ago wondering how he could use <a href=\"http:\/\/www.macromedia.com\/software\/coldfusion\/\">ColdFusion<\/a> and <a href=\"http:\/\/jakarta.apache.org\/lucene\/docs\/index.html\">Lucene<\/a> to index and then search a database table.  Since we&#8217;re completely socked in here in Boston, I had nothing better to do today that hack together a quick snippet that does just that:<br \/>\n<code><br \/>\n&lt;cfset an = CreateObject(\"java\", \"org.apache.lucene.analysis.StopAnalyzer\")&gt;<br \/>\n&lt;cfset an.init()&gt;<br \/>\n&lt;cfset writer = CreateObject(\"java\", \"org.apache.lucene.index.IndexWriter\")&gt;<br \/>\n&lt;cfset writer.init(\"C:\\mysite\\index\\\", an, \"true\")&gt;<br \/>\n&lt;cfquery name=\"contentIndex\" datasource=\"sample\"&gt;<br \/>\nselect label, description, id<br \/>\nFROM product<br \/>\n&lt;\/cfquery&gt;<br \/>\n&lt;cfloop query=\"contentIndex\"&gt;<br \/>\n&nbsp;&nbsp;&lt;cfset d = CreateObject(\"java\", \"org.apache.lucene.document.Document\")&gt;<br \/>\n&nbsp;&nbsp;&lt;cfset fld = CreateObject(\"java\", \"org.apache.lucene.document.Field\")&gt;<br \/>\n&nbsp;&nbsp;&lt;cfset content = contentIndex.description&gt;<br \/>\n&nbsp;&nbsp;&lt;cfset title = contentIndex.label&gt;<br \/>\n&nbsp;&nbsp;&lt;cfset urlpath = \"\/products\/detail.cfm?id=\" &amp; contentIndex.id&gt;<br \/>\n&nbsp;&nbsp;&lt;cfset d.add(fld.Keyword(\"url\", urlpath))&gt;<br \/>\n&nbsp;&nbsp;&lt;cfset d.add(fld.Text(\"title\", title))&gt;<br \/>\n&nbsp;&nbsp;&lt;cfset d.add(fld.UnIndexed(\"summary\", content))&gt;<br \/>\n&nbsp;&nbsp;&lt;cfset d.add(fld.UnStored(\"body\", content))&gt;<br \/>\n&nbsp;&nbsp;&lt;cfset writer.addDocument(doc)&gt;<br \/>\n&lt;\/cfloop&gt;&nbsp;&nbsp;<br \/>\n&lt;cfset writer.close()&gt;<br \/>\n<\/code><br \/>\nThe only real change from the code that I wrote previously to index a document was that instead of looping over the file system looking for documents, I loop over a query and then indexed the text of a column from the database rather than the text of a document.  (I would have written in in <a href=\"http:\/\/livedocs.macromedia.com\/coldfusion\/6\/Developing_ColdFusion_MX_Applications_with_CFML\/CFScript.htm\">CFScript<\/a>, but you can&#8217;t do queries from CFScript yet, unless you use a UDF to do the query)<\/p>\n<p>You can download the source <a href=\"\/projects\/coldfusion_lucene\/cfml\/indexing_database.cfm\">here<\/a>, if you&#8217;re so inclined.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Terry emailed me a couple days ago wondering how he could use ColdFusion and Lucene to index and then search a database table. Since we&#8217;re completely socked in here in Boston, I had nothing better to do today that hack together a quick snippet that does just that: &lt;cfset an = CreateObject(&#8220;java&#8221;, &#8220;org.apache.lucene.analysis.StopAnalyzer&#8221;)&gt; &lt;cfset an.init()&gt; &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/12\/06\/indexing-database-content-with-lucene-coldfusion\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Indexing Database Content with Lucene &amp; ColdFusion<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,19],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/544"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=544"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/544\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}