gpt4 book ai didi

javascript - 如何从MSearch返回数组匹配

转载 作者:行者123 更新时间:2023-12-02 22:53:37 26 4
gpt4 key购买 nike

我有一个Elasticsearch集群,我正在针对多个索引(msearch)运行搜索,该搜索返回一个对象数组(每个要查询的索引一个对象)。这些对象中的每一个在hits对象内部都有一个hits数组。我真的只是在_source对象之后。您将如何获取所有嵌套的“_source”对象的数组?

[
{
"hits": {
"hits": [
{
"_index": "index1",
"_type": "type1",
"_id": "1",
"_score": 12.163426,
"_source": {
"somekey": "some value",
"someotherkey": "another value"
}
}
]
},
},
{
"hits": {
"hits": []
},
},
{
"hits": {
"hits": [
{
"_index": "index2",
"_type": "type2",
"_id": "2",
"_score": 7.0380797,
"_source": {
"somekey": "some value",
"someotherkey": "another value"
}
},
{
"_index": "index2",
"_type": "type2",
"_id": "3",
"_score": 6.07253,
"_source": {
"somekey": "some value 2",
"someotherkey": "another value 2"
}
}
]
},
},
]

最佳答案

您可以使用 Array.prototype.flatMap() 遍历外部数组,并使用 Array.prototype.map() hits.hits转换为_source属性数组作为项:

const src = [{"hits":{"hits":[{"_index":"index1","_type":"type1","_id":"1","_score":12.163426,"_source":{"somekey":"some value","someotherkey":"another value"}}]},},{"hits":{"hits":[]},},{"hits":{"hits":[{"_index":"index2","_type":"type2","_id":"2","_score":7.0380797,"_source":{"somekey":"some value1","someotherkey":"another value1"}},{"_index":"index2","_type":"type2","_id":"3","_score":6.07253,"_source":{"somekey":"some value 2","someotherkey":"another value 2"}}]},},],

result = src.flatMap(o => o.hits.hits.map(({_source}) => _source))

console.log(result)
.as-console-wrapper{min-height:100%;}

关于javascript - 如何从MSearch返回数组匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63181609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com