query-user.asciidoc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. [[security-api-query-user-query-params]]
  49. ==== {api-query-parms-title}
  50. `with_profile_uid`::
  51. (Optional, boolean) Determines whether to retrieve the <<user-profile,user profile>> `uid`,
  52. if exists, for the users. Defaults to `false`.
  53. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=from]
  54. +
  55. By default, you cannot page through more than 10,000 hits using the `from` and
  56. `size` parameters. To page through more hits, use the
  57. <<search-after,`search_after`>> parameter.
  58. `size`::
  59. (Optional, integer) The number of hits to return. Must not be negative and defaults to `10`.
  60. +
  61. By default, you cannot page through more than 10,000 hits using the `from` and
  62. `size` parameters. To page through more hits, use the
  63. <<search-after,`search_after`>> parameter.
  64. `sort`::
  65. (Optional, object) <<sort-search-results,Sort definition>>. You can sort on `username`, `roles` or `enabled`.
  66. In addition, sort can also be applied to the `_doc` field to sort by index order.
  67. `search_after`::
  68. (Optional, array) <<search-after,Search after>> definition.
  69. [[security-api-query-user-response-body]]
  70. ==== {api-response-body-title}
  71. This API returns the following top level fields:
  72. `total`::
  73. The total number of users found.
  74. `count`::
  75. The number of users returned in the response.
  76. `users`::
  77. A list of users that match the query.
  78. [[security-api-query-user-example]]
  79. ==== {api-examples-title}
  80. The following request lists all users, assuming you have the
  81. `read_security` privilege:
  82. [source,console]
  83. ----
  84. GET /_security/_query/user
  85. ----
  86. // TEST[setup:jacknich_user,sandrakn_user]
  87. A successful call returns a JSON structure that contains the information
  88. retrieved from one or more users:
  89. [source,console-result]
  90. ----
  91. {
  92. "total": 2,
  93. "count": 2,
  94. "users": [ <1>
  95. {
  96. "username": "jacknich",
  97. "roles": [
  98. "admin",
  99. "other_role1"
  100. ],
  101. "full_name": "Jack Nicholson",
  102. "email": "jacknich@example.com",
  103. "metadata": {
  104. "intelligence": 7
  105. },
  106. "enabled": true
  107. },
  108. {
  109. "username": "sandrakn",
  110. "roles": [
  111. "admin",
  112. "other_role1"
  113. ],
  114. "full_name": "Sandra Knight",
  115. "email": "sandrakn@example.com",
  116. "metadata": {
  117. "intelligence": 7
  118. },
  119. "enabled": true
  120. }
  121. ]
  122. }
  123. ----
  124. // NOTCONSOLE
  125. <1> The list of users that were retrieved for this request
  126. If you create a user with the following details:
  127. [source,console]
  128. ----
  129. POST /_security/user/jacknich
  130. {
  131. "password" : "l0ng-r4nd0m-p@ssw0rd",
  132. "roles" : [ "admin", "other_role1" ],
  133. "full_name" : "Jack Nicholson",
  134. "email" : "jacknich@example.com",
  135. "metadata" : {
  136. "intelligence" : 7
  137. }
  138. }
  139. ----
  140. A successful call returns a JSON structure:
  141. [source,console-result]
  142. ----
  143. {
  144. "created": true
  145. }
  146. ----
  147. Use the user information retrieve the user with a query:
  148. [source,console]
  149. ----
  150. GET /_security/_query/user
  151. {
  152. "query": {
  153. "prefix": {
  154. "roles": "other"
  155. }
  156. }
  157. }
  158. ----
  159. // TEST[setup:jacknich_user]
  160. A successful call returns a JSON structure for a user:
  161. [source,console-result]
  162. --------------------------------------------------
  163. {
  164. "total": 1,
  165. "count": 1,
  166. "users": [
  167. {
  168. "username": "jacknich",
  169. "roles": [
  170. "admin",
  171. "other_role1"
  172. ],
  173. "full_name": "Jack Nicholson",
  174. "email": "jacknich@example.com",
  175. "metadata": {
  176. "intelligence": 7
  177. },
  178. "enabled": true
  179. }
  180. ]
  181. }
  182. --------------------------------------------------
  183. // NOTCONSOLE
  184. To retrieve the user `profile_uid` as part of the response:
  185. [source,console]
  186. --------------------------------------------------
  187. GET /_security/_query/user?with_profile_uid=true
  188. {
  189. "query": {
  190. "prefix": {
  191. "roles": "other"
  192. }
  193. }
  194. }
  195. --------------------------------------------------
  196. // TEST[setup:jacknich_user]
  197. [source,console-result]
  198. --------------------------------------------------
  199. {
  200. "total": 1,
  201. "count": 1,
  202. "users": [
  203. {
  204. "username": "jacknich",
  205. "roles": [
  206. "admin",
  207. "other_role1"
  208. ],
  209. "full_name": "Jack Nicholson",
  210. "email": "jacknich@example.com",
  211. "metadata": {
  212. "intelligence": 7
  213. },
  214. "enabled": true,
  215. "profile_uid": "u_79HkWkwmnBH5gqFKwoxggWPjEBOur1zLPXQPEl1VBW0_0"
  216. }
  217. ]
  218. }
  219. --------------------------------------------------
  220. // NOTCONSOLE
  221. Use a `bool` query to issue complex logical conditions and use
  222. `from`, `size`, `sort` to help paginate the result:
  223. [source,js]
  224. ----
  225. GET /_security/_query/user
  226. {
  227. "query": {
  228. "bool": {
  229. "must": [
  230. {
  231. "wildcard": {
  232. "email": "*example.com" <1>
  233. }
  234. },
  235. {
  236. "term": {
  237. "enabled": true <2>
  238. }
  239. }
  240. ],
  241. "filter": [
  242. {
  243. "wildcard": {
  244. "roles": "*other*" <3>
  245. }
  246. }
  247. ]
  248. }
  249. },
  250. "from": 1, <4>
  251. "size": 2, <5>
  252. "sort": [
  253. { "username": { "order": "desc"} } <6>
  254. ]
  255. }
  256. ----
  257. // NOTCONSOLE
  258. <1> The email must end with `example.com`
  259. <2> The user must be enabled
  260. <3> The result will be filtered to only contain users with at least one role that contains the substring `other`
  261. <4> The offset to begin the search result is the 2nd (zero-based index) user
  262. <5> The page size of the response is 2 users
  263. <6> The result is sorted by `username` in descending order
  264. The response contains a list of matched users along with their sort values:
  265. [source,js]
  266. ----
  267. {
  268. "total": 5,
  269. "count": 2,
  270. "users": [
  271. {
  272. "username": "ray",
  273. "roles": [
  274. "other_role3"
  275. ],
  276. "full_name": "Ray Nicholson",
  277. "email": "rayn@example.com",
  278. "metadata": {
  279. "intelligence": 7
  280. },
  281. "enabled": true,
  282. "_sort": [
  283. "ray" <1>
  284. ]
  285. },
  286. {
  287. "username": "lorraine",
  288. "roles": [
  289. "other_role3"
  290. ],
  291. "full_name": "Lorraine Nicholson",
  292. "email": "lorraine@example.com",
  293. "metadata": {
  294. "intelligence": 7
  295. },
  296. "enabled": true,
  297. "_sort": [
  298. "lorraine"
  299. ]
  300. }
  301. ]
  302. }
  303. ----
  304. // NOTCONSOLE
  305. <1> The sort value is `username`