我用inner_hits解决了这个问题,请参见下面的查询:
{
"query": {
"nested": {
"path": "campi",
"query": {
"query_string": {
"query": "*"
}
},
"inner_hits": {
"script_fields": {
"distanceInMeters": {
"script": {
"inline": "!doc['campi.location'].empty ? doc['campi.location'].arcDistance(params.lat, params.lon) : 0",
"lang": "painless",
"params": {
"lon": -43.9207766,
"lat": -19.910621
}
}
}
}
}
}
}
}
下面是一个响应示例:
{
"took": 42,
"timed_out": false,
"terminated_early": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 42,
"max_score": 1.0,
"hits": [
{
"_index": "search",
"_type": "baseCourse",
"_id": "1813-67-14-2309",
"_score": 1.0,
"_source": {
"cityName": "Belo Horizonte",
"institutionName": "CENTRO DE EDUCAÇÃO SUPERIOR",
"campi": [
{
"address_zipCode": null,
"address_street": "Avenida Contorno, 6.475 - São Pedro",
"stateCode": "MG",
"id": 33,
"campus_name": "Unidade SEDE"
}
],
"baseCourseName": "ADMINISTRAÇÃO"
},
"inner_hits": {
"campi": {
"hits": {
"total": 1,
"max_score": null,
"hits": [
{
"_nested": {
"field": "campi",
"offset": 0
},
"_score": null,
"fields": {
"distanceInMeters": [
3593.558492923913
]
},
"sort": [
3593.558492923913
]
}
]
}
}
}
}
]
}
}