2013-08-28 19:24:34 -04:00
|
|
|
[[search]]
|
|
|
|
== Search API
|
|
|
|
|
|
|
|
The search API is very similar to the
|
2015-10-22 18:00:35 -04:00
|
|
|
// {java}/java-search.html[]
|
|
|
|
Java search API. The Groovy
|
2013-08-28 19:24:34 -04:00
|
|
|
extension allows to provide the search source to execute as a `Closure`
|
|
|
|
including the query itself (similar to GORM criteria builder):
|
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
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,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
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
|
2013-09-03 10:15:28 -04:00
|
|
|
{ref}/search-search.html[Search API] request.
|
2013-08-28 19:24:34 -04:00
|
|
|
|
2013-09-03 10:15:07 -04:00
|
|
|
|
2013-09-25 12:17:40 -04:00
|
|
|
[[more-examples]]
|
2013-08-28 19:24:34 -04:00
|
|
|
=== More examples
|
|
|
|
|
|
|
|
Term query where multiple values are provided (see
|
2013-09-03 10:15:28 -04:00
|
|
|
{ref}/query-dsl-terms-query.html[terms]):
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
def search = node.client.search {
|
|
|
|
indices "test"
|
|
|
|
types "type1"
|
|
|
|
source {
|
|
|
|
query {
|
|
|
|
terms(test: ["value1", "value2"])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
Query string (see
|
2013-09-03 10:15:28 -04:00
|
|
|
{ref}/query-dsl-query-string-query.html[query string]):
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
def search = node.client.search {
|
|
|
|
indices "test"
|
|
|
|
types "type1"
|
|
|
|
source {
|
|
|
|
query {
|
|
|
|
query_string(
|
|
|
|
fields: ["test"],
|
|
|
|
query: "value1 value2")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
|
|
|
Pagination (see
|
2013-09-03 10:15:28 -04:00
|
|
|
{ref}/search-request-from-size.html[from/size]):
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
def search = node.client.search {
|
|
|
|
indices "test"
|
|
|
|
types "type1"
|
|
|
|
source {
|
|
|
|
from = 0
|
|
|
|
size = 10
|
|
|
|
query {
|
|
|
|
term(test: "value")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|
|
|
|
|
2013-09-03 10:15:28 -04:00
|
|
|
Sorting (see {ref}/search-request-sort.html[sort]):
|
2013-08-28 19:24:34 -04:00
|
|
|
|
|
|
|
[source,js]
|
|
|
|
--------------------------------------------------
|
|
|
|
def search = node.client.search {
|
|
|
|
indices "test"
|
|
|
|
types "type1"
|
|
|
|
source {
|
|
|
|
query {
|
|
|
|
term(test: "value")
|
|
|
|
}
|
|
|
|
sort = [
|
|
|
|
date : [ order: "desc"]
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--------------------------------------------------
|