This article is a mirror article of machine translation, please click here to jump to the original article.

View: 15452|Reply: 0

[Source] Java Geolocation Information in ElasticSearch (geo_point)

[Copy link]
Posted on 1/9/2019 5:25:39 PM | | |
Brief introduction

We know that ES supports a wide variety of data types, in addition to our common basic data types, it also supports data types that record location information. In ES, there are two types of data that record geolocation information, namely geo_shap and geo_point, and I will briefly introduce geo_point types below
geo_point supports multiple data transfer methods:

string
Position: lat + "," + lon

array
Position: {"lat": ...,"lon": ...}

Object
Location: [lon, lat]


Note: Probably everyone has stepped on this pit at least once == Geographic coordinate points are latitude first and longitude last ("latitude, longitude") when expressed as strings, while arrays are represented in the opposite form, with longitude first and latitude last ([longitude, latitude]). In fact, within Elasticesearch, whether it is in the form of a string or an array, latitude comes first and longitude comes last. However, in the early days, in order to adapt to the formatting specification of GeoJSON, the representation of array forms was adjusted. This is explained on the official website.
There are generally the following query requirements for geo_point types

distance query
Find points within range from the center point

distance range query (deprecated)
Querying points within the specified range of the center point is removed from the API in the new version of ES, so I won't explain it too much here
bounding query
Find points within a rectangle consisting of a specified point

polygon query
Find points in a polygon made up of multiple points

The SearchSourceBuilder factory constructed for different queries in Java is as follows:


service as follows:

The controller layer code is as follows:

[1]. How to use

Before we use geo_point types, we should first explicitly specify the field as the geo_point type via mapping when creating the index, as follows:


Above we have created a new index named restraunt, and in its mapping file, we explicitly specify the type of "location" field as "geo_point"

[2] Prepare some data

On Kibana we have prepared the following data:


We have prepared 7 pieces of data above, and then we need to execute the query to see if our interface meets our needs.

[3] Inquiry

Query distance


The other two inquiries are the same as above.




Previous:ElasticsearchParseException[field must be either [lat], [lon] or [geohash]]
Next:Discuz installs the app and says "Data is not recognized, please return" workaround
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com