官术网_书友最值得收藏!

Mapping a GeoPoint field

ElasticSearch natively supports the use of geolocation types—special types that allow localization of your document in geographic coordinate (latitude and longitude) around the world.

There are two main types used in the geographic world: the point and the shape. In this recipe we'll see the GeoPoint—the base element of a geolocation.

Getting ready

You need a working ElasticSearch cluster.

How to do it...

The type of the field must be set to geo_point to define a GeoPoint.

We can extend the order example adding a new field that stores the location of a customer. The following code will be the result:

{
  "order": {
    "properties": {
      "id": {
        "type": "string",
        "store": "yes",
        "index": "not_analyzed"
      },
    "date": {
      "type": "date",
      "store": "no",
      "index": "not_analyzed"
    },
    "customer_id": {
      "type": "string",
      "store": "yes",
      "index": "not_analyzed"
    },
    "customer_ip": {
      "type": "ip",
      "store": "yes",
      "index": "yes"
    },
    "customer_location": {
      "type": "geo_point",
      "store": "yes"
    },
    "sent": {
      "type": "boolean",
      "store": "no",
      "index": "not_analyzed"
    }
  }
  }
}

How it works...

When ElasticSearch indexes a document with a GeoPoint field (such as lat, lon), it processes the latitude and longitude coordinates and creates a special accessory field data to fast query on these coordinates.

It depends on properties, given latitude and a longitude, it's possible to compute a geohash value (http://en.wikipedia.org/wiki/Geohash) and the index process also optimizes these values for special computation such as distance, ranges, and in-shape match.

GeoPoint has special parameters that allow storage of additional geographic data:

  • lat_lon (defaults to false): This allows storing the latitude and longitude as a .lat and .lon field. Storing these values improves the performance in many memory algorithms used in distance and in-shape calculus.

    Note

    It makes sense to be stored only if there is a single point value for field, for multiple values.

  • geohash (defaults to false): This allows storing the computed geohash value.
  • geohash_precision (defaults to 12): This defines the precision to be used in geohash calculus.

For example, given a GeoPoint value [45.61752, 9.08363], it will store:

  • customer_location = "45.61752, 9.08363"
  • customer_location.lat = 45.61752
  • customer_location.lon = 9.08363
  • customer_location.geohash = "u0n7w8qmrfj"

There's more...

GeoPoint is a special type and can accept several formats as input:

  • lat and lon as properties:
        "customer_location": {
            "lat": 45.61752,
            "lon": 9.08363
        },
  • lat and lon as strings:
    "customer_location": "45.61752,9.08363",
  • Geohash string
    "customer_location": "u0n7w8qmrfj",
  • As a GeoJSON array (note in it that lat and lon are reversed)
    "customer_location": [9.08363, 45.61752]
主站蜘蛛池模板: 新巴尔虎右旗| 韶关市| 三门峡市| 金昌市| 大邑县| 承德市| 南和县| 招远市| 通州市| 汽车| 海晏县| 卫辉市| 南郑县| 榆中县| 诏安县| 中宁县| 宁津县| 额敏县| 邛崃市| 威海市| 东乡族自治县| 辽阳县| 闵行区| 同德县| 灌南县| 孙吴县| 莱阳市| 鄱阳县| 柏乡县| 阳城县| 盐源县| 清徐县| 项城市| 玉山县| 沙田区| 岫岩| 铜陵市| 精河县| 佛山市| 仙游县| 扎赉特旗|