# Storing and retrieving the index name

**URL:** <https://discuss.elastic.co/t/storing-and-retrieving-the-index-name/32684>\
**Category:** Elasticsearch\
**Created:** [October 21, 2015, 3:14pm UTC](https://discuss.elastic.co/t/storing-and-retrieving-the-index-name/32684 "2015-10-21T15:14:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pezzell](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@pezzell](https://discuss.elastic.co/u/pezzell)\
**Post date:** [October 21, 2015, 3:14pm UTC](https://discuss.elastic.co/t/storing-and-retrieving-the-index-name/32684/1 "2015-10-21T15:14:12Z")

</div>

Is there a way to store the index name in the documents?

I have created an alias to search across three indexes. And, in some cases I would like to specify the index without changing the main api call.

I have updated the mappings for the index using:  
{  
"tweet" : {  
"\_index" : { "enabled" : true }  
}  
}

When I run the following query, nothing is returned. Is the index name not stored in the document?  
{"filter":{"terms":{"\_index":["tweet"]}},"query":{"match\_all":{}}}

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [October 21, 2015, 3:22pm UTC](https://discuss.elastic.co/t/storing-and-retrieving-the-index-name/32684/2 "2015-10-21T15:22:44Z")

</div>

The index name should be stored with the documents, however according to this [open issue](https://github.com/elastic/elasticsearch/issues/12017) it is not usable in terms queries (amongst other) yet. As a workaround you could try the [Indices Query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-indices-query.html) with only one index.

---

<div class="post-metadata">

**Author:** ![pezzell](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@pezzell](https://discuss.elastic.co/u/pezzell)\
**Post date:** [October 21, 2015, 3:36pm UTC](https://discuss.elastic.co/t/storing-and-retrieving-the-index-name/32684/3 "2015-10-21T15:36:44Z")

</div>

Thank you Christoph. When I tried the query against my alias, it says that the alias "has more than one indices associated with it" and it "can't execute a single index op"?

{  
"indices" : {  
"indices" : ["tweets"]  
}  
}

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [October 21, 2015, 4:02pm UTC](https://discuss.elastic.co/t/storing-and-retrieving-the-index-name/32684/4 "2015-10-21T16:02:07Z")

</div>

Sounds like you're trying to index into an alias, that doesn't work. If you have an alias `test_indices` for three indices `i1`, `i2` and `i3` then the following query only returns docs from one index:

```auto
GET /test_indices/_search
{
  "query": {
    "indices": {
      "indices" : ["i1"],
      "query": {
        "match_all": {}
      }, 
      "no_match_query" : "none"
    }
  }
}
...
"hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "i1",
            "_type": "test",
            "_id": "1",
            "_score": 1,
            "_source": {
               "title": "index1"
            }
         }
      ]
   }

```

---

<div class="post-metadata">

**Author:** ![pezzell](https://avatars.discourse-cdn.com/v4/letter/p/4af34b/32.png) [@pezzell](https://discuss.elastic.co/u/pezzell)\
**Post date:** [October 21, 2015, 4:07pm UTC](https://discuss.elastic.co/t/storing-and-retrieving-the-index-name/32684/5 "2015-10-21T16:07:23Z")

</div>

That was the issue. Thank you so much!

---

<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:** [July 5, 2017, 11:43pm UTC](https://discuss.elastic.co/t/storing-and-retrieving-the-index-name/32684/6 "2017-07-05T23:43:29Z")

</div>


