# Geo Search with Elasticsearch.Net/Nest

**URL:** <https://discuss.elastic.co/t/geo-search-with-elasticsearch-net-nest/111025>\
**Category:** Elasticsearch\
**Created:** [December 11, 2017, 7:31am UTC](https://discuss.elastic.co/t/geo-search-with-elasticsearch-net-nest/111025 "2017-12-11T07:31:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amir\_P](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Amir\_P](https://discuss.elastic.co/u/Amir_P)\
**Post date:** [December 11, 2017, 7:31am UTC](https://discuss.elastic.co/t/geo-search-with-elasticsearch-net-nest/111025/1 "2017-12-11T07:31:54Z")

</div>

Hello everyone,  
I’m trying to make a simple geo bounding box search using [Elasticsearch.Net/Nest](http://Elasticsearch.Net/Nest) but couldn’t find the right way.  
I would be happy if someone could help me, thanks ahead.

---

<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:** [December 12, 2017, 12:50am UTC](https://discuss.elastic.co/t/geo-search-with-elasticsearch-net-nest/111025/2 "2017-12-12T00:50:35Z")

</div>

Here's a simple example with NEST 5.x against Elasticsearch 5.x

```auto
void Main()
{
    var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var defaultIndex = "messages";
    var connectionSettings = new ConnectionSettings(pool)
        .DefaultIndex(defaultIndex);

    var client = new ElasticClient(connectionSettings);

    // delete the index if it already exists 
    // (allows you to run this example again and again)
    if (client.IndexExists(defaultIndex).Exists)
    {
        client.DeleteIndex(defaultIndex);
    }
  
    // create the index with the message mapping  
    client.CreateIndex(defaultIndex, c=> c
        .Mappings(m => m
            .Map<Message>(mm => mm
                .AutoMap()
                .Properties(p => p
                    .GeoPoint(k => k
                        .Name(n => n.Location)
                    )
                )
            )
        )
    );

    // index a few documents
    client.IndexMany(new[]
    {
        new Message { Id = 1, Location = new GeoLocation(2, 2) },
        new Message { Id = 2, Location = new GeoLocation(1, 5) },
        new Message { Id = 3, Location = new GeoLocation(4, 3) }
    });
    
    // refresh index so newly indexed documents appear in search.
    // NOTE: you ideally don't call this in production
    // as it causes many segments to be written to disk. Best to
    // let the refresh interval take its course
    client.Refresh(defaultIndex);

    // search...
    var searchResponse = client.Search<Message>(s => s
        .Query(q => +q
            .GeoBoundingBox(g => g
                .Field(f => f.Location)
                .BoundingBox(bb => bb
                    .TopLeft(4, 1)
                    .BottomRight(1, 4)
                )
            )
        )
    );
    
    // do something with documents returned
    var documents = searchResponse.Documents;
}

public class Message
{
    public int Id { get; set; }
    
    public GeoLocation Location { get; set; }
}

```

The response from the search query is

```json
{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.0,
    "hits" : [
      {
        "_index" : "messages",
        "_type" : "message",
        "_id" : "1",
        "_score" : 0.0,
        "_source" : {
          "id" : 1,
          "location" : {
            "lat" : 2.0,
            "lon" : 2.0
          }
        }
      },
      {
        "_index" : "messages",
        "_type" : "message",
        "_id" : "3",
        "_score" : 0.0,
        "_source" : {
          "id" : 3,
          "location" : {
            "lat" : 4.0,
            "lon" : 3.0
          }
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![Amir\_P](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Amir\_P](https://discuss.elastic.co/u/Amir_P)\
**Post date:** [December 12, 2017, 7:57am UTC](https://discuss.elastic.co/t/geo-search-with-elasticsearch-net-nest/111025/3 "2017-12-12T07:57:51Z")

</div>

Thank you for your help.  
Is there a source you took this code from?

---

<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:** [December 12, 2017, 8:16am UTC](https://discuss.elastic.co/t/geo-search-with-elasticsearch-net-nest/111025/4 "2017-12-12T08:16:37Z")

</div>

> [@Amir\_P](#):
>
> Is there a source you took this code from?

No, it's just an example I wrote in LinqPad. It's worth checking out [the documentation for the client](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/index.html).

---

<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:** [January 9, 2018, 8:17am UTC](https://discuss.elastic.co/t/geo-search-with-elasticsearch-net-nest/111025/5 "2018-01-09T08:17:06Z")

</div>

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