Current location - Health Preservation Learning Network - Health preserving class - Flexible search geographical location query
Flexible search geographical location query
Recently, there is a need to obtain real estate near a certain location, such as obtaining real estate information within the range of Fiona Fang 100km where the current user is located. After investigation, it is found that it can be realized by redis, mongodb and elasticsearch. After consideration, we decided to use es. Here, we briefly record the use of geo api in es.

There are two types of geographic location data in es, geo_point and geo_shape. Es does not automatically recognize this data type and needs to specify it manually when creating the map.

Geo_point uses latitude and longitude coordinates, and can calculate points falling in a rectangle, points with a certain radius (circle), points in a polygon (deprecated), sorting, aggregation and other operations.

Geo_shape represents a complex graph, which is expressed in the format of GeoJSON. For example, if we want to express the coordinate position of a library, if the library occupies a large position, it may not be accurate to express it with a point, and then we can express it with geo_shape.

However, this data type also has disadvantages: for example, it cannot be sorted (because it is a polygon point).

1, ① ② ③ ④ in the figure indicate the buildings that need to be added with es.

2. The circles, squares and polygons in the picture indicate that you need to use es to find out the position inside.

3. Short arrow in the picture? Represents a boundary.

Attention? :

1. In the index, the type of the location field we specified ourselves is geo_point.

2. The field type of 2.building_name is keyword, which means no word segmentation. This field is only used for testing and has no use.

3. There is only one type in ES 7, and the type of index is not specified.

Attention? :

1. As can be seen from the above, there are four ways to insert geographical location.

As can be seen from the above figure, the coordinates of the upper left corner and the lower right corner are (121.444075,31.265395) and (1217,31.5395) respectively.

By executing the query, you should be able to find Shanghai Railway Station and Ye Wan Yuan Jing Building.

As shown in the figure, Shanghai Railway Station and Ye Wan Yuan Jing Building have been inquired, and the results are correct.

This is a distance query, which extends from a certain point to the surrounding distance range.

In the background of the previous step, we know the coordinates of Shanghai Station (121.462311.31.256224) and how far it is from the surrounding area. Here, we ask about the 600-meter building in Fiona Fang with Shanghai Station as the center.

Attention? :

There are two arcs and planes in the values of 1 and distance_type.

2. The units available after the distance are kilometers, meters, centimeters, millimeters, nautical miles, meters, yards, feet and inches.

Attention? :

1, sort performs sorting.

Demand:

1, es query statement

As can be seen from the above picture:

1. There are only 1 buildings within 0-500m of Shanghai Railway Station.

2. There is 1 building between 500- 1000 from Shanghai Railway Station.

3. There are two stops from Shanghai Railway Station 1000.

1, query statement, query all data, and filter out all buildings within 3 kilometers of Shanghai Station.

2.aggs is used to count the number of buildings within 500m, 500- 1000m and 1000m in Shanghai.

3.sort is used for sorting.

4. Use post_filter to reduce the result to Shanghai Railway Station 1000 meters.

1, geographical point data type

2. Distance unit

Step 3 tidy up

4. Rectangular query

5, circular query, distance query

6. Coordinate pickup system