search.asciidoc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. [[search]]
  2. == Search API
  3. The search API is very similar to the
  4. {java}/search.html[Java search API]. The Groovy
  5. extension allows to provide the search source to execute as a `Closure`
  6. including the query itself (similar to GORM criteria builder):
  7. [source,js]
  8. --------------------------------------------------
  9. def search = node.client.search {
  10. indices "test"
  11. types "type1"
  12. source {
  13. query {
  14. term(test: "value")
  15. }
  16. }
  17. }
  18. search.response.hits.each {SearchHit hit ->
  19. println "Got hit $hit.id from $hit.index/$hit.type"
  20. }
  21. --------------------------------------------------
  22. It can also be executed using the "Java API" while still using a closure
  23. for the query:
  24. [source,js]
  25. --------------------------------------------------
  26. def search = node.client.prepareSearch("test").setQuery({
  27. term(test: "value")
  28. }).gexecute();
  29. search.response.hits.each {SearchHit hit ->
  30. println "Got hit $hit.id from $hit.index/$hit.type"
  31. }
  32. --------------------------------------------------
  33. The format of the search `Closure` follows the same JSON syntax as the
  34. {ref}/search-search.html[Search API] request.
  35. [[more-examples]]
  36. === More examples
  37. Term query where multiple values are provided (see
  38. {ref}/query-dsl-terms-query.html[terms]):
  39. [source,js]
  40. --------------------------------------------------
  41. def search = node.client.search {
  42. indices "test"
  43. types "type1"
  44. source {
  45. query {
  46. terms(test: ["value1", "value2"])
  47. }
  48. }
  49. }
  50. --------------------------------------------------
  51. Query string (see
  52. {ref}/query-dsl-query-string-query.html[query string]):
  53. [source,js]
  54. --------------------------------------------------
  55. def search = node.client.search {
  56. indices "test"
  57. types "type1"
  58. source {
  59. query {
  60. query_string(
  61. fields: ["test"],
  62. query: "value1 value2")
  63. }
  64. }
  65. }
  66. --------------------------------------------------
  67. Pagination (see
  68. {ref}/search-request-from-size.html[from/size]):
  69. [source,js]
  70. --------------------------------------------------
  71. def search = node.client.search {
  72. indices "test"
  73. types "type1"
  74. source {
  75. from = 0
  76. size = 10
  77. query {
  78. term(test: "value")
  79. }
  80. }
  81. }
  82. --------------------------------------------------
  83. Sorting (see {ref}/search-request-sort.html[sort]):
  84. [source,js]
  85. --------------------------------------------------
  86. def search = node.client.search {
  87. indices "test"
  88. types "type1"
  89. source {
  90. query {
  91. term(test: "value")
  92. }
  93. sort = [
  94. date : [ order: "desc"]
  95. ]
  96. }
  97. }
  98. --------------------------------------------------