How to request a single document by _id via alias?

后端 未结 4 1435
失恋的感觉
失恋的感觉 2021-01-11 20:36

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

相关标签:
4条回答
  • 2021-01-11 21:14

    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"
            }
          }
        ]
      }
    }
    
    0 讨论(0)
  • 2021-01-11 21:16

    From Elasticsearch 5.1 the query looks like:

    GET /my_alias_name/_search/
    {
        "query": { 
            "bool": {
             "filter": {
                    "term": {
                       "_id": "AUwNrOZsm6BwwrmnodbW"
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-11 21:18

    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
    
    0 讨论(0)
  • 2021-01-11 21:18

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

    curl -X GET 'localhost:9200/_search?q=id:42&pretty'
    
    0 讨论(0)
提交回复
热议问题