Geo Search with Elasticsearch.Net/Nest

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

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

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

{
  "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
          }
        }
      }
    ]
  }
}
1 Like

Thank you for your help.
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 .

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