Last active
December 25, 2015 11:50
-
-
Save andrew-dixon/d9f2db32e30bfe5fee54 to your computer and use it in GitHub Desktop.
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
<cfscript> | |
// If you don't set this then the searchParams structure will be uppercased when converted to JSON and cause ElasticSearch to error. | |
processingdirective preserveCase="true"; | |
public struct function elasticSearchSearch( required string serverURL, | |
required struct searchParams, | |
required string index, | |
required string type) { | |
// Post the search parameters to the ElasticSearch index and type | |
http method="post" url="#arguments.serverURL#/#arguments.index#/#arguments.type#/_search" result="searchResponse" { | |
httpparam type="body" value="#SerializeJSON(arguments.searchParams)#"; | |
}; | |
// Return the ElasticSearch response as a structure. | |
return deSerializeJSON(searchResponse.filecontent); | |
} | |
// Set the server URL | |
variables.serverURL = 'http://localhost:9200'; | |
// Create a search parameters array of structures | |
variables.searchParams = [] | |
// In this first example we are getting an extact match against the field myField | |
variables.searchParams[1] = { | |
filter = { | |
term = { | |
myField = 1 | |
} | |
} | |
} | |
// In this second example we are going to do a full text search for the word "people" in the "description" field | |
variables.searchParams[2] = { | |
query = { | |
match: { | |
description: "people" | |
} | |
} | |
} | |
for (searchToPass in variables.searchParams) { | |
// Execute the search against the ElasticSearch server. | |
variables.searchResults = elasticSearchSearch( serverURL = variables.serverURL, | |
searchParams = variables.searchToPass, | |
index = 'myIndex', | |
type = 'myType'); | |
// The response from ElasticSearch is JSON, so deserialize it and dump it out. | |
dump(variables.searchResults); | |
writeOutput("<br><br>") | |
} | |
</cfscript> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above example shows two example searches against the ElasticSearch index created using data from a query, as shown here:
https://gist.github.com/andrew-dixon/27788b7c730dff66875f
The above code is for Railo 4.1+