# Assigning a certain field value to the user using elasticsearch queries

**URL:** <https://discuss.elastic.co/t/assigning-a-certain-field-value-to-the-user-using-elasticsearch-queries/188937>\
**Category:** Elasticsearch\
**Created:** [July 4, 2019, 3:08pm UTC](https://discuss.elastic.co/t/assigning-a-certain-field-value-to-the-user-using-elasticsearch-queries/188937 "2019-07-04T15:08:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![EZprogramming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ezprogramming/32/57291_2.png) [@EZprogramming](https://discuss.elastic.co/u/EZprogramming)\
**Post date:** [July 4, 2019, 3:08pm UTC](https://discuss.elastic.co/t/assigning-a-certain-field-value-to-the-user-using-elasticsearch-queries/188937/1 "2019-07-04T15:08:03Z")

</div>

Given an index, and its field. Is it possible to restrict a user to a specific value in a field using Elasticsearch queries in the Kiabana dev tools?

For example,

I have the following field called `cloudwatch_logs.log_group`. Also, there are these values that correspond to this field.

/aws/lambda/b2\_c\_api  
/aws/lambda/b2\_s3\_writer  
/aws/lambda/b2\_record\_processor  
/aws/lambda/b2\_raw\_processor  
/aws/lambda/fast\_access\_processor

I want to restrict the user to view only the Kibana dashboards that are associated with `cloudwatch_logs.log_group: /aws/lambda/b2_c_api` . Is that possible?

[Edit]:

### Available fields in the index

- 
###### Popular
- @timestampadd

- **t** cloudwatch\_logs.log\_groupadd

- **t** messageadd

- **t** @versionadd

- **t** \_idadd

- **t** \_indexadd

- **#** \_scoreadd

- **t** \_typeadd

- **t** cloudwatch\_logs.event\_idadd

- cloudwatch\_logs.ingestion\_timeadd

- **t** cloudwatch\_logs.log\_streamadd

---

<div class="post-metadata">

**Author:** ![whatgeorgemade](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/whatgeorgemade/32/103246_2.png) [@whatgeorgemade](https://discuss.elastic.co/u/whatgeorgemade)\
**Post date:** [July 4, 2019, 3:43pm UTC](https://discuss.elastic.co/t/assigning-a-certain-field-value-to-the-user-using-elasticsearch-queries/188937/2 "2019-07-04T15:43:00Z")

</div>

You can't restrict access based on the value of a field; only the field as a whole.

To restrict access to documents with a certain field value, try creating an [alias with a filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html#filtered), then give certain users access to that alias instead of the whole index.

I don't know how you'd restrict the user to certain dashboards, though.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![EZprogramming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ezprogramming/32/57291_2.png) [@EZprogramming](https://discuss.elastic.co/u/EZprogramming)\
**Post date:** [July 4, 2019, 9:45pm UTC](https://discuss.elastic.co/t/assigning-a-certain-field-value-to-the-user-using-elasticsearch-queries/188937/3 "2019-07-04T21:45:21Z")

</div>

Thanks for your suggestion.

> [@whatgeorgemade](#):
>
> I don't know how you'd restrict the user to certain dashboards, though.

To restrict the user to a dashboard, you can assign `kibana_dashboard_only_user` to the user plus a custom role with read privileges for a specific index. Once the user logs in, they have access only to the dashboard section in the left hand menu in Kibana. However, they can still see all the dashboard names in the dashboard section, but they can view the content of the dashboard if the visualizations are from the index that was originally assigned to this user. I was just wondering if there is a way to only show the dashboard that the user has access to, and not the other dashboard names that the user cannot view. I guess I'll just open another question on this.

> [@whatgeorgemade](#):
>
> To restrict access to documents with a certain field value, try creating an [alias with a filter](https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html#filtered)

Could you please confirm if I am doing this right?

**Creating the alias:**

```
POST /_aliases
{
  "actions":[
    {
      "add" : {
        "index": "cloudwatch",
        "alias": "alias1",
        "filter": { "term": {"message" : "error"}}
      }
    }
  ]
}

```

**Create the role:**

```
PUT /_security/role/test
{ 
  "cluster" : [],
  "indices" : [
    {
      "names": "cloudwatch*",
      "privileges": ["all"],
      "field_security" : {
        "grant" : ["*"]
      },
      "query": {
        "term": {
          "message" : {
            "value": "error"
          }
        }
      }
    }
  ],
  "applications" : [
    {
      "application" : "kibana-.kibana",
      "privileges" : [
        "all"
      ],
      "resources" : [
        "*"
      ]
    }
  ],
  "run_as" : []  
}

```

Output:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/9/59774c6679a2409b5298fb28b46a45ddab1c86f0.png)

[Edit]

I logged in with the user kz, if I restrict the field to message, I can then see the value as error. However, restricting the value to a json array doesn't work.

The following elasticsearch query should restrict the field, cloudwatch\_logs.log\_group to the value  
"/aws/lambda/b2\_record\_processor", but it doesn't do the job.

```
PUT /_security/role/test
{ 
  "cluster" : [],
  "indices" : [
    {
      "names": "cloudwatch*",
      "privileges": ["all"],
      "field_security" : {
        "grant" : ["*"]
      },
      "query": {
        "term": {
          "cloudwatch_logs.log_group" : {
            "value": "/aws/lambda/b2_record_processor"
          }
        }
      }
    }
  ],
  "applications" : [
    {
      "application" : "kibana-.kibana",
      "privileges" : [
        "all"
      ],
      "resources" : [
        "*"
      ]
    }
  ],
  "run_as" : []  
}

```

Output:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/5/a/5a53e24a0dd7479daf7b73fa673e8d36638375fb.png)

Ignore the 'Expand your time range', the output should work, but it doesn't! Any idea why? It must be the elasticsearch query, not sure if that is the right way to handle json array.

---

<div class="post-metadata">

**Author:** ![martinr\_ubi](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@martinr\_ubi](https://discuss.elastic.co/u/martinr_ubi)\
**Post date:** [July 5, 2019, 1:57am UTC](https://discuss.elastic.co/t/assigning-a-certain-field-value-to-the-user-using-elasticsearch-queries/188937/4 "2019-07-05T01:57:38Z")

</div>

You don’t need alias filter for this just document security. I don’t currently see why tou can’t make it work with just that as your use case is a 1 to 1 match with document security, I even remember the doc gives an example which is exactly your usecase: filter on value of a field.

If you have a platinum license why aren’t you opening a support ticket with your engineer instead of relying on the forums? They would tell you the best practice for filtering on multiple values for the same role I guess.  
It would be either by adjusting the query or giving multiple roles to the user maybe?

Plus you say it doesn’t work but it should work if configured correctly, thats why that feature exists 😉

---

<div class="post-metadata">

**Author:** ![EZprogramming](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ezprogramming/32/57291_2.png) [@EZprogramming](https://discuss.elastic.co/u/EZprogramming)\
**Post date:** [July 5, 2019, 4:15pm UTC](https://discuss.elastic.co/t/assigning-a-certain-field-value-to-the-user-using-elasticsearch-queries/188937/5 "2019-07-05T16:15:46Z")

</div>

@martinr_ubi, I made it work after few hours of trying.

Could you please explain when an alias filter would be useful? I am trying to understand when to use it.

**Solution:**

```
PUT /_security/role/test
{ 
  "cluster" : [],
  "indices" : [
    {
      "names": "cloudwatch*",
      "privileges": ["read"],
      "field_security" : {
        "grant" : ["*"]
      },
      "query":{
        "bool":{
          "must":[
            {
              "match_phrase":{
                "cloudwatch_logs.log_group": "/aws/lambda/s_data_worker"
              }
            },
            {
             "match":{
                "message": "error"
              }
            }
          ]
        }
      }
    }
  ],
  "applications" : [
    {
      "application" : "kibana-.kibana",
      "privileges" : [
        "read"
      ],
      "resources" : [
        "*"
      ]
    }
  ],
  "run_as" : []  
}
```

---

<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 2, 2019, 4:15pm UTC](https://discuss.elastic.co/t/assigning-a-certain-field-value-to-the-user-using-elasticsearch-queries/188937/6 "2019-08-02T16:15:47Z")

</div>

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