1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
[[search]]
== Search API
The search API is very similar to the
{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,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
{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,js]
--------------------------------------------------
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,js]
--------------------------------------------------
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,js]
--------------------------------------------------
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,js]
--------------------------------------------------
def search = node.client.search {
indices "test"
types "type1"
source {
query {
term(test: "value")
}
sort = [
date : [ order: "desc"]
]
}
}
--------------------------------------------------
|