当前位置: 首页 > 工具软件 > Dr Geo > 使用案例 >

[elasticsearch笔记] geo

宰父远
2023-12-01

note

demo

GET /kibana_sample_data_flights/_search?size=1

# https://en.wikipedia.org/wiki/Geohash
# Geohash detail: https://zhuanlan.zhihu.com/p/35940647

# lat lon as properties
GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "DestLocation" : {
                        "top_left" : {
                            "lat" : 40.73,
                            "lon" : -74.1
                        },
                        "bottom_right" : {
                            "lat" : 40.01,
                            "lon" : -71.12
                        }
                    }
                }
            }
        }
    }
}

# lat lon as array
GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "DestLocation" : {
                        "top_left" : [-74.1, 40.73],
                        "bottom_right" : [-71.12, 40.01]
                    }
                }
            }
        }
    }
}

# lat lon as string
GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "DestLocation" : {
                        "top_left" : "40.73,-74.1",
                        "bottom_right" : "40.01,-71.12"
                    }
                }
            }
        }
    }
}

# Bounding Box as Well-known Text(WKT)
GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "DestLocation" : {
                        "wkt" : "BBOX (-74.1, -71.12, 40.73, 40.01)"
                    }
                }
            }
        }
    }
}

# ?? Geohash
GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "DestLocation" : {
                        "top_left" : "dr5r9ydj2y73",
                        "bottom_right" : "drj7teegpus6"
                    }
                }
            }
        }
    }
}

# match entire area of a geohash
GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "DestLocation" : {
                        "top_left" : "dr",
                        "bottom_right" : "dr"
                    }
                }
            }
        }
    }
}

GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_bounding_box" : {
                    "DestLocation" : {
                        "top" : 40.73,
                        "left" : -74.1,
                        "bottom" : 40.01,
                        "right" : -71.12
                    }
                }
            }
        }
    }
}

GET /kibana_sample_data_flights/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "DestLocation": {
            "top_left": {
              "lat": 40.73,
              "lon": -74.1
            },
            "bottom_right": {
              "lat": 40.1,
              "lon": -71.12
            }
          }
        }
      }
    }
  }
}


GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "200km",
                    "DestLocation": {
                        "lat" : -33,
                        "lon" : 151
                    }
                }
            }
        }
    }
}

GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "200km",
                    "DestLocation": [151,-33]
                }
            }
        }
    }
}

GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "200km",
                    "DestLocation": "-33,151"
                }
            }
        }
    }
}

GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_distance" : {
                    "distance" : "200km",
                    "DestLocation": "drm3btev3e86"
                }
            }
        }
    }
}

GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_polygon" : {
                    "DestLocation": {
                        "points" : [
                            {"lat" : 40, "lon" : -70},
                            {"lat" : 30, "lon" : -80},
                            {"lat" : 10, "lon" : -90}
                        ]
                    }
                }
            }
        }
    }
}

GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_polygon" : {
                    "DestLocation": {
                        "points" : [
                            [ -70,40],
                            [-80,30],
                            [-90,10]
                        ]
                    }
                }
            }
        }
    }
}

GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_polygon" : {
                    "DestLocation": {
                        "points" : [
                            "40,-70",
                            "30,-80",
                            "10,-90"
                        ]
                    }
                }
            }
        }
    }
}

GET /kibana_sample_data_flights/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match_all" : {}
            },
            "filter" : {
                "geo_polygon" : {
                    "DestLocation": {
                        "points" : [
                            "drn5x1g8cu2y",
                            "30,-80",
                            "10,-90"
                        ]
                    }
                }
            }
        }
    }
}

PUT /example
{
    "mappings": {
        "properties": {
            "location": {
                "type": "geo_shape"
            }
        }
    }
}
POST /example/_doc?refresh
{
    "name": "Wind & Wetter, Berlin, Germany",
    "location": {
        "type": "point",
        "coordinates": [13.400544, 52.530286]
    }
}

GET /example/_search
{
    "query":{
        "bool": {
            "must": {
                "match_all": {}
            },
            "filter": {
                "geo_shape": {
                    "location": {
                        "shape": {
                            "type": "envelope",
                            "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
                        },
                        "relation": "within"
                    }
                }
            }
        }
    }
}

PUT /shapes
{
    "mappings": {
        "properties": {
            "location": {
                "type": "geo_shape"
            }
        }
    }
}

PUT /shapes/_doc/deu
{
    "location": {
        "type": "envelope",
        "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
    }
}

GET /example/_search
{
    "query": {
        "bool": {
            "filter": {
                "geo_shape": {
                    "location": {
                        "indexed_shape": {
                            "index": "shapes",
                            "id": "deu",
                            "path": "location"
                        }
                    }
                }
            }
        }
    }
}
 类似资料: