5 Searching - Reference Documentation
Authors: Manuarii Stein, Stephane Maldini, Serge P. Nekoval
Version: 0.18.7.1-SNAPSHOT
5 Searching
The plugin provides 2 ways to send search requests.- You can use the
elasticSearchServiceand its publicsearchmethod for cross-domain searching, meaning that ElasticSearch
def res = elasticSearchService.search("${params.query}")
// 'res' search results may contains multiple types of results- You can use the injected dynamic method in the domain for domain-specific searching.
def res = Tweet.search("${params.query}")
// 'res' search results contains only Tweet instancesMap containing 3 entries:
- a
totalentry, representing the total number of hits found - a
searchResultsentry, containing the hits - a
scoresentry, containing the hits scores
def res = Tweet.search("${params.query}") println "Found ${res.total} result(s)" res.searchResults.each { println it.message }def res = elasticSearchService.search("${params.query}") println "Found ${res.total} result(s)" res.searchResults.each { if(it instanceof Tweet) { println it.message } else { println it.toString() } }
countHits()
method. It will only return an Integer representing the total hits matching your query.Example
def res = Tweet.countHits("${params.query}") println "Found ${res} result(s)"def res = elasticSearchService.countHits("${params.query}", [indices:'test']) println "Found ${res} result(s)"
5.1 Query Strings
The search method injected in the domain or theElasticSearchService has multiple signatures available.
You can pass it a simple String to compute your search request. That string will be parsed by the Lucene query parser
so feel free to use its syntax to do more specific search query.You can find out about the syntax on the Apache Lucene website.Example
def results = elasticSearchService.search("${params.query}") def resultsTweets = Tweet.search("message:${params.query}")
5.2 Query Closure
You can use the Groovy Query DSL to build your search query as aClosure.
The format of the search Closure follow the same JSON syntax as the ElasticSearch REST API
and the Java Query DSL.Example
def result = elasticSearchService.search(searchType:'dfs_query_and_fetch') {
bool {
must {
query_string(query: params.query)
}
if (params.firstname) {
must {
term(firstname: params.firstname)
}
}
}
}5.3 Highlighting
The search method support highlighting: automatic wrapping of the matching terms in the search results with HTML/XML/Whatever tags. You can activate this with aClosure containing the highlight settings in the search method highlight parameter.
The format of the Closure for defining the highlight settings is the same as the
ElasticSearch REST API.Example
// Define the pre & post tag that will wrap each term matched in the document.
def highlighter = {
field 'message'
field 'tags.name'
preTags '<strong>'
postTags '</strong>'
}def results = Tweet.search("${params.query}", [highlight: highlightSettings])Highlight results
If a search result is found, thesearch method will add a highlight entry in the map result.
That entry contains a List with every highlighted fragments/fields found for each hit.def results = Tweet.search("${params.query}", [highlight: { field 'message' }]) def highlighted = results.highlightresults?.searchResults?.eachWithIndex { hit, index -> // Retrieve the 'message' field fragments for the current hits def fragments = highlighted[index].message?.fragments // Print the fragment println fragments?.size() ? fragments[0] : '' }
Highlighted fields
To determine which fields are to be processed by ElasticSearch, use thefield setting.
You can call the field setting as many time as you want to add any field.Signature
field <fieldName>[, <fragmentSize>[, <numberOfFragment>]]
def highlightSettings = {
field 'message' // Add the 'message' field in the highlighted fields list
field 'tags.name' // Add the 'name' field contained in the 'tags' field of
// the document in the highlighted fields list
field 'thatAwesomeField', 0, 20 // Add the 'thatAwesomeField' field with
// some values fixed for fragmentSize and numberOfFragment parameters
}def highlightSettings2 = {
field '_all' // Add the special '_all' field in the highlighted fields list
}def results = Tweet.search("${params.query}", [highlight: highlightSettings])
def results2 = Tweet.search("${params.query}", [highlight: highlightSettings2])Highlighting tags
By default, ElasticSearch will use emphasis tag "<em>...</em>" to wrap the matching text.
You can customize the tags with the preTags and postTags settings.def highlightSettings = {
field 'message'
preTags '<myAweSomeTag>'
postTags '</myAweSomeTag>'
}