MST
星途 面试题库

面试题:ElasticSearch中如何使用GET API选择特定字段

假设在ElasticSearch中有一个索引名为`products`,文档类型为`product`,其中文档包含`name`、`price`、`description`等字段。请描述如何使用GET API只获取`name`和`price`字段。
48.2万 热度难度
数据库ElasticSearch

知识考点

AI 面试

面试题答案

一键面试

在Elasticsearch中,可以使用如下的GET请求只获取nameprice字段:

GET products/product/_search
{
    "_source": ["name", "price"],
    "query": {
        "match_all": {}
    }
}

上述请求中,_source指定了要返回的字段为namepricequery部分使用match_all查询是为了匹配索引中的所有文档。如果有特定的过滤条件,可在query部分进行修改。