# NEST (C#) Index list of custom, location-like objects - how to map it to index properly?

**URL:** https://discuss.elastic.co/t/nest-c-index-list-of-custom-location-like-objects-how-to-map-it-to-index-properly/144598
**Category:** Elasticsearch
**Created:** [August 15, 2018, 11:47pm UTC](https://discuss.elastic.co/t/nest-c-index-list-of-custom-location-like-objects-how-to-map-it-to-index-properly/144598 "2018-08-15T23:47:43Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![ivankarpey](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@ivankarpey](https://discuss.elastic.co/u/ivankarpey)
#### Post date: [August 15, 2018, 11:47pm UTC](https://discuss.elastic.co/t/nest-c-index-list-of-custom-location-like-objects-how-to-map-it-to-index-properly/144598/1 "2018-08-15T23:47:43Z")

</div>

I have a simple Poco

> class Product  
> {  
> ...  
> public List Adrresses {get; set;}  
> }

> class GeoItem  
> {  
> public string Address {get; set;}  
> public double Latititude {get; set;}  
> public double Longitude {get; set;}  
> ....  
> }

So, could you please provide me an example of how should I write fluent mapping for the **Product** and could I map **GeoPoint** from **GeoItem** directly, or I need to somehow convert **GeoItem** to **GeoLocation** object somehow with decorator for instance?

---

<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: [August 16, 2018, 3:56am UTC](https://discuss.elastic.co/t/nest-c-index-list-of-custom-location-like-objects-how-to-map-it-to-index-properly/144598/2 "2018-08-16T03:56:43Z")

</div>

Take a look at the [fluent mapping documentation](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/fluent-mapping.html) as I think it'll help to see what mapping is generated for different fluent API mappings.

You probably want to map at least the `Latitude` and a `Longitude` of `GeoItem` to `GeoLocation` as NEST understands how to serialize `GeoLocation` when making the indexing request to Elasticsearch, and understands to [automap](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/auto-map.html) as [`geo_point`](https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html) when creating a mapping from the type.

Here's an example

```auto
public class Product
{
    public List<GeoItem> Addresses {get;set;}
}

public class GeoItem
{
    public string Address { get; set; }
    public GeoLocation LatLon {get;set;}
}

var client = new ElasticClient(settings);

var indexResponse = client.CreateIndex("my_index", c => c
    .Mappings(m => m
        .Map<Product>(mm => mm
            .AutoMap()
            .Properties(p => p
                .Nested<GeoItem>(n => n
                    .Name(nn => nn.Addresses)
                    .AutoMap()
                )
            )
        )
    )
);

```

generates the following create index request

```json
PUT http://localhost:9200/my_index
{
  "mappings": {
    "product": {
      "properties": {
        "addresses": {
          "type": "nested",
          "properties": {
            "address": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            "latLon": {
              "type": "geo_point"
            }
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![ivankarpey](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@ivankarpey](https://discuss.elastic.co/u/ivankarpey)
#### Post date: [August 16, 2018, 11:30am UTC](https://discuss.elastic.co/t/nest-c-index-list-of-custom-location-like-objects-how-to-map-it-to-index-properly/144598/3 "2018-08-16T11:30:29Z")

</div>

@forloop The problem is that I use **GeoItem** in other places of my code and it's already settled with current structure, so I can't change it to support elastic **GeoLocation** directly. Is there a way to somehow set dynamic conversion from **GeoItem** to **GeoLocation** during the mapping phase (like in automapper for instance) or I need to create separate model?

---

<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: [August 17, 2018, 4:35am UTC](https://discuss.elastic.co/t/nest-c-index-list-of-custom-location-like-objects-how-to-map-it-to-index-properly/144598/4 "2018-08-17T04:35:27Z")

</div>

You could write your own [Json.NET](http://Json.NET) `JsonConverter` for `GeoItem` that serializes it to the format expected by Elasticsearch, then use the [Nest.JsonNetSerializer nuget package](https://www.nuget.org/packages/NEST.JsonNetSerializer/) and [hook up the `JsonNetSerializer`](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/custom-serialization.html#_jsonnetserializer) as the serializer for the client to use

```auto
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var connectionSettings =
    new ConnectionSettings(pool, sourceSerializer: JsonNetSerializer.Default);
var client = new ElasticClient(connectionSettings);

```

Alternatively, you could use specific types to represent the documents in Elasticsearch and use `GeoLocation`, then map your domain objects to these POCOs with something like AutoMapper.

---

<div class="post-metadata">

### Author: ![ivankarpey](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@ivankarpey](https://discuss.elastic.co/u/ivankarpey)
#### Post date: [August 17, 2018, 8:23am UTC](https://discuss.elastic.co/t/nest-c-index-list-of-custom-location-like-objects-how-to-map-it-to-index-properly/144598/5 "2018-08-17T08:23:33Z")

</div>

ok, I decide to proceed with special elasticmodels since there is seems like similar problem with other elastic specific fields like DoubleRange for example. But thanks a lot for your advice

---

<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: [August 17, 2018, 8:35am UTC](https://discuss.elastic.co/t/nest-c-index-list-of-custom-location-like-objects-how-to-map-it-to-index-properly/144598/6 "2018-08-17T08:35:47Z")

</div>

> [@ivankarpey](#):
>
> I decide to proceed with special elasticmodels

That's what I would recommend, since it'll allow you to take advantage of Nest specific types on your models

---

<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: [September 14, 2018, 8:36am UTC](https://discuss.elastic.co/t/nest-c-index-list-of-custom-location-like-objects-how-to-map-it-to-index-properly/144598/7 "2018-09-14T08:36:03Z")

</div>

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