put-search-application.asciidoc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. [role="xpack"]
  2. [[put-search-application]]
  3. === Put Search Application
  4. beta::[]
  5. ++++
  6. <titleabbrev>Put Search Application</titleabbrev>
  7. ++++
  8. Creates or updates a Search Application.
  9. [[put-search-application-request]]
  10. ==== {api-request-title}
  11. `PUT _application/search_application/<name>`
  12. [[put-search-application-prereqs]]
  13. ==== {api-prereq-title}
  14. Requires the `manage_search_application` cluster privilege.
  15. Also requires <<privileges-list-indices,manage privileges>> on all indices that are added to the Search Application.
  16. [[put-search-application-path-params]]
  17. ==== {api-path-parms-title}
  18. `create`::
  19. (Optional, Boolean) If `true`, this request cannot replace or update existing Search Applications.
  20. Defaults to `false`.
  21. `<body>`::
  22. (Required, object)
  23. Contains parameters for a search application:
  24. +
  25. .Properties of `<body>` objects
  26. [%collapsible%open]
  27. ====
  28. `indices`::
  29. (Required, array of strings)
  30. The <<indices,indices>> associated with this search application. All indices need to exist in order to be added to a search application.
  31. `template`::
  32. (Optional, object)
  33. The <<search-template,search template>> associated with this search application. The search application's template is only stored and accessible through the search application.
  34. - This search template must be a Mustache template.
  35. - The template must contain a Mustache script and script source.
  36. - The template may be modified with subsequent <<put-search-application,put search application>> requests.
  37. - If no template is specified when creating a search application, or if a template is removed from a search application, we use the <<query-string-query-ex-request,query_string>> defined in the template examples as a default.
  38. - This template will be used by the <<search-application-search,search application search>> API to execute searches.
  39. - The template accepts an optional `dictionary` parameter which defines a https://json-schema.org[JSON schema] used for validating parameters sent to the <<search-application-search,search application search>> API.
  40. .Properties of `<template>`
  41. [%collapsible%open]
  42. =====
  43. `script`::
  44. (Required, object)
  45. The associated mustache template.
  46. `dictionary`::
  47. (Optional, object)
  48. The dictionary used to validate the parameters used with the <<search-application-search, search application search>> API. The dictionary must be a valid JSON schema.
  49. If `dictionary` is not specified, then the parameters will not be validated before being applied in the template.
  50. =====
  51. ====
  52. [[put-search-application-response-codes]]
  53. ==== {api-response-codes-title}
  54. `404`::
  55. Search Application `<name>` does not exist.
  56. `409`::
  57. Search Application `<name>` exists and `create` is `true`.
  58. [[put-search-application-example]]
  59. ==== {api-examples-title}
  60. The following example creates or updates a new Search Application called `my-app`:
  61. ////
  62. [source,console]
  63. ----
  64. PUT /index1
  65. PUT /index2
  66. PUT _application/search_application/my-app
  67. {
  68. "indices": [ "index1", "index2" ],
  69. "template": {
  70. "script": {
  71. "source": {
  72. "query": {
  73. "query_string": {
  74. "query": "{{query_string}}",
  75. "default_field": "{{default_field}}"
  76. }
  77. }
  78. },
  79. "params": {
  80. "query_string": "*",
  81. "default_field": "*"
  82. }
  83. },
  84. "dictionary": {
  85. "properties": {
  86. "query_string": {
  87. "type": "string"
  88. },
  89. "default_field": {
  90. "type": "string",
  91. "enum": [
  92. "title",
  93. "description"
  94. ]
  95. },
  96. "additionalProperties": false
  97. },
  98. "required": [
  99. "query_string"
  100. ]
  101. }
  102. }
  103. }
  104. ----
  105. // TESTSETUP
  106. //////////////////////////
  107. [source,console]
  108. --------------------------------------------------
  109. DELETE _application/search_application/my-app
  110. DELETE /index1
  111. DELETE /index2
  112. --------------------------------------------------
  113. // TEARDOWN
  114. ////
  115. [source,console]
  116. ----
  117. PUT _application/search_application/my-app
  118. {
  119. "indices": [ "index1", "index2" ],
  120. "template": {
  121. "script": {
  122. "source": {
  123. "query": {
  124. "query_string": {
  125. "query": "{{query_string}}",
  126. "default_field": "{{default_field}}"
  127. }
  128. }
  129. },
  130. "params": {
  131. "query_string": "*",
  132. "default_field": "*"
  133. }
  134. },
  135. "dictionary": {
  136. "properties": {
  137. "query_string": {
  138. "type": "string"
  139. },
  140. "default_field": {
  141. "type": "string",
  142. "enum": [
  143. "title",
  144. "description"
  145. ]
  146. },
  147. "additionalProperties": false
  148. },
  149. "required": [
  150. "query_string"
  151. ]
  152. }
  153. }
  154. }
  155. ----
  156. When the above `dictionary` parameter is specified, the <<search-application-search, search application search>> API will perform the following parameter validation:
  157. * It accepts only the `query_string` and `default_field` parameters
  158. * It verifies that `query_string` and `default_field` are both strings
  159. * It accepts `default_field` only if it takes the values `title` or `description`
  160. If the parameters are not valid, the the <<search-application-search, search application search>> API will return an error.
  161. [source,console]
  162. ----
  163. POST _application/search_application/my-app/_search
  164. {
  165. "params": {
  166. "default_field": "author",
  167. "query_string": "Jane"
  168. }
  169. }
  170. ----
  171. // TEST[catch:bad_request]
  172. In the above example, the value of the `default_field` parameter is not valid, therefore Elasticsearch will return an error:
  173. [source,JSON]
  174. ----
  175. {
  176. "error": {
  177. "root_cause": [
  178. {
  179. "type": "validation_exception",
  180. "reason": 'Validation Failed: 1: $.default_field: does not have a value in the enumeration [title, description];',
  181. "stack_trace": ...
  182. }
  183. ],
  184. "type": "validation_exception",
  185. "reason": 'Validation Failed: 1: $.default_field: does not have a value in the enumeration [title, description];',
  186. "stack_trace": ...
  187. },
  188. "status": 400
  189. }
  190. ----
  191. // TESTRESPONSE[s/"stack_trace": \.\.\./"stack_trace": $body.$_path/]
  192. // Need to use source,JSON above instead console-result, as $ fields are replaced in console-result snippets