|
| 1 | +<cfscript> |
| 2 | + // solr defaults |
| 3 | + THIS.host = "localhost"; |
| 4 | + THIS.port = 8983; |
| 5 | + THIS.path = "/solr"; |
| 6 | + THIS.solrURL = "http://#THIS.host#:#THIS.port##THIS.path#"; |
| 7 | + THIS.queueSize = 100; |
| 8 | + THIS.threadCount = 5; |
| 9 | + |
| 10 | + // java defaults |
| 11 | + THIS.javaLoaderInstance = ""; |
| 12 | +</cfscript> |
| 13 | + |
| 14 | + |
| 15 | +<cffunction name="init" access="public" output="false" returntype="CFSolrLib"> |
| 16 | + <cfargument name="javaloaderInstance" required="true" hint="An instance of JavaLoader." /> |
| 17 | + <cfargument name="host" required="true" type="string" default="localhost" hint="Solr Server host" /> |
| 18 | + <cfargument name="port" required="false" type="numeric" default="8983" hint="Port Solr server is running on" /> |
| 19 | + <cfargument name="path" required="false" type="string" default="/solr" hint="Path to solr instance"> |
| 20 | + <cfargument name="queueSize" required="false" type="numeric" default="100" hint="The buffer size before the documents are sent to the server"> |
| 21 | + <cfargument name="threadCount" required="false" type="numeric" default="5" hint="The number of background threads used to empty the queue"> |
| 22 | + <cfargument name="binaryEnabled" required="false" type="boolean" default="true" hint="Should we use the faster binary data transfer format?"> |
| 23 | + |
| 24 | + <cfset THIS.javaLoaderInstance = ARGUMENTS.javaloaderInstance /> |
| 25 | + <cfset THIS.host = ARGUMENTS.host /> |
| 26 | + <cfset THIS.port = ARGUMENTS.port /> |
| 27 | + <cfset THIS.path = ARGUMENTS.path /> |
| 28 | + <cfset THIS.solrURL = "http://#THIS.host#:#THIS.port##THIS.path#" /> |
| 29 | + <cfset THIS.queueSize = ARGUMENTS.queueSize /> |
| 30 | + <cfset THIS.threadCount = ARGUMENTS.threadCount /> |
| 31 | + |
| 32 | + <cfscript> |
| 33 | + // create an update server instance |
| 34 | + THIS.solrUpdateServer = THIS.javaLoaderInstance.create("org.apache.solr.client.solrj.impl.StreamingUpdateSolrServer").init(THIS.solrURL,THIS.queueSize,THIS.threadCount); |
| 35 | + |
| 36 | + // create a query server instance |
| 37 | + THIS.solrQueryServer = THIS.javaLoaderInstance.create("org.apache.solr.client.solrj.impl.CommonsHttpSolrServer").init(THIS.solrURL); |
| 38 | + |
| 39 | + // enable binary |
| 40 | + if (ARGUMENTS.binaryEnabled) { |
| 41 | + BinaryRequestWriter = THIS.javaLoaderInstance.create("org.apache.solr.client.solrj.impl.BinaryRequestWriter"); |
| 42 | + THIS.solrUpdateServer.setRequestWriter(BinaryRequestWriter.init()); // comment this out if you didn't enable binary |
| 43 | + THIS.solrQueryServer.setRequestWriter(BinaryRequestWriter.init()); // comment this out if you didn't enable binary |
| 44 | + } |
| 45 | + </cfscript> |
| 46 | + |
| 47 | + <cfreturn this/> |
| 48 | +</cffunction> |
| 49 | + |
| 50 | +<cffunction name="search" access="public" output="false" hint="Search for documents in the Solr index"> |
| 51 | + <cfargument name="q" type="string" required="true" hint="Your query string" /> |
| 52 | + <cfargument name="start" type="numeric" required="false" default="0" hint="Offset for results, starting with 0" /> |
| 53 | + <cfargument name="rows" type="numeric" required="false" default="20" hint="Number of rows you want returned" /> |
| 54 | + <cfargument name="params" type="array" required="false" default="#arrayNew(1)#" hint="An array of name value pairs of additional params. Values do not need to be only strings." /> |
| 55 | + |
| 56 | + <cfset thisQuery = THIS.javaLoaderInstance.create("org.apache.solr.client.solrj.SolrQuery").init(ARGUMENTS.q).setStart(ARGUMENTS.start).setRows(ARGUMENTS.rows) /> |
| 57 | + <cfloop array="#ARGUMENTS.params#" index="thisParam"> |
| 58 | + <cfset thisQuery.setParam(thisParam.name,thisParam.value)> |
| 59 | + </cfloop> |
| 60 | + |
| 61 | + <!--- we do this instead of making the user call java functions, to work around a CF bug ---> |
| 62 | + <cfset response = THIS.solrQueryServer.query(thisQuery) /> |
| 63 | + <cfset ret = structNew() /> |
| 64 | + <cfset ret.results = response.getResults() / > |
| 65 | + <cfset ret.spellCheck = response.getSpellCheckResponse() / > |
| 66 | + |
| 67 | + <cfreturn duplicate(ret) /> <!--- duplicate clears out the case-sensitive structure ---> |
| 68 | +</cffunction> |
| 69 | + |
| 70 | +<cffunction name="add" access="public" output="false" hint="Add a document to the Solr index"> |
| 71 | + <cfargument name="doc" type="array" required="true" hint="An array of field objects, with name, value, and an optional boost attribute. {name:""Some Name"",value:""Some Value""[,boost:5]}" /> |
| 72 | + <cfargument name="docBoost" type="numeric" required="false" hint="Value of boost for this document." /> |
| 73 | + |
| 74 | + <cfset thisDoc = THIS.javaLoaderInstance.create("org.apache.solr.common.SolrInputDocument").init() /> |
| 75 | + <cfif isDefined("ARGUMENTS.docBoost")> |
| 76 | + <cfset thisDoc.setDocumentBoost(ARGUMENTS.docBoost) /> |
| 77 | + </cfif> |
| 78 | + |
| 79 | + <cfloop array="#ARGUMENTS.doc#" index="thisParam"> |
| 80 | + <cfif isDefined("thisParam.boost")> |
| 81 | + <cfset thisDoc.addField(thisParam.name,thisParam.value,thisParam.boost) /> |
| 82 | + <cfelse> |
| 83 | + <cfset thisDoc.addField(thisParam.name,thisParam.value) /> |
| 84 | + </cfif> |
| 85 | + </cfloop> |
| 86 | + |
| 87 | + <cfreturn THIS.solrUpdateServer.add(thisDoc) /> |
| 88 | +</cffunction> |
| 89 | + |
| 90 | +<cffunction name="addField" access="public" output="false" returnType="array" hint="Creates a field object and appends it to the array. This is a helper method for adding to your index."> |
| 91 | + <cfargument name="documentArray" required="true" type="array" hint="An array to add your document field to." /> |
| 92 | + <cfargument name="name" required="true" type="string" hint="Name of your field." /> |
| 93 | + <cfargument name="value" required="true" hint="Value of your field." /> |
| 94 | + <cfargument name="boost" required="false" type="numeric" hint="An array to add your document field to." /> |
| 95 | + |
| 96 | + <cfset thisField = structNew() /> |
| 97 | + <cfset thisField.name = ARGUMENTS.name /> |
| 98 | + <cfset thisField.value = ARGUMENTS.value /> |
| 99 | + <cfif isDefined("ARGUMENTS.boost")> |
| 100 | + <cfset thisField.boost = ARGUMENTS.boost /> |
| 101 | + </cfif> |
| 102 | + |
| 103 | + <cfset arrayAppend(ARGUMENTS.documentArray,thisField) /> |
| 104 | + |
| 105 | + <cfreturn ARGUMENTS.documentArray /> |
| 106 | +</cffunction> |
| 107 | + |
| 108 | +<cffunction name="addFile" access="public" output="false" hint="Creates a field object for appending to an array. This is a helper method for adding to your index."> |
| 109 | + <cfargument name="id" required="true" hint="The unique ID of the document" /> |
| 110 | + <cfargument name="file" required="true" type="string" hint="path to the document to be added" /> |
| 111 | + <cfargument name="fmap" required="false" type="struct" hint="The mappings of document metadata fields to index fields." /> |
| 112 | + <cfargument name="saveMetadata" required="false" type="boolean" default="true" hint="Store non-mapped metadata in dynamic fields" /> |
| 113 | + <cfargument name="metadataPrefix" required="false" type="string" default="attr_" hint="Metadata dynamic field prefix" /> |
| 114 | + |
| 115 | + <cfset docRequest = THIS.javaLoaderInstance.create("org.apache.solr.client.solrj.request.ContentStreamUpdateRequest").init("/update/extract") /> |
| 116 | + <cfset docRequest.addFile(createObject("java","java.io.File").init(ARGUMENTS.file)) /> |
| 117 | + <cfset docRequest.setParam("literal.id",ARGUMENTS.id) /> |
| 118 | + <cfif ARGUMENTS.saveMetadata> |
| 119 | + <cfset docRequest.setParam("uprefix",metadataPrefix) /> |
| 120 | + </cfif> |
| 121 | + <cfif isDefined("ARGUMENTS.fmap")> |
| 122 | + <cfloop list="#structKeyList(ARGUMENTS.fmap)#" index="thisKey"> |
| 123 | + <cfset docRequest.setParam("fmap.#thisKey#",ARGUMENTS.fmap[thisKey]) /> |
| 124 | + </cfloop> |
| 125 | + </cfif> |
| 126 | + |
| 127 | + <cfreturn THIS.solrUpdateServer.request(docRequest) /> |
| 128 | +</cffunction> |
| 129 | + |
| 130 | +<cffunction name="deleteByID" access="public" output="false" hint="Delete a document from the index by ID"> |
| 131 | + <cfargument name="id" type="string" required="true" hint="ID of object to delete."> |
| 132 | + |
| 133 | + <cfset THIS.solrUpdateServer.deleteByID(ARGUMENTS.id) /> |
| 134 | +</cffunction> |
| 135 | + |
| 136 | +<cffunction name="deleteByQuery" access="public" output="false" hint="Delete a document from the index by Query"> |
| 137 | + <cfargument name="q" type="string" required="true" hint="Query string to delete objects with."> |
| 138 | + |
| 139 | + <cfset THIS.solrUpdateServer.deleteByQuery(ARGUMENTS.q) /> |
| 140 | +</cffunction> |
| 141 | + |
| 142 | +<cffunction name="resetIndex" access="public" output="false" hint="Clear out the index."> |
| 143 | + <cfset THIS.deleteByQuery("*:*") /> |
| 144 | + <cfset THIS.commit() /> |
| 145 | + <cfset THIS.optimize() /> |
| 146 | +</cffunction> |
| 147 | + |
| 148 | +<cffunction name="rollback" access="public" output="false" hint="Roll back all pending changes to the index"> |
| 149 | + <cfset THIS.solrUpdateServer.rollback() /> |
| 150 | +</cffunction> |
| 151 | + |
| 152 | +<cffunction name="commit" access="public" output="false" hint="Commit all pending changes to the index"> |
| 153 | + <cfset THIS.solrUpdateServer.commit() /> |
| 154 | +</cffunction> |
| 155 | + |
| 156 | +<cffunction name="optimize" access="public" output="false" hint="Commit all pending changes to the index"> |
| 157 | + <cfset THIS.solrUpdateServer.optimize() /> |
| 158 | +</cffunction> |
0 commit comments