Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping for array of geo_point fields in elastic

I'd like to persist some JSON to elastic(search) that looks a little like this:

{
  "name": "value",
  "points": [
    { "lat": 0.0, "lon": 0.0 },
    { "lat": 1.0, "lon": 1.0 }
  ]
}

Points being a list of the type geo_point in elastic. Because they're geo_point values I need to define the index mapping, but the closest I can see is to do this:

"place": {
  "properties": {
    "name": {
      "type": "string"
    },
    "points": {
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}

Which would mean having each point be a map with a single key of location and a geo_point value. I just want the geo_points, is this possible?

like image 778
rich Avatar asked Mar 16 '16 21:03

rich


People also ask

Can Elasticsearch index have multiple mappings?

From Elasticsearch version 6.0 by default index doesn't allow multiple types per index. The better option is to always have one document type per index. The single responsibility of the index is to maintain the single document type/mapping type per index.

How do I show mappings in Elasticsearch?

These can be accessed from your dashboard by choosing Stack Settings > Elasticsearch . The next step is to write a a curl -x get command to retrieve the mappings from Elasticsearch. You will be returned with a json output similar to the below screenshot.

What is field mapping in Elasticsearch?

Mapping is the process of defining how a document, and the fields it contains, are stored and indexed. Each document is a collection of fields, which each have their own data type. When mapping your data, you create a mapping definition, which contains a list of fields that are pertinent to the document.

How do I declare an array in Elasticsearch?

In Elasticsearch, there is no dedicated array data type. Any field can contain zero or more values by default, however, all values in the array must be of the same data type. For instance: an array of strings: [ "one" , "two" ]


2 Answers

You can define your mapping as below :

"place": {
  "properties": {
    "name": {
      "type": "string"
    },
    "points": {  // <--  it can also be a multi-valued field if you insert multiple values, as skal88 mentioned, your json would perfectly fit in this mapping.
      "type": "geo_point" 
    }
  }
}
like image 164
Rahul Avatar answered Oct 24 '22 12:10

Rahul


Yes. When you define a mapping, the values inside field can be single or array of the specific "type". You need only insert on this value a array of geo_points. Some like this:

{
  "name": "some name",
  "points": [
    { lat: 41.12, lon: 2.023 },
    { lat: 30, lon: 4.1 }
  ]
}
like image 38
skal88 Avatar answered Oct 24 '22 14:10

skal88