Home > Blockchain >  Getting only the insides of the source from the elastic search query
Getting only the insides of the source from the elastic search query

Time:01-25

What im trying to do is to get only the source from the elastic search query in order to skip the processing on the javascript, so i could squize some more performance gains.Is it possible to do so?

So here is what i currenly do, i just get the data from the elastic search and iterate every one of those objects in order to construct an array that only contains the what's inside of _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);

So my question is, is there a way to only get the _source from the elastic search, in order to skip the processing on the JavaScript? So the object returned from the elastic search would look like this

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

so without all of the other fields like hits, took etc...

CodePudding user response:

It's not possible to change the structure of the response you get from the search call.

However, what you can do is to specify filter_path so you only get the _source content in the response and you wouldn't need to process it since you know you only have _source content

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,
  },
});
  •  Tags:  
  • Related