Created
July 18, 2013 17:16
-
-
Save tgmweb/6031116 to your computer and use it in GitHub Desktop.
Example CFC for elasticSearch interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfcomponent output="false" > | |
<cffunction name="init" access="public" returntype="any" > | |
<!--- this just sets up the CFC with the elasticSearch URL and port ---> | |
<cfargument name="searchEndPoint" type="string" required="true"/> | |
<cfargument name="searchPort" type="string" required="true"/> | |
<cfset variables.searchEndPoint = arguments.searchEndPoint /> | |
<cfset variables.searchPort = arguments.searchPort /> | |
<cfreturn this /> | |
</cffunction> | |
<cffunction name="search" access="public" returntype="Struct"> | |
<cfargument name="collection" type="string" required="true"> <!--- this is your "collection". Think database name ---> | |
<cfargument name="type" type="string" required="true"> <!--- this is object type. Think table name ---> | |
<cfargument name="sQuery" type="struct" required="true"> <!--- the query struct ---> | |
<cfargument name="routing" type="string" required="true" default=""> <!--- optional routing - see docs ---> | |
<cfset var pURL = "#variables.searchEndPoint#/#arguments.collection#/#arguments.type#/_search"> | |
<cfif arguments.routing neq ""> | |
<cfset pURL = "#pURL#?routing=#arguments.routing#"> | |
</cfif> | |
<cfhttp port="#variables.searchPort#" url="#pURL#" method="POST"> | |
<cfhttpparam type="header" name="content-type" value="application/json"> | |
<cfhttpparam type="body" name="json" value="#SerializeJSON(sQuery)#"> | |
</cfhttp> | |
<cfset var stResponse = {}> | |
<cfif cfhttp.statusCode neq "200 OK"> | |
<cfset stResponse.success=false /> | |
<cfset stResponse.statusCode=cfhttp.statusCode /> | |
<cfset stResponse.result = DeSerializeJSON(cfhttp.fileContent)/> | |
<cfset stResponse.incoming =sQuery> | |
<cfelse> | |
<cfset stResponse.success=true /> | |
<cfset stResponse.result = DeSerializeJSON(cfhttp.fileContent)/> | |
</cfif> | |
<cfreturn stResponse /> | |
</cffunction> | |
<cffunction name="delete" access="public" returntype="void"> | |
<cfargument name="collection" type="string" required="true"> | |
<cfhttp port="#variables.searchPort#" url="#variables.searchEndPoint#/#arguments.collection#" method="DELETE"> | |
</cffunction> | |
<cffunction name="addItem" access="public" returntype="any"> | |
<cfargument name="collection" type="string" required="true"><!--- this is your "collection" ---> | |
<cfargument name="type" type="string" required="true"> <!--- this is your object type ---> | |
<cfargument name="id" type="string" required="true"> <!--- unique object ID ---> | |
<cfargument name="item" type="struct" required="true"> <!--- the json object ---> | |
<cfargument name="routing" type="string" required="true" default=""> | |
<cfset var pURL = "#variables.searchEndPoint#/#collection#/#arguments.type#/#arguments.id#"> | |
<cfif arguments.routing neq ""> | |
<cfset pURL = "#pURL#?routing=#arguments.routing#"> | |
</cfif> | |
<cfhttp method="POST" port="#variables.searchPort#" url="#pURL#" result="httpRequest" timeout="20" charset="utf-8"> | |
<cfhttpparam type="header" name="Content-Type" value="application/json; charset=utf-8" /> | |
<cfhttpparam type="body" value="#SerializeJSON(arguments.item)#"> | |
</cfhttp> | |
<cfreturn httpRequest> | |
</cffunction> | |
</cfcomponent> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// a very simple query string type search: | |
<cfset var sQuery = {}> | |
<cfset sQuery["size"] = 10> <!--- size of returned results ---> | |
<cfset sQuery["from"] = 1> <!--- start row ---> | |
<!--- this will return everything ---> | |
<cfset sQuery["query"]["filtered"]["query"]["match_all"] = {}> | |
<!--- this will return a simple queried result ---> | |
<cfset sQuery["query"]["filtered"]["query"]["query_string"]["query"] = "Test"> | |
<!--- this will return a specific attribute, for example a product type ---> | |
<cfset sQuery["query"]["filtered"]["query"]["match"]["ProductType"] = "Used"> | |
<!--- let's do the search ---> | |
<cfset results = MyCFC.search("@collectionName@",'@type@',sQuery)> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The first query returns everything for the elasticSearch collection. In SQL terms, this might be like Select * from Products;
The second returns a result where any attribute has the value "test". There's no real SQL comparison here, other than "Select * from Products where allcolums LIKE '%Test%'
The third returns a result based on a attribute. Think Select * from Products where Product Type = 'Used'.
You could also do the second and third together. That would be similar to: Select * from Products where productType = 'Used' AND anycolumn LIKE '%Test%'