es of geographic inquiry

In order to facilitate learning ES location query, here prepared some geographical coordinates of the test data, each data contains the city name and geographical coordinates of the two fields. First, save the following to geo.json file:

{"index":{"_index":"geo","_id":"1"}}

{"city":"北京","localtion":"40.019559,116.312282"}

{"index":{"_index":"geo","_id":"2"}}

{"city":"乌鲁木齐","localtion":"43.863737,87.53891"}

{"index":{"_index":"geo","_id":"3"}}

{"city":"西安","localtion":"34.43376,108.879774"}

{"index":{"_index":"geo","_id":"4"}}

{"city":"郑州","localtion":"34.76845,113.589482"}

{"index":{"_index":"geo","_id":"5"}}

{"city":"杭州","localtion":"30.345351,120.102125"}

{"index":{"_index":"geo","_id":"6"}}

{"city":"济南","localtion":"36.688506,117.158558"}

{"index":{"_index":"geo","_id":"7"}}

{"city":"上海","localtion":"31.298035,121.426731"}

{"index":{"_index":"geo","_id":"8"}}

{"city":"武汉","localtion":"30.632158,114.28858"}

{"index":{"_index":"geo","_id":"9"}}

{"city":"广州","localtion":"23.150725,113.221536"}

 

Then create an index:

 

PUT geo

{

"settings": {

"number_of_shards": "1",

"number_of_replicas": "0"

},

"mappings": {

"properties": {

"city": {

"type": "keyword"

},

"location": {

"type": "geo_point"

}

}

}

}

Then execute:

 

$curl -X POST "localhost:9200/_bulk?pretty" -H 'Content-Type: application/json' --data-binary @geo.json

1

1. radius query (geo_distance query)

geo_distance query can find the geographic center point of the document within a specified radius. Such as a query within 500km from the city of Tianjin, the search results will return to Beijing, Jinan, the command is as follows:

 

{

"query":{

"bool":{

"must":{

"match_all":{}

},

"filter":{

"geo_distance":{

"distance":"500km",

"location":{

"lat":"38.993443",

"lon":"117.158558"

}

}

}

}

}

}

Sort by distance from Tianjin:

 

{

"query": {

"bool": {

"must": {

"match_all": {}

},

"filter": {

"geo_distance": {

"distance": "500km",

"location": {

"lat": "38.993443",

"lon": "117.158558"

}

}

}

}

},

"sort": [

{

"_geo_distance": {

"location": {

"lat": "38.993443",

"lon": "117.158558"

},

"unit": "km"

}

}

]

}

2. Specify the query within a rectangle (geo_bounding_box query)

geo_bounding_box query configured to query the geographic coordinates fall within the rectangle specified. Query a rectangle defined by two points, as shown in Yinchuan and Nanchang points made on both vertical (longitude), and parallel lines (dimension), respectively, the line of intersection will form a rectangular area, you can query to Xi'an , Zhengzhou, Wuhan.

 

[Picture dump outside the chain fails, the source station may have a security chain mechanism, it is recommended to save the pictures uploaded directly down (img-ZiJZ1UCp-1578904421240) (D: \ work \ elasticsearch \ geo_bouding_box.png)]

Left and right corners inquiry

 

{

"query":{

"bool":{

"must":{

"match_all":{}

},

"filter":{

"geo_bounding_box":{

"location":{

"top_left":{

"lat":"38.532499",

"lon":"106.193769"

},

"bottom_right":{

"lon":"115.907542"

}

}

}

}

}

}

}

The lower left and upper right corner of inquiry

 

{

"query": {

"bool": {

"must": {

"match_all": {}

},

"filter": {

"geo_bounding_box": {

"location": {

"top_right": {

"lat": "40.807062",

"lon": "119.108671"

},

"bottom_left": {

"lat": "22.12604",

"lon": "101.741623"

}

}

}

}

}

}

}

20

3. Data Query (geo_polygon query) in a designated polygon

geo_polygon query used to find geographic point within a given polygon. For example, a triangle Hohhot, Chongqing, Shanghai, the three make up the query in changing the position of the triangular area of ​​the city.

 

[Picture dump outside the chain fails, the source station may have a security chain mechanism, it is recommended to save the pictures uploaded directly down (img-MXBETgbX-1578904421241) (D: \ work \ elasticsearch \ geo_polygon.png)]

 

{

"query":{

"bool":{

"must":{

"match_all":{}

},

"filter":{

"geo_polygon":{

"location":{

"points":[

{"lat":"40.835015","lon":"111.712958"},

{"lat":"29.640695","lon":"106.561715"},

{"lat":"31.23482","lon":"121.50032"}

]

}

}

}

}

}

4 types of data query geo_shape (geo_shape query)

geo_shapequery geo_shape query for the type of geographic data, comprising the relationship between the geographic shape it is: intersection, comprising disjoint three. Create a new index for the test, which type setting location field is geo_shape type:

 

PUT geoshape

{

"settings": {

"number_of_shards": "1",

"number_of_replicas": "0"

},

"mappings": {

"properties": {

"city": {

"type": "keyword"

},

"location": {

"type": "geo_shape"

}

}

}

}

13

geo_point types of fields are: first latitude, the longitude, but the point is geo_shape types: first longitude, latitude after. This requires special attention.

 

POST geoshape/_doc/1

{

"City": "Xi'an - Zhengzhou"

"location":{

"type":"linestring",

"coordinates":[

[108.953364,34.372762],

[113.626277,34.76845]

]

}

}

Query the data contained within the rectangular shape Yinchuan geographical Nanchang composition, since the linear Xian and Zhengzhou composition falling within the rectangular area, it can be queried.

 

{

"query":{

"bool":{

"must":{

"match_all":{}

},

"filter":{

"geo_shape":{

"location":{

"shape":{

"type":"envelope",

"coordinates":[

[106.230564,38.50359],

[115.870747,28.704175]

]

},

"relation":"within"

}

}

}

}

}

}

Source: https://blog.csdn.net/dwjf321/article/details/103960280

 

Published 277 original articles · won praise 65 · views 380 000 +

Guess you like

Origin blog.csdn.net/ailiandeziwei/article/details/104737068