query-user.asciidoc 8.5 KB

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