OpenSearch/docs/groovy-api/search.asciidoc
Nik Everett 5cff2a046d Remove most of the need for // NOTCONSOLE
and be much more stingy about what we consider a console candidate.

* Add `// CONSOLE` to check-running
* Fix version in some snippets
* Mark groovy snippets as groovy
* Fix versions in plugins
* Fix language marker errors
* Fix language parsing in snippets

  This adds support for snippets who's language is written like
  `[source, txt]` and `["source","js",subs="attributes,callouts"]`.

  This also makes language required for snippets which is nice because
  then we can be sure we can grep for snippets in a particular language.
2016-09-06 10:32:54 -04:00

117 lines
2.7 KiB
Plaintext

[[search]]
== Search API
The search API is very similar to the
// {java}/java-search.html[]
Java search API. The Groovy
extension allows to provide the search source to execute as a `Closure`
including the query itself (similar to GORM criteria builder):
[source,groovy]
--------------------------------------------------
def search = node.client.search {
indices "test"
types "type1"
source {
query {
term(test: "value")
}
}
}
search.response.hits.each {SearchHit hit ->
println "Got hit $hit.id from $hit.index/$hit.type"
}
--------------------------------------------------
It can also be executed using the "Java API" while still using a closure
for the query:
[source,groovy]
--------------------------------------------------
def search = node.client.prepareSearch("test").setQuery({
term(test: "value")
}).gexecute();
search.response.hits.each {SearchHit hit ->
println "Got hit $hit.id from $hit.index/$hit.type"
}
--------------------------------------------------
The format of the search `Closure` follows the same JSON syntax as the
{ref}/search-search.html[Search API] request.
[[more-examples]]
=== More examples
Term query where multiple values are provided (see
{ref}/query-dsl-terms-query.html[terms]):
[source,groovy]
--------------------------------------------------
def search = node.client.search {
indices "test"
types "type1"
source {
query {
terms(test: ["value1", "value2"])
}
}
}
--------------------------------------------------
Query string (see
{ref}/query-dsl-query-string-query.html[query string]):
[source,groovy]
--------------------------------------------------
def search = node.client.search {
indices "test"
types "type1"
source {
query {
query_string(
fields: ["test"],
query: "value1 value2")
}
}
}
--------------------------------------------------
Pagination (see
{ref}/search-request-from-size.html[from/size]):
[source,groovy]
--------------------------------------------------
def search = node.client.search {
indices "test"
types "type1"
source {
from = 0
size = 10
query {
term(test: "value")
}
}
}
--------------------------------------------------
Sorting (see {ref}/search-request-sort.html[sort]):
[source,groovy]
--------------------------------------------------
def search = node.client.search {
indices "test"
types "type1"
source {
query {
term(test: "value")
}
sort = [
date : [ order: "desc"]
]
}
}
--------------------------------------------------