gpt4 book ai didi

elasticsearch - 展平 Elasticsearch _源输出

转载 作者:行者123 更新时间:2023-12-03 02:26:28 27 4
gpt4 key购买 nike

我在使用Elastic Search(ES)检索JSON对象时遇到问题。现在,当我尝试使用下面的请求正文从ES查询一些数据时,

"_source": [
"data.id",
"data.completed",
"data.label",
"data.url",
"data.mobile"
],

"query": {
...
}

我总是得到以下形式的回应
{
"took": 225,
"timed_out": false,
"_shards": {
...
},
"hits": {
"total": {
...
},
"max_score": null,
"hits": [
{
...

"_source": {
"data": {
"mobile": 1,
"label": "EU Mobile Test",
"completed": 1582990420,
"id": "2002",
"url": "http://www.example.com/"
}
},

...
},


...
]
}
}

有没有一种方法可以直接通过请求正文检索JSON子级信息?这样响应应该是这样的:
 {
"took": 225,
"timed_out": false,
"_shards": {
...
},
"hits": {
"total": {
...
},
"max_score": null,
"hits": [
{
...

"_source": {
"mobile": 1,
"label": "EU Mobile Test",
"completed": 1582990420,
"id": "2002",
"url": "http://www.example.com/"
},

...
},


...
]
}
}

最佳答案

除了限制像使用_source一样返回的内容外,您无法更改hit_source属性的结构方式。

您可以使用脚本字段来破解:

GET biao/_search
{
"_source": "extracted_data",
"script_fields": {
"extracted_data": {
"script": {
"source": """
def map = [:];
for (def field : params['data_fields']) {
try {
map[field] = doc['data.' + field].value;
} catch(Exception e){
try {
map[field] = doc['data.' + field + '.keyword'][0];
} catch(Exception ee) {

}
}
}
return map;

""",
"params": {
"data_fields": [
"mobile",
"label",
"completed",
"id",
"url"
]
}
}
}
}
}

屈服
"hits" : [
{
"_index" : "biao",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : { },
"fields" : {
"extracted_data" : [
{
"mobile" : 1,
"label" : "EU Mobile Test",
"completed" : 1582990420,
"id" : "2002",
"url" : "http://www.example.com/"
}
]
}
}
]

但现在您遇到了一个新问题,因为您需要使用 fields.extracted_data[0];)访问数据。

再次阅读redash文档-必须有一种更干净的方法!

关于elasticsearch - 展平 Elasticsearch _源输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60887653/

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