MST

星途 面试题库

面试题:ElasticSearch中MGet API如何使用_source过滤获取指定字段

假设在ElasticSearch中有一个索引名为'products',文档类型为'product',文档包含字段'id'、'name'、'price'、'description'。请使用MGet API,通过_source过滤,只获取一批文档中的'id'和'name'字段,给出具体的请求示例。
15.6万 热度难度
数据库ElasticSearch

知识考点

AI 面试

面试题答案

一键面试
GET /products/product/_mget
{
    "docs": [
        {
            "_id": "文档1的ID",
            "_source": ["id", "name"]
        },
        {
            "_id": "文档2的ID",
            "_source": ["id", "name"]
        }
    ]
}

将上述示例中的“文档1的ID”、“文档2的ID”替换为实际需要获取的文档ID。如果知道文档的_index_type,也可以在每个docs对象中指定_index_type 。 例如:

GET /_mget
{
    "docs": [
        {
            "_index": "products",
            "_type": "product",
            "_id": "文档1的ID",
            "_source": ["id", "name"]
        },
        {
            "_index": "products",
            "_type": "product",
            "_id": "文档2的ID",
            "_source": ["id", "name"]
        }
    ]
}