# GET \_doc request not working as expected with routing aliases

**URL:** <https://discuss.elastic.co/t/get-doc-request-not-working-as-expected-with-routing-aliases/285039>\
**Category:** Elasticsearch\
**Created:** [September 24, 2021, 12:03am UTC](https://discuss.elastic.co/t/get-doc-request-not-working-as-expected-with-routing-aliases/285039 "2021-09-24T00:03:27Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vineethsuhas](https://avatars.discourse-cdn.com/v4/letter/v/ac8455/32.png) [@vineethsuhas](https://discuss.elastic.co/u/vineethsuhas)\
**Post date:** [September 24, 2021, 12:03am UTC](https://discuss.elastic.co/t/get-doc-request-not-working-as-expected-with-routing-aliases/285039/1 "2021-09-24T00:03:27Z")

</div>

Hi,

Here is the scenario:

```auto
1. We created an index (routing_test) => (with 2 Shards)
2. We created a couple of aliases on that index (routing_test_100 and routing_test_200) using:
PUT routing_test/_alias/routing_test_100
{
  "routing": "100",
  "filter" : {
    "term" : {
      "data.Id" : "100"
    }
  }
}
3. After creating the aliases, both the aliases are pointing to the same shard (found using _search_shards) query.
4. I indexed a document with ID 123 and data.Id as 100. And here are the scenarios I'm observing:

```

> - GET routing\_test/\_doc/123 =\> No results (Expected: Should return a document)
> - GET routing\_test\_200/\_doc/123 =\> Found the doc (Expected: Should not return a document)
> - GET routing\_test\_100/\_doc/123 =\> Found the doc (Expected: Should return a document)

I am expecting that I should be able to get the document in the original index irrespective of any condition. Also, It should not return a document when I'm querying a different alias. Am I missing something or doing something wrong here?

Can you please help me identify the issue here?

Thank you!

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [September 25, 2021, 6:28am UTC](https://discuss.elastic.co/t/get-doc-request-not-working-as-expected-with-routing-aliases/285039/2 "2021-09-25T06:28:06Z")

</div>

If you do not provide a routing key when you GET the focument from the index the I'd will be hashed and the shard determined by this. It is therefore possible that the GET request will not find the document if the routing value resulted in a different shard when it was indexed.

In order to reliably get the document from the index without using the correct routing value you would need to run a search, which naturally is less efficient than a GET and require the document to be searchable.

As the aliases result in the same underlying shard they will always return the same documents. There is no filtering based on used routing value taking place, just data localisation.

What you are seeing is therefore expected.

What is the problem you are trying to solve using these features?

---

<div class="post-metadata">

**Author:** ![Nirav\_Vira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nirav_vira/32/95063_2.png) [@Nirav\_Vira](https://discuss.elastic.co/u/Nirav_Vira)\
**Post date:** [September 25, 2021, 10:45pm UTC](https://discuss.elastic.co/t/get-doc-request-not-working-as-expected-with-routing-aliases/285039/3 "2021-09-25T22:45:07Z")

</div>

Is there a syntax to GET document by Id (\_doc) with routing key? That is what we are trying to attempt by using the alias.

Re: business case We are trying to partition data (read) within an index and apply access control (aka limiting data visibility) without having to create separate indexes which adds a lot of maintenance).

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [September 26, 2021, 3:55am UTC](https://discuss.elastic.co/t/get-doc-request-not-working-as-expected-with-routing-aliases/285039/4 "2021-09-26T03:55:28Z")

</div>

Yes, [you can provide a routing key to a GET request](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/docs-get.html#get-routing). Using this for access control will however not work as it does not include any filter and just co-locates related data. If you have 2 shards an average of half the data will be visible for any routing value irrespective of how many routing values you have in use. You will need to add filtering based on some field in the documents to restrict access.

The best way to control access within an index, e.g. in a multi-tenant environment, is [document level security](https://www.elastic.co/guide/en/elasticsearch/reference/7.14/document-level-security.html), which is a commercial feature.

---

<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:** [October 24, 2021, 3:55am UTC](https://discuss.elastic.co/t/get-doc-request-not-working-as-expected-with-routing-aliases/285039/5 "2021-10-24T03:55:31Z")

</div>

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