Using elastic search I'm trying to add a calculated distance
field to a geo search. I just want to append an extra calculated field to the search document but when I add the calculated field via "script_fields", then only that field is returned.
I tried adding a wildcard fields part, but it didn't affect the result.
How to make this query return the complete documents
with the extra calculated field added?
GET /ocsm_test/inventory/_search
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "2km",
"address.geo.location": [],
"loc.address.geo.location": [
151.2165507,
-33.8732887
]
}
}
}
},
"aggs": {
"partNumber": {
"terms": {
"field": "partNumber",
"order": {
"_term": "asc"
}
}
},
"location": {
"terms": {
"field": "loc.identifier",
"order": {
"_term": "asc"
}
}
}
},
"script_fields": {
"distance": {
"params": {
"lat": -33.8732887,
"lon": 151.2165507
},
"script": "doc['loc.address.geo.location'].distanceInKm(lat,lon)"
}
},
"fields": [
".*"
],
"post_filter": {
"bool": {
"must": [
{
"term": {
"partNumber": "p-0099393-3"
}
}
]
}
}
}
Retrieving fields is not recommended, you should use source filtering instead.
So, instead of this
"fields": [
".*"
],
Use this:
"_source": true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With