Search code examples
mongodbelasticsearchelasticsearch-plugin

ElasticSearch mapping type Twitter coordinates


Is there anyway to mapping type this structure for Spatial Search ElasticSearch:

"coordinates" : {
        "type" : "Point",
        "coordinates" : [
            100.41404641,
            5.37384675
        ]
    },

I was wondering to use this as it comes from my MongoDB for geo-point type without splitting them into lat and long fields like existing tutorials and examples.

This structure is what Twitter API streams as a default, so I could save it as "location":{ "lat": 100.41, "long": 5.34 } into MongoDB. But before adding extra fields to my database or changing things I want to make sure there is no way to use this structure for spatial search in ElasticSearch.

Thanks


Solution

  • According to ElasticSearch documents it is possible to have an array of lat and long in the mapping as a Geo point type.

    Mapping:

    PUT my_index
    {
      "mappings": {
        "my_type": {
          "properties": {
            "location": {
              "type": "geo_point"
            }
          }
        }
      }
    }
    

    Format in [lon, lat], note, the order of lon/lat here in order to conform with GeoJSON.

    PUT my_index/my_type/4
    {
      "text": "Geo-point as an array",
      "location": [ -71.34, 41.12 ] 
    }
    

    Other examples for Geo-point datatype:

    • Geo-point expressed as a string with the format: "lat,lon"
    • Geo-point expressed as a geohash
    • Geo-point expressed as an array with the format: [ lon, lat]