search.asciidoc 2.7 KB

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