Since it is a substitute for a memo, the explanation of the background etc. will be sloppy.
When doing a search with Elasticsearch 5.1.1, please also calculate and return the distance between the geo_point
type field and the point specified in the query. I knew how to do it in 2 series & Groovy, but I need to fix it because it became 5 series & Painless
Groovy has been deprecated since Elasticsearch 5.0.0, and a newly developed language called Painless has become the default. So you need to change it to painless first. Also, since the various helper methods that were available in 2.0 have been deprecated, you need to write alternative processing.
Since I used it with a python client, the example is a Python dict type, but I think that the contents of inline are common, so use it as it is with the REST API or when accessing from a client of another language, mainly inline I feel that I only need to look at the contents of params.
location_field
is a field of type geo_distance
, and latitude_value
and longitude_value
are variables that contain latitude and longitude values, respectively.
Before
{
"distance": {
"script": {
"lang": "painless",
"inline": "doc[\"location_field\"].distanceInKm(lat, lon)",
"params": {
"lat": latitude_value
"lon": longitude_value
}
},
}
}
After
{
"distance": {
"script": {
"lang": "painless",
"inline": "doc[\"location_field\"].arcDistance(params.lat, params.lon) * 0.001",
"params": {
"lat": latitude_value,
"lon": longitude_value
}
},
}
}
Since distanceInKm
is obsolete, use ʻarcDistance`.
However, in this case, the unit is rice, so it is converted to km by multiplying by 0.001.
Another point is that the variables defined in params
could be used with the names defined before, but for some reason (in painless?), It was necessary to add params.
to the head.
I haven't used Groovy in the first place, but the basic grammar is almost the same, and I don't think I'll do much complicated things, so if you read the Document, it's quite so. However, I thought it would be rather difficult to rewrite the abolished helper method for 5 series ... (I can't understand the processing contents of the helper method even if I read the Elasticsearch document, and which helper method is painless I don't know if it can be used)
The documentation also describes how to debug, but I didn't do it because it's a hassle to read to write just one line of processing (** As a result, it takes almost an hour to fix this one line of inline). It took**)
It has become a complaining entry, but there is a high possibility that I am bad just by reading the document properly, so I will read it again when I get rid of the tension at midnight.
Recommended Posts