如何在OpenSearch API请求中过滤响应,仅获取hits内的_source?
OpenSearch查询仅返回hits内的_source数据解决方案
问题根源:你的请求参数分隔符使用错误,HTTP请求中多个参数需要用
&分隔,而非重复使用?,这导致filter_path参数未被正确解析,元数据因此依然被返回。正确请求格式:
GET http://localhost:9200/my_index/_search?q="product1234"&format=json&filter_path=hits.hits._source发送该请求后,返回结果会仅保留
hits.hits下的_source字段,结构如下:{ "hits" : { "hits" : [ { "_source" : { ... } }, { "_source" : { ... } } ] } }进阶优化:
- 若只想返回
_source内的特定字段,可添加_source参数指定字段,比如仅返回product_id:GET http://localhost:9200/my_index/_search?q="product1234"&format=json&filter_path=hits.hits._source&_source=product_id - 若期望直接得到
[{"_source": {...}}, ...]格式的数组,OpenSearch服务端无法直接生成该结构,需在客户端对返回结果做简单提取,比如通过代码取response.hits.hits即可得到目标数组。
- 若只想返回
内容的提问来源于stack exchange,提问作者Juki
相关产品推荐
相关产品推荐

