(Quick Reference)

search

Purpose

Search through indices for the specified search query. The returned result may contain different type of domain.

Examples

def highlighter = {
  field 'message', 0, 0
  preTags '<strong>'
  postTags '</strong>'
}

def res = elasticSearchService.search("${params.query}") def res = elasticSearchService.search("${params.query}", [from:0, size:30, highlighter:highlighter]) def res = elasticSearchService.search(){ queryString("${params.query}") } def res = elasticSearchService.search(from:0, size:30, indices:"tweet") { queryString("${params.query}") }

def sortBuilder = SortBuilders.fieldSort("name").order(SortOrder.ASC)

def res = elasticSearchService.search([sort: sortBuilder], { match("name": "foo") })

def res = elasticSearchService.search(null as Closure, { range { "price"(gte: 1.99, lte: 2.3) } })

def res = elasticSearchService.search({ wildcard("name": "*st") }, [:])

def hasParentQuery = QueryBuilders.hasParentQuery("store", QueryBuilders.matchQuery("owner", "Horst")) def result = elasticSearchService.search(hasParentQuery)

Description

search signatures:

def search(Map params, Closure query, filter = null)
def search(Map params, QueryBuilder query, filter = null)
def search(String query, Map params = [:])
def search(Closure query, Map params)
def search(Closure query, filter = null, Map params = [:])
def search(QueryBuilder query, filter = null, Map params = [:])

Parameters

  • query - The search query.
    • As a String, the query is parsed by the Lucene query parser for advanced searching.
    • Can also be a Closure, using the Groovy Query DSL of the ElasticSearch groovy client.
    • Can also be a QueryBuilder.
  • filter - The search filter
    • A Closure, using the Groovy Query DSL of the ElasticSearch groovy client.
    • can also be a FilterBuilder.
  • params - A list of additional parameters to customize the searching process
    • from and size - From (hit) and the size (number of hits) to return.
    • sort - Sort based on different fields including ElasticSearch's internal ones (like _score)
      • As a String, sort by the field with this name (e.g. sort: 'name' means 'sort by name')
      • Can also be a SortBuilder
    • order - Sort order ("ASC" or "DESC", case insensitive). Default: "ASC"
    • indices - Limit the search only to the specified indices (may be a String, or Collection of String)
    • types - Limit the search only to the specified types (domains) (may be a String, or Collection of String).
    • highlight - A Closure containing the highlighting settings.

Returned value

Return a Map containing:

  • a total entry, representing the total number of hits found.
  • a searchResults entry, containing the hits.
  • a scores entry, containing the hits' scores.
  • a highlight entry if the highlighter parameter was set.
  • a sort entry if the sort parameter was set. Contains all sortValues of the search. Maps the id of a search hit to the sort values
    • Example: id: [12.34]

ElasticSearch Builders

ElasticSearch provides many builders (e.g. QueryBuilders) and builder factories (ending with *Builders, e.g. QueryBuilders). The factories provide methods for creating the concrete builders - e.g. QueryBuilders.matchQuery() produces a MatchQueryBuilder, which can be used to create a match query.

Here is a list of builder factories:

  • org.elasticsearch.index.mapper.MapperBuilders
  • org.elasticsearch.index.query.QueryBuilders
  • org.elasticsearch.index.query.FilterBuilders
  • org.elasticsearch.search.sort.SortBuilders
  • org.elasticsearch.search.facet.FacetBuilders
  • org.elasticsearch.cluster.routing.RoutingBuilders

JSON vs. closure syntax comparison for queries and filters

Since there seems to be no obvious rule for the conversion of queries and filters between the JSON syntax and the closure syntax, I just provide some examples.

Range filter

JSON

{
    "range" : {
        "price" : {
            "gte" : 1.99,
            "lte" : 2.3
        }
    }
}

Closure

{
    range {
        "price"(gte: 1.99, lte: 2.3)
    }
}

Wildcard Query

JSON

{
    "wildcard" : {
        "name" : "*st"
    }
}

Closure

{
    wildcard("name": "*st")
}

Bool Query

JSON

{
    "bool" : {
        "must" : {
            "term" : { "firstname" : "kimchy" }
        }
    }
}

Closure

{
  bool {
      must {
          term(firstname: "kimchy")
      }
  }
}

Geo_reference filter

JSON

{
    "filtered" : {
        "filter" : {
            "geo_distance" : {
                "distance" : "200km",
                "pin.location" : {
                    "lat" : 40,
                    "lon" : -70
                }
            }
        }
    }
}

Closure

{
    geo_distance(
        'distance': '200km',
        'pin.location': [lat: 40, lon: -70]
    )
}