Mischa Arefiev
Mischa Arefiev

Reputation: 5417

How to request a single document by _id via alias?

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)?

Upvotes: 21

Views: 44554

Answers (5)

Aliaksei Litsvin
Aliaksei Litsvin

Reputation: 1261

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

curl -X GET 'localhost:9200/_search?q=id:42&pretty'

Upvotes: 3

Cong
Cong

Reputation: 188

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

GET my-index-000001/_doc/0

Upvotes: 12

Michael Ben-Nes
Michael Ben-Nes

Reputation: 7645

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"
        }
      }
    ]
  }
}

Upvotes: 4

Shams
Shams

Reputation: 3667

From Elasticsearch 5.1 the query looks like:

GET /my_alias_name/_search/
{
    "query": { 
        "bool": {
         "filter": {
                "term": {
                   "_id": "AUwNrOZsm6BwwrmnodbW"
                }
            }
        }
    }
}

Upvotes: 16

Heschoon
Heschoon

Reputation: 3019

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

Upvotes: 16

Related Questions