MST
星途 面试题库

面试题:ElasticSearch中MGet API下_source过滤的基本用法

在ElasticSearch的MGet API中,如何使用_source过滤来仅获取文档中的特定字段?请举例说明,假设索引名为'test_index',文档类型为'doc',要获取字段'field1'和'field2'。
15.7万 热度难度
数据库ElasticSearch

知识考点

AI 面试

面试题答案

一键面试

在ElasticSearch的MGet API中,可以通过在请求体的_source字段中指定要获取的特定字段来实现。示例如下:

POST /test_index/doc/_mget
{
  "docs": [
    {
      "_id": "doc_id_1",
      "_source": ["field1", "field2"]
    },
    {
      "_id": "doc_id_2",
      "_source": ["field1", "field2"]
    }
  ]
}

在上述示例中,对test_index索引下doc类型的文档,通过_mget API获取doc_id_1doc_id_2两个文档的field1field2字段。将实际的文档ID替换为doc_id_1doc_id_2即可。