# Create Index with GeoPoint type

**URL:** <https://discuss.elastic.co/t/create-index-with-geopoint-type/107293>\
**Category:** Elasticsearch\
**Created:** [November 12, 2017, 8:18am UTC](https://discuss.elastic.co/t/create-index-with-geopoint-type/107293 "2017-11-12T08:18:50Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![yanwei2508](https://avatars.discourse-cdn.com/v4/letter/y/ee59a6/32.png) [@yanwei2508](https://discuss.elastic.co/u/yanwei2508)\
**Post date:** [November 12, 2017, 8:18am UTC](https://discuss.elastic.co/t/create-index-with-geopoint-type/107293/1 "2017-11-12T08:18:50Z")

</div>

NEST/Elasticsearch.Net version:5.5

I want to Create index with model like below:  
public class company  
{

public long id { get; set; }

public string name { get; set; }

public string address { get; set; }

public long areaCode { get; set; }

public string city { get; set; }

public string state { get; set; }

public string latestActivityTimestamp { get; set; }

public location location { get; set; }  
}

public class location  
{  
[JsonProperty(PropertyName = "lat")]  
public double latitude { get; set; }

[JsonProperty(PropertyName = "lon")]  
public double longitude { get; set; }  
}

I want to Create index it and mapping the location as GeoPoint ,I find some example like :  
internal class Location  
{  
[JsonProperty(PropertyName = "lat")]  
public double Latitude { get; set; }

[JsonProperty(PropertyName = "lon")]  
public double Longitude { get; set; }

}  
and then mapping it with:  
.MapFromAttributes()  
.Properties(p =\>  
p.GeoPoint(s =\>  
[s.Name](http://s.Name)(n =\> n.Location).IndexGeoHash().IndexLatLon().GeoHashPrecision(12)  
)  
)

The Problem is I can not find MapFromAttributes(), IndexGeoHash() ,I don't know how to deal with my problem.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 13, 2017, 1:05am UTC](https://discuss.elastic.co/t/create-index-with-geopoint-type/107293/2 "2017-11-13T01:05:31Z")

</div>

> [@yanwei2508](#):
>
> MapFromAttributes(),

This is in NEST 1.x, but does not exist in NEST 5.x.

You should use the [`GeoLocation`](https://github.com/elastic/elasticsearch-net/blob/47c2123eb328220bf5aba35c901016f352c28123/src/Nest/QueryDsl/Geo/GeoLocation.cs#L15) type as the property to represent a `geo_point`. [Automapping will map this as a `geo_point` by default.](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/auto-map.html)

---

<div class="post-metadata">

**Author:** ![yanwei2508](https://avatars.discourse-cdn.com/v4/letter/y/ee59a6/32.png) [@yanwei2508](https://discuss.elastic.co/u/yanwei2508)\
**Post date:** [November 13, 2017, 7:16am UTC](https://discuss.elastic.co/t/create-index-with-geopoint-type/107293/3 "2017-11-13T07:16:01Z")

</div>

I import the Geolocation to my project, and define the location as  
Geolocation ,but after automapping, I get the mapping is like below

 ![](https://us1.discourse-cdn.com/elastic/original/3X/f/c/fccb755df76e7afb4ad6372ef296fca1ea5d9d7d.png)

so the type is still not geo\_point?

my code:  
public class company  
{

```
    public long id { get; set; }

    public string name { get; set; }

    public string address { get; set; }

    public long areaCode { get; set; }

    public string city { get; set; }

    public string state { get; set; }

    public string latestActivityTimestamp { get; set; }

    public GeoLocation location { get; set; }
}

```

client.CreateIndex(UsersIndexName, descriptor =\> descriptor  
.Mappings(ms =\> ms  
.Map(m =\> m

.AutoMap()  
)  
)  
);

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 14, 2017, 11:45pm UTC](https://discuss.elastic.co/t/create-index-with-geopoint-type/107293/4 "2017-11-14T23:45:20Z")

</div>

You'll need to delete the existing index if there is one and create again

```auto
public class Message
{
    public GeoLocation Location { get; set; }
}

var defaultIndex = "messages";
var client = new ElasticClient();

if (client.IndexExists(defaultIndex).Exists)
{		
    client.DeleteIndex(defaultIndex);
}
	
client.CreateIndex(defaultIndex, c=> c
    .Mappings(m => m
        .Map<Message>(mm => mm
            .AutoMap()
        )
    )
);

```

yields

```json
PUT http://localhost:9200/messages
{
  "mappings": {
    "message": {
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![yanwei2508](https://avatars.discourse-cdn.com/v4/letter/y/ee59a6/32.png) [@yanwei2508](https://discuss.elastic.co/u/yanwei2508)\
**Post date:** [November 19, 2017, 2:09pm UTC](https://discuss.elastic.co/t/create-index-with-geopoint-type/107293/5 "2017-11-19T14:09:32Z")

</div>

I already delete the existing index before create again, but it still doesn't work, so I try to use curl to put index and mapping, now the problem is my json data never can bulk in to es , I look at [https://www.elastic.co/guide/en/kibana/current/tutorial-load-dataset.html](https://www.elastic.co/guide/en/kibana/current/tutorial-load-dataset.html)

theirs data I can bulk to es, and they are ndjson, when I edit my data as theirs data ,it can work and bulk to es. like below:  
{"index":{"\_index":"indexcompany","\_type":"company"}}  
{"id":174,"name":"JET REPORTS DANMARK ApS","address":"Lyskær 3","areaCode":2730,"city":"Herlev","location":{"lat":55.7145620017324,"lon":12.4359915318663}}  
{"index":{"\_index":"indexcompany","\_type":"company"}}  
{"id":568,"name":"OUTOKUMPU A/S","address":"Alhambravej 3","areaCode":1826,"city":"Frederiksberg C","location":{"lat":55.6743134573089,"lon":12.5440667455454}}

but my original data is like:  
[{  
"id": 174,  
"name": "JET REPORTS DANMARK ApS",  
"address": "Lyskær 3",  
"areaCode": "2730",  
"city": "Herlev",  
"state": null,  
"location": {  
"latitude": 55.7145620017324,  
"longitude": 12.4359915318663  
},  
"latestActivityTimestamp": null,  
"isFavorite": false  
}, {  
"id": 568,  
"name": "OUTOKUMPU A/S",  
"address": "Alhambravej 3",  
"areaCode": "1826",  
"city": "Frederiksberg C",  
"state": null,  
"location": {  
"latitude": 55.6743134573089,  
"longitude": 12.5440667455454  
},  
"latestActivityTimestamp": null,  
"isFavorite": false  
}]

it never works, so what I can do now?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 20, 2017, 2:18am UTC](https://discuss.elastic.co/t/create-index-with-geopoint-type/107293/6 "2017-11-20T02:18:22Z")

</div>

> [@yanwei2508](#):
>
> I already delete the existing index before create again, but it still doesn't work

Run [the example provided](https://discuss.elastic.co/t/create-index-with-geopoint-type/107293/4) using NEST 5.5.0 against Elasticsearch 5.x and you'll see that it does work.

> [@yanwei2508](#):
>
> it never works, so what I can do now?

You'll either need to convert your data to fit the newline delimited JSON that the bulk API expects, or deserialize your JSON data into C# POCOs and use NEST's `Bulk()` method to index your data.

---

<div class="post-metadata">

**Author:** ![yanwei2508](https://avatars.discourse-cdn.com/v4/letter/y/ee59a6/32.png) [@yanwei2508](https://discuss.elastic.co/u/yanwei2508)\
**Post date:** [November 20, 2017, 10:44am UTC](https://discuss.elastic.co/t/create-index-with-geopoint-type/107293/7 "2017-11-20T10:44:56Z")

</div>

Before I always use NEST'S bulk(), but now I have the geopoint type in my data ,the earlier topics I already mentioned it ,I use geolocation to define the location ,but it can't be convert to geopoint type. So I try bulk API and they expects NDJSON, I edit my data like:  
{"index":{"\_id":"1"}}  
{"id":174,"name":"JET REPORTS DANMARK ApS","address":"Lyskær 3","areaCode":2730,"city":"Herlev","location":{"lat":55.7145620017324,"lon":12.4359915318663}}  
{"index":{"\_id":"2"}}  
{"id":568,"name":"OUTOKUMPU A/S","address":"Alhambravej 3","areaCode":1826,"city":"Frederiksberg C","location":{"lat":55.6743134573089,"lon":12.5440667455454}}

I get success to import them to es and the location is geopoint type. But if it is necessary every line must with {"index":{"\_id":"1"}},{"index":{"\_id":"2"}}? I can't bulk them like:  
{"id":174,"name":"JET REPORTS DANMARK ApS","address":"Lyskær 3","areaCode":2730,"city":"Herlev","location":{"lat":55.7145620017324,"lon":12.4359915318663}}  
{"id":568,"name":"OUTOKUMPU A/S","address":"Alhambravej 3","areaCode":1826,"city":"Frederiksberg C","location":{"lat":55.6743134573089,"lon":12.5440667455454}}

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 20, 2017, 8:24pm UTC](https://discuss.elastic.co/t/create-index-with-geopoint-type/107293/8 "2017-11-20T20:24:49Z")

</div>

> [@yanwei2508](#):
>
> I get success to import them to es and the location is geopoint type. But if it is necessary every line must with {"index":{"\_id":"1"}},{"index":{"\_id":"2"}}?

Yes, that is [**the format that the bulk API expects**](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/docs-bulk.html). With NEST, when indexing a collection of objects, the client generates this format for you.

---

<div class="post-metadata">

**Author:** ![yanwei2508](https://avatars.discourse-cdn.com/v4/letter/y/ee59a6/32.png) [@yanwei2508](https://discuss.elastic.co/u/yanwei2508)\
**Post date:** [November 22, 2017, 11:31am UTC](https://discuss.elastic.co/t/create-index-with-geopoint-type/107293/9 "2017-11-22T11:31:44Z")

</div>

Ok, thank you ,now I understand it ,and I already test some sample data ,get success with geopoint:slight\_smile:

About the NEST bulk(), for the geopoint data type it does not work, I use 5.5 version, so If it will be ok in later version? Because we want to import data to ES with C# API.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [November 23, 2017, 8:22pm UTC](https://discuss.elastic.co/t/create-index-with-geopoint-type/107293/10 "2017-11-23T20:22:07Z")

</div>

> [@yanwei2508](#):
>
> About the NEST bulk(), for the geopoint data type it does not work

Can you provide a small concise but complete example demonstrating that it "does not work"?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 21, 2017, 8:22pm UTC](https://discuss.elastic.co/t/create-index-with-geopoint-type/107293/11 "2017-12-21T20:22:08Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
