如何从Elasticsearch返回源字段



我使用的是Elasticsearch V7。

我只想从Elasticsearch中获取源字段。

请求查询:

GET /test/_search?filter_path=hits.hits._source
{
"query": {
"term": {
"test": {
"value": "123"
}
}
}
}

响应:

{
"hits" : {
"hits" : [
{
"_source" : {
"field1" : "value1",
"field2" : "value2",
"field3" : "value3",
}
}
]
}
}

预期结果:

{
"hits" : {
"hits" : [
{
"field1" : "value1",
"field2" : "value2",
"field3" : "value3",
}, 
{
"field1" : "value1",
"field2" : "value2",
"field3" : "value3",
}
]
}
}

"hits" : [
{
"field1" : "value1",
"field2" : "value2",
"field3" : "value3",
}, 
{
"field1" : "value1",
"field2" : "value2",
"field3" : "value3",
}
]

有没有办法从Elasticsearch中获取上述预期结果?

不,这是不可能的。filter_path在本机响应过滤方面做得非常好。

话虽如此,您可以使用命令行JSON处理器jq来进一步提取_sources

curl -XGET "http://localhost:9200/test/_search?filter_path=hits.hits._source" 
-H 'Content-Type: application/json' 
-d'{ 
"query": {   
"match_all": {} 
}
}' 
| jq '{hits: [.hits.hits[]._source]}'

最新更新