从弹性搜索查询中仅获取源的内部

Getting only the insides of the source from the elastic search query

我想做的是仅从弹性搜索查询中获取源,以便跳过 javascript 上的处理,这样我就可以压缩更多的性能 gains.Is这样做吗?

所以这就是我目前所做的,我只是从弹性搜索中获取数据并迭代这些对象中的每一个,以便构建一个只包含 _source 内部内容的数组:

const { body } = await this.elsticSearchService.search<any>({
 index: this.configService.get('ELASTICSEARCH_INDEX'),
 body: {
 query: {
    match_all: {},
    },
    size: 10000,
  },
});

const hits = body.hits.hits;

return hits.map((item: any) => item._source);

所以我的问题是,有没有办法只从弹性搜索中获取 _source,以便 跳过 JavaScript 上的处理? 所以弹性搜索返回的对象看起来像这样

[
0:{ "key": "value"}, // key,value from _source object
1:{ "key": "value"}, // key,value from _source object
2:{ "key": "value"}, // key,value from _source object
]

所以没有所有其他字段,如点击次数、拍摄次数等...

无法更改您从搜索调用中获得的响应的结构。

但是,您可以做的是 specify filter_path,因此您只会在响应中获得 _source 内容,并且不需要处理它,因为您知道您只有 _source 内容

const { body } = await this.elsticSearchService.search<any>({
 index: this.configService.get('ELASTICSEARCH_INDEX'),
 filter_path: '**._source',         <--- add this line
 body: {
 query: {
    match_all: {},
    },
    size: 10000,
  },
});