query-user.asciidoc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. [role="xpack"]
  2. [[security-api-query-user]]
  3. === Query User API
  4. ++++
  5. <titleabbrev>Query User</titleabbrev>
  6. ++++
  7. Retrieves <<native-realm,native users>> with <<query-dsl,Query DSL>> in a <<paginate-search-results,paginated>> fashion.
  8. NOTE: As opposed to the <<security-api-get-user,get user api>>, <<built-in-users,built-in users>> are excluded from the
  9. result. This API is only for <<native-realm,native users>>.
  10. [[security-api-query-user-request]]
  11. ==== {api-request-title}
  12. `GET /_security/_query/user`
  13. `POST /_security/_query/user`
  14. [[security-api-query-user-prereqs]]
  15. ==== {api-prereq-title}
  16. * To use this API, you must have at least the `read_security` cluster privilege.
  17. [[security-api-query-user-desc]]
  18. ==== {api-description-title}
  19. Use this API to retrieve users managed by the
  20. <<native-realm,native realm>> in a paginated manner.
  21. You can optionally filter the results with a query.
  22. [[security-api-query-user-request-body]]
  23. ==== {api-request-body-title}
  24. You can specify the following parameters in the request body:
  25. `query`::
  26. (Optional, string) A <<query-dsl,query>> to filter which users to return.
  27. The query supports a subset of query types, including
  28. <<query-dsl-match-all-query,`match_all`>>, <<query-dsl-bool-query,`bool`>>,
  29. <<query-dsl-term-query,`term`>>, <<query-dsl-terms-query,`terms`>>,
  30. <<query-dsl-prefix-query,`prefix`>>, <<query-dsl-wildcard-query,`wildcard`>> and <<query-dsl-exists-query,`exists`>>.
  31. +
  32. You can query the following public values associated with a user.
  33. +
  34. .Valid values for `query`
  35. [%collapsible%open]
  36. ====
  37. `username`::
  38. An identifier for the user.
  39. `roles`::
  40. An array of the role names of the <<defining-roles, roles>> that are assigned to the user.
  41. `full_name`::
  42. Full name of the user.
  43. `email`::
  44. The email of the user.
  45. `enabled`::
  46. Specifies whether the user is enabled.
  47. ====
  48. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=from]
  49. +
  50. By default, you cannot page through more than 10,000 hits using the `from` and
  51. `size` parameters. To page through more hits, use the
  52. <<search-after,`search_after`>> parameter.
  53. `size`::
  54. (Optional, integer) The number of hits to return. Must not be negative and defaults to `10`.
  55. +
  56. By default, you cannot page through more than 10,000 hits using the `from` and
  57. `size` parameters. To page through more hits, use the
  58. <<search-after,`search_after`>> parameter.
  59. `sort`::
  60. (Optional, object) <<sort-search-results,Sort definition>>. You can sort on `username`, `roles` or `enabled`.
  61. In addition, sort can also be applied to the `_doc` field to sort by index order.
  62. `search_after`::
  63. (Optional, array) <<search-after,Search after>> definition.
  64. [[security-api-query-user-response-body]]
  65. ==== {api-response-body-title}
  66. This API returns the following top level fields:
  67. `total`::
  68. The total number of users found.
  69. `count`::
  70. The number of users returned in the response.
  71. `users`::
  72. A list of users that match the query.
  73. [[security-api-query-user-example]]
  74. ==== {api-examples-title}
  75. The following request lists all users, assuming you have the
  76. `read_security` privilege:
  77. [source,console]
  78. ----
  79. GET /_security/_query/user
  80. ----
  81. // TEST[setup:jacknich_user,sandrakn_user]
  82. A successful call returns a JSON structure that contains the information
  83. retrieved from one or more users:
  84. [source,console-result]
  85. ----
  86. {
  87. "total": 2,
  88. "count": 2,
  89. "users": [ <1>
  90. {
  91. "username": "jacknich",
  92. "roles": [
  93. "admin",
  94. "other_role1"
  95. ],
  96. "full_name": "Jack Nicholson",
  97. "email": "jacknich@example.com",
  98. "metadata": {
  99. "intelligence": 7
  100. },
  101. "enabled": true
  102. },
  103. {
  104. "username": "sandrakn",
  105. "roles": [
  106. "admin",
  107. "other_role1"
  108. ],
  109. "full_name": "Sandra Knight",
  110. "email": "sandrakn@example.com",
  111. "metadata": {
  112. "intelligence": 7
  113. },
  114. "enabled": true
  115. }
  116. ]
  117. }
  118. ----
  119. // NOTCONSOLE
  120. <1> The list of users that were retrieved for this request
  121. If you create a user with the following details:
  122. [source,console]
  123. ----
  124. POST /_security/user/jacknich
  125. {
  126. "password" : "l0ng-r4nd0m-p@ssw0rd",
  127. "roles" : [ "admin", "other_role1" ],
  128. "full_name" : "Jack Nicholson",
  129. "email" : "jacknich@example.com",
  130. "metadata" : {
  131. "intelligence" : 7
  132. }
  133. }
  134. ----
  135. A successful call returns a JSON structure:
  136. [source,console-result]
  137. ----
  138. {
  139. "created": true
  140. }
  141. ----
  142. Use the user information retrieve the user with a query:
  143. [source,console]
  144. ----
  145. GET /_security/_query/user
  146. {
  147. "query": {
  148. "prefix": {
  149. "roles": "other"
  150. }
  151. }
  152. }
  153. ----
  154. // TEST[setup:jacknich_user]
  155. A successful call returns a JSON structure for a user:
  156. [source,console-result]
  157. --------------------------------------------------
  158. {
  159. "total": 1,
  160. "count": 1,
  161. "users": [
  162. {
  163. "username": "jacknich",
  164. "roles": [
  165. "admin",
  166. "other_role1"
  167. ],
  168. "full_name": "Jack Nicholson",
  169. "email": "jacknich@example.com",
  170. "metadata": {
  171. "intelligence": 7
  172. },
  173. "enabled": true
  174. }
  175. ]
  176. }
  177. --------------------------------------------------
  178. // NOTCONSOLE
  179. Use a `bool` query to issue complex logical conditions and use
  180. `from`, `size`, `sort` to help paginate the result:
  181. [source,js]
  182. ----
  183. GET /_security/_query/user
  184. {
  185. "query": {
  186. "bool": {
  187. "must": [
  188. {
  189. "wildcard": {
  190. "email": "*example.com" <1>
  191. }
  192. },
  193. {
  194. "term": {
  195. "enabled": true <2>
  196. }
  197. }
  198. ],
  199. "filter": [
  200. {
  201. "wildcard": {
  202. "roles": "*other*" <3>
  203. }
  204. }
  205. ]
  206. }
  207. },
  208. "from": 1, <4>
  209. "size": 2, <5>
  210. "sort": [
  211. { "username": { "order": "desc"} } <6>
  212. ]
  213. }
  214. ----
  215. // NOTCONSOLE
  216. <1> The email must end with `example.com`
  217. <2> The user must be enabled
  218. <3> The result will be filtered to only contain users with at least one role that contains the substring `other`
  219. <4> The offset to begin the search result is the 2nd (zero-based index) user
  220. <5> The page size of the response is 2 users
  221. <6> The result is sorted by `username` in descending order
  222. The response contains a list of matched users along with their sort values:
  223. [source,js]
  224. ----
  225. {
  226. "total": 5,
  227. "count": 2,
  228. "users": [
  229. {
  230. "username": "ray",
  231. "roles": [
  232. "other_role3"
  233. ],
  234. "full_name": "Ray Nicholson",
  235. "email": "rayn@example.com",
  236. "metadata": {
  237. "intelligence": 7
  238. },
  239. "enabled": true,
  240. "_sort": [
  241. "ray" <1>
  242. ]
  243. },
  244. {
  245. "username": "lorraine",
  246. "roles": [
  247. "other_role3"
  248. ],
  249. "full_name": "Lorraine Nicholson",
  250. "email": "lorraine@example.com",
  251. "metadata": {
  252. "intelligence": 7
  253. },
  254. "enabled": true,
  255. "_sort": [
  256. "lorraine"
  257. ]
  258. }
  259. ]
  260. }
  261. ----
  262. // NOTCONSOLE
  263. <1> The sort value is `username`