Mick Walker
Mick Walker

Reputation: 3847

Elastic Search - Creating Query with NEST

Given the query:

GET places/_search
    {
      "query": {
        "match_all": {}
      },
      "size": 0,
      "aggs": {
        "filtered_cells": {
          "filter": {
            "geo_bounding_box": {
              "loc": {
                "top_left": {
                  "lat": 53.480950,
                  "lon": -2.2374300
                },
                "bottom_right": {
                  "lat": 51.5085300,
                  "lon": -0.1257400
                }
              }
            }
          },
          "aggs": {
            "cells": {
              "geohash_grid": {
                "field": "loc",
                "precision": "precision"
              },
              "aggs": {
                "center_lat": {
                  "avg": {
                    "script": "doc['loc'].lat"
                  }
                },
                "center_lon": {
                  "avg": {
                    "script": "doc['loc'].lon"
                  }
                }
              }
            }
          }
        }
      }
    }

I am trying to move this query into a piece of code I am developing, but I am having trouble structuring the query above using the NEST library.

Here is what I have so far:

var s = new SearchDescriptor<FacebookGraphResponse>()
                .MatchAll()
                .From(0)
                .Size(10)
                .Filter(filter => filter
                    .GeoBoundingBox(f => f.Loc, 53.480950, -2.2374300, 51.5085300, -0.1257400)
                );

Is anyone able to provide any input on how I can translate this query to nest, as I am currently stumped.

Many Thanks

Upvotes: 0

Views: 211

Answers (1)

Mick Walker
Mick Walker

Reputation: 3847

Solved it!

var s = new SearchDescriptor<FacebookGraphResponse>()
                .MatchAll()
                .Size(0)
                .Aggregations(aggs => aggs
                    .Filter("filtered_cells", f => f
                        .Filter(f1 => f1
                            .GeoBoundingBox(bb => bb.Loc, -2.2374300, 53.480950, -0.1257400, 51.5085300))
                        .Aggregations(a => a
                            .GeoHash("cells", geo => geo
                                .GeoHashPrecision(GeoHashPrecision.Precision10)
                                .Field("loc")
                                .Aggregations(a1 => a1
                                    .Average("center_lat", x => x.Script("doc['loc'].lat"))
                                    .Average("center_lon", x => x.Script("doc['loc'].lon")))))));

Upvotes: 1

Related Questions