[PYTHON] Write a script to calculate the distance with Elasticsearch 5 system painless

Since it is a substitute for a memo, the explanation of the background etc. will be sloppy.

Thing you want to do

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

why?

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.

Fixes

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
            }
        },
    }
}

the difference

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.

Impressions

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

Write a script to calculate the distance with Elasticsearch 5 system painless
Write a python program to find the editing distance [python] [Levenshtein distance]
Write a batch script with Python3.5 ~
Create a star system with Blender 2.80 script
Creating a shell script to write a diary
Think about how to write a filter with the Shotgun API-Contact Versions
[Introduction to Python] How to write a character string with the format function
How to calculate the volatility of a brand
Save the object to a file with pickle
Write a script to convert a MySQL dump to TSV
I want to write to a file with Python
Try to write a ping confirmation script appropriately
Feel free to write a test with nose (in the case of + gevent)
Calculate the optimal solution to set a world record for decathlon with scipy.optimize
How to write a GUI using the maya command
How to create a submenu with the [Blender] plugin
Steps to calculate the likelihood of a normal distribution
[Python] A memo to write CSV vertically with Pandas
A program to write Lattice Hinge with Rhinoceros with Python
Transit to the update screen with the Django a tag
Calculate the product of matrices with a character expression?
A script that makes it easy to create rich menus with the LINE Messaging API
Recursively get the Excel list in a specific folder with python and write it to Excel.
I replaced the Windows PowerShell cookbook with a python script.
[Linux] Copy data from Linux to Windows with a shell script
Finding a solution to the N-Queen problem with a genetic algorithm (2)
Probably the easiest way to create a pdf with Python3
Write a program to solve the 4x4x4 Rubik's Cube! 1. Overview
The first step to creating a serverless application with Zappa
Process the files in the folder in order with a shell script
I'll never forget how to write a shell script, don't forget! !!
To write a test in Go, first design the interface
A story about how to deal with the CORS problem
I wrote a script to combine the divided ts files
The usual way to add a Kernel with Jupyter Notebook
[Python] The first step to making a game with Pyxel
[Linux] Write a deployment tool using rsync with a shell script
Finding a solution to the N-Queen problem with a genetic algorithm (1)
Write to csv with Python
Write a script in Shell and Python to notify you in Slack when the process is finished
Write processing time measurement a little easier using the with clause
Process the contents of the file in order with a shell script
[Blender] Script to check if the selected one is a mesh
Let's write a program to solve the 4x4x4 Rubik's Cube! 2. Algorithm
Let's write a program to solve the 4x4x4 Rubik's Cube! 3. Implementation
[Introduction to Python] How to split a character string with the split function
Conditional branch due to the existence of a shell script file
Create a shell script to run the python file multiple times
How to make a command to read the configuration file with pyramid
I wrote a script to get you started with AtCoder fast!
I want to do a full text search with elasticsearch + python
I want to create a system to prevent forgetting to tighten the key 1
Try to write a program that abuses the program and sends 100 emails
I wanted to solve the ABC164 A ~ D problem with Python
A script that returns 0, 1 attached to the first Python prime number
How to write a docstring to create a named tuple document with sphinx
Try using Elasticsearch as the foundation of a question answering system
[AWS / Tello] Build a system to operate the drone on the cloud
How to send a request to the DMM (FANZA) API with python
Create a REST API to operate dynamodb with the Django REST Framework
How to calculate the autocorrelation coefficient