# How to specify role when using search API

**URL:** https://discuss.elastic.co/t/how-to-specify-role-when-using-search-api/141969
**Category:** Elasticsearch
**Created:** [July 27, 2018, 3:47pm UTC](https://discuss.elastic.co/t/how-to-specify-role-when-using-search-api/141969 "2018-07-27T15:47:14Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![nadirHafs](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@nadirHafs](https://discuss.elastic.co/u/nadirHafs)
#### Post date: [July 27, 2018, 3:47pm UTC](https://discuss.elastic.co/t/how-to-specify-role-when-using-search-api/141969/1 "2018-07-27T15:47:14Z")

</div>

Hello,

I'm using elasticSearch with a nodejs/mongodb application. I want to set up permission and role on documents. I saw that X-pack was doing what i want.

I have a problem, i try the following permission :

POST /\_xpack/security/role/dept\_role  
{  
"indices" : [  
{  
"names" : ["\*"],  
"privileges" : ["read"],  
"query" : {  
"term" : { "department\_id" : 12 }  
}  
}  
]  
}

But when i'm using the search api on elasticsearch i don't know how to specify my role? Maybe i don't understand how it's working. Can someone help me? Thanks.

EDIT : More details : in my nodejs app , when i want to search document i use the search API (GET /myIndex/\_search/ {myQuery}) When i search i want to specify my role that i defined in x-pack) . Sorry if my english is bad 😃

---

<div class="post-metadata">

### Author: ![Albert\_Zaharovits](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/albert_zaharovits/32/24390_2.png) [@Albert\_Zaharovits](https://discuss.elastic.co/u/Albert_Zaharovits)
#### Post date: [July 29, 2018, 9:16pm UTC](https://discuss.elastic.co/t/how-to-specify-role-when-using-search-api/141969/2 "2018-07-29T21:16:59Z")

</div>

Hello @nadirHafs ,

> But when i'm using the search api on elasticsearch i don't know how to specify my role?

Roles pertain to users by a process called _Role mapping_ and there is an [API](https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-role-mapping.html) to configure it. In a request you identify as a user in a realm which will then have roles assigned to it which subsequently grant the user privileges over indices, docs and fields.

---

<div class="post-metadata">

### Author: ![nadirHafs](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@nadirHafs](https://discuss.elastic.co/u/nadirHafs)
#### Post date: [July 30, 2018, 9:58am UTC](https://discuss.elastic.co/t/how-to-specify-role-when-using-search-api/141969/3 "2018-07-30T09:58:07Z")

</div>

Thanks for you reply, i didn't have the knowledege about realm 🙂 . But i still want to specify my user in that kind of query :

Passing a full request definition in the Elasticsearch’s Query DSL as a Hash.  
const response = await client.search({  
index: 'myindex',  
body: {  
query: {  
match: {  
title: 'test'  
}  
},  
facets: {  
tags: {  
terms: {  
field: 'tags'  
}  
}  
}  
}  
});

When you say "you identify as a user in a realm" , how can i do that in my javascript query? Thanks

PS : Here in SOAP UI i can add authentication (left bottom corner)

 ![authentication](https://us1.discourse-cdn.com/elastic/original/3X/d/d/dd4567f93a4ae141c85770131e60138a8a379076.png)

I want to do the same with the javascript client.

---

<div class="post-metadata">

### Author: ![Albert\_Zaharovits](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/albert_zaharovits/32/24390_2.png) [@Albert\_Zaharovits](https://discuss.elastic.co/u/Albert_Zaharovits)
#### Post date: [July 30, 2018, 11:48am UTC](https://discuss.elastic.co/t/how-to-specify-role-when-using-search-api/141969/4 "2018-07-30T11:48:58Z")

</div>

> When you say "you identify as a user in a realm" , how can i do that in my javascript query?

The short answer is that the HTTPS request needs to have attached the BASIC AUTH HTTP Header. In `curl` that will be the `-u` option.  
But you really need to cover the docs on these topics:

> **[User authentication | Elastic Stack Overview \[7.4\] | Elastic](https://www.elastic.co/guide/en/elastic-stack-overview/current/setting-up-authentication.html)**

---

<div class="post-metadata">

### Author: ![nadirHafs](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@nadirHafs](https://discuss.elastic.co/u/nadirHafs)
#### Post date: [July 30, 2018, 12:03pm UTC](https://discuss.elastic.co/t/how-to-specify-role-when-using-search-api/141969/5 "2018-07-30T12:03:22Z")

</div>

Ok thanks you very much i think its all clear : )

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [August 27, 2018, 12:03pm UTC](https://discuss.elastic.co/t/how-to-specify-role-when-using-search-api/141969/6 "2018-08-27T12:03:23Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
