20

Is it possible to request a single document by its id by querying an alias, provided that all keys across all indices in the alias are unique (it is an external guarantee)?

Luc E
  • 1,204
  • 8
  • 16
Mischa Arefiev
  • 5,227
  • 4
  • 26
  • 34

5 Answers5

16

Yes, querying an alias spanning over multiple indices work the same way as querying one indice.

Just do this query over the alias:

POST my_alias_name/_search
{
    "filter":{
        "term":{"_id": "AUwNrOZsm6BwwrmnodbW"}
    }
}

EDIT: GET operations are not real searches and can't be done on aliases spanning over multiple indexes. So the following query is in fact no permitted:

GET my_alias_name/my_type/AUwNrOZsm6BwwrmnodbW
Heschoon
  • 2,915
  • 9
  • 26
  • 55
16

From Elasticsearch 5.1 the query looks like:

GET /my_alias_name/_search/
{
    "query": { 
        "bool": {
         "filter": {
                "term": {
                   "_id": "AUwNrOZsm6BwwrmnodbW"
                }
            }
        }
    }
}
Shams
  • 3,637
  • 5
  • 31
  • 49
8

Following Elasticsearch 8.2 Doc, you can retrieve a single document by using GET API:

GET my-index-000001/_doc/0
Cong
  • 131
  • 1
  • 5
  • This does not work if you have an alias with multiple indexes. You get the error above "has more than one index associated with it". – bschulz Aug 15 '23 at 22:01
3

7.2 version Docs suggest:

GET /_search
{
    "query": {
        "ids" : {
            "values" : ["1", "4", "100"]
        }
    }
}

Response should look like:

{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 2,
    "successful": 2,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 1,
      "relation": "eq"
    },
    "max_score": 1.0,
    "hits": [
      {
        "_index": "indexwhatever",
        "_type": "_doc",
        "_id": "anyID",
        "_score": 1.0,
        "_source": {
          "field1": "value1",
          "field2": "value2"
        }
      }
    ]
  }
}
michaelbn
  • 7,393
  • 3
  • 33
  • 46
2

In case you want to find a document with some internal id with curl:

curl -X GET 'localhost:9200/_search?q=id:42&pretty'
Aliaksei
  • 1,094
  • 11
  • 20