Geo Shape Query Usageedit

The GeoShape Query uses the same grid square representation as the geo_shape mapping to find documents that have a shape that intersects with the query shape. It will also use the same PrefixTree configuration as defined for the field mapping.

The query supports two ways of defining the query shape, either by providing a whole shape definition, or by referencing the name of a shape pre-indexed in another index.

See the Elasticsearch documentation on geoshape queries for more detail.

Querying with Pointedit

Fluent DSL exampleedit

q
.GeoShape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.LocationShape)
    .Shape(s => s
        .Point(PointCoordinates)
    )
    .Relation(GeoShapeRelation.Intersects)
)

Object Initializer syntax exampleedit

new GeoShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.LocationShape),
    Shape = new PointGeoShape(PointCoordinates),
    Relation = GeoShapeRelation.Intersects,
}

Example json output.

{
  "geo_shape": {
    "_name": "named_query",
    "boost": 1.1,
    "locationShape": {
      "relation": "intersects",
      "shape": {
        "type": "point",
        "coordinates": [
          38.897676,
          -77.03653
        ]
      }
    }
  }
}

Querying with MultiPointedit

Elasticsearch 7.7.0+ required when MultiPoint is indexed using BKD trees (the default).

Fluent DSL exampleedit

q
.GeoShape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.LocationShape)
    .Shape(s => s
        .MultiPoint(MultiPointCoordinates)
    )
    .Relation(GeoShapeRelation.Intersects)
)

Object Initializer syntax exampleedit

new GeoShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.LocationShape),
    Shape = new MultiPointGeoShape(MultiPointCoordinates),
    Relation = GeoShapeRelation.Intersects,
}

Example json output.

{
  "geo_shape": {
    "_name": "named_query",
    "boost": 1.1,
    "locationShape": {
      "relation": "intersects",
      "shape": {
        "type": "multipoint",
        "coordinates": [
          [
            38.897676,
            -77.03653
          ],
          [
            38.889939,
            -77.009051
          ]
        ]
      }
    }
  }
}

Querying with LineStringedit

Fluent DSL exampleedit

q
.GeoShape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.LocationShape)
    .Shape(s => s
        .LineString(LineStringCoordinates)
    )
    .Relation(GeoShapeRelation.Intersects)
)

Object Initializer syntax exampleedit

new GeoShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.LocationShape),
    Shape = new LineStringGeoShape(LineStringCoordinates),
    Relation = GeoShapeRelation.Intersects,
}

Example json output.

{
  "geo_shape": {
    "_name": "named_query",
    "boost": 1.1,
    "locationShape": {
      "relation": "intersects",
      "shape": {
        "type": "linestring",
        "coordinates": [
          [
            38.897676,
            -77.03653
          ],
          [
            38.889939,
            -77.009051
          ]
        ]
      }
    }
  }
}

Querying with MultiLineStringedit

Fluent DSL exampleedit

q
.GeoShape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.LocationShape)
    .Shape(s => s
        .MultiLineString(MultiLineStringCoordinates)
    )
    .Relation(GeoShapeRelation.Intersects)
)

Object Initializer syntax exampleedit

new GeoShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.LocationShape),
    Shape = new MultiLineStringGeoShape(MultiLineStringCoordinates),
    Relation = GeoShapeRelation.Intersects,
}

Example json output.

{
  "geo_shape": {
    "_name": "named_query",
    "boost": 1.1,
    "locationShape": {
      "relation": "intersects",
      "shape": {
        "type": "multilinestring",
        "coordinates": [
          [
            [
              2.0,
              12.0
            ],
            [
              2.0,
              13.0
            ],
            [
              3.0,
              13.0
            ],
            [
              3.0,
              12.0
            ]
          ],
          [
            [
              0.0,
              10.0
            ],
            [
              0.0,
              11.0
            ],
            [
              1.0,
              11.0
            ],
            [
              1.0,
              10.0
            ]
          ],
          [
            [
              0.2,
              10.2
            ],
            [
              0.2,
              10.8
            ],
            [
              0.8,
              10.8
            ],
            [
              0.8,
              12.0
            ]
          ]
        ]
      }
    }
  }
}

Querying with Polygonedit

Fluent DSL exampleedit

q
.GeoShape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.LocationShape)
    .Shape(s => s
        .Polygon(PolygonCoordinates)
    )
    .IgnoreUnmapped()
    .Relation(GeoShapeRelation.Intersects)
)

Object Initializer syntax exampleedit

new GeoShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.LocationShape),
    Shape = new PolygonGeoShape(PolygonCoordinates),
    IgnoreUnmapped = true,
    Relation = GeoShapeRelation.Intersects,
}

Example json output.

{
  "geo_shape": {
    "_name": "named_query",
    "boost": 1.1,
    "ignore_unmapped": true,
    "locationShape": {
      "relation": "intersects",
      "shape": {
        "type": "polygon",
        "coordinates": [
          [
            [
              10.0,
              -17.0
            ],
            [
              15.0,
              16.0
            ],
            [
              0.0,
              12.0
            ],
            [
              -15.0,
              16.0
            ],
            [
              -10.0,
              -17.0
            ],
            [
              10.0,
              -17.0
            ]
          ],
          [
            [
              8.2,
              18.2
            ],
            [
              8.2,
              -18.8
            ],
            [
              -8.8,
              -10.8
            ],
            [
              8.8,
              18.2
            ]
          ]
        ]
      }
    }
  }
}

Querying with MultiPolygonedit

Fluent DSL exampleedit

q
.GeoShape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.LocationShape)
    .Shape(s => s
        .MultiPolygon(MultiPolygonCoordinates)
    )
    .Relation(GeoShapeRelation.Intersects)
)

Object Initializer syntax exampleedit

new GeoShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.LocationShape),
    Shape = new MultiPolygonGeoShape(MultiPolygonCoordinates),
    Relation = GeoShapeRelation.Intersects,
}

Example json output.

{
  "geo_shape": {
    "_name": "named_query",
    "boost": 1.1,
    "locationShape": {
      "relation": "intersects",
      "shape": {
        "type": "multipolygon",
        "coordinates": [
          [
            [
              [
                10.0,
                -17.0
              ],
              [
                15.0,
                16.0
              ],
              [
                0.0,
                12.0
              ],
              [
                -15.0,
                16.0
              ],
              [
                -10.0,
                -17.0
              ],
              [
                10.0,
                -17.0
              ]
            ],
            [
              [
                8.2,
                18.2
              ],
              [
                8.2,
                -18.8
              ],
              [
                -8.8,
                -10.8
              ],
              [
                8.8,
                18.2
              ]
            ]
          ],
          [
            [
              [
                8.0,
                -15.0
              ],
              [
                15.0,
                16.0
              ],
              [
                0.0,
                12.0
              ],
              [
                -15.0,
                16.0
              ],
              [
                -10.0,
                -17.0
              ],
              [
                8.0,
                -15.0
              ]
            ]
          ]
        ]
      }
    }
  }
}

Querying with GeometryCollectionedit

Fluent DSL exampleedit

q
.GeoShape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.LocationShape)
    .Shape(s => s
        .GeometryCollection(
            new PointGeoShape(PointCoordinates),
            new MultiPointGeoShape(MultiPointCoordinates),
            new LineStringGeoShape(LineStringCoordinates),
            new MultiLineStringGeoShape(MultiLineStringCoordinates),
            new PolygonGeoShape(PolygonCoordinates),
            new MultiPolygonGeoShape(MultiPolygonCoordinates)
        )
    )
    .Relation(GeoShapeRelation.Intersects)
)

Object Initializer syntax exampleedit

new GeoShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.LocationShape),
    Shape = new GeometryCollection(new IGeoShape[]
    {
        new PointGeoShape(PointCoordinates),
        new MultiPointGeoShape(MultiPointCoordinates),
        new LineStringGeoShape(LineStringCoordinates),
        new MultiLineStringGeoShape(MultiLineStringCoordinates),
        new PolygonGeoShape(PolygonCoordinates),
        new MultiPolygonGeoShape(MultiPolygonCoordinates),
    }),
    Relation = GeoShapeRelation.Intersects,
}

Example json output.

{
  "geo_shape": {
    "_name": "named_query",
    "boost": 1.1,
    "locationShape": {
      "relation": "intersects",
      "shape": {
        "type": "geometrycollection",
        "geometries": [
          {
            "type": "point",
            "coordinates": [
              38.897676,
              -77.03653
            ]
          },
          {
            "type": "multipoint",
            "coordinates": [
              [
                38.897676,
                -77.03653
              ],
              [
                38.889939,
                -77.009051
              ]
            ]
          },
          {
            "type": "linestring",
            "coordinates": [
              [
                38.897676,
                -77.03653
              ],
              [
                38.889939,
                -77.009051
              ]
            ]
          },
          {
            "type": "multilinestring",
            "coordinates": [
              [
                [
                  2.0,
                  12.0
                ],
                [
                  2.0,
                  13.0
                ],
                [
                  3.0,
                  13.0
                ],
                [
                  3.0,
                  12.0
                ]
              ],
              [
                [
                  0.0,
                  10.0
                ],
                [
                  0.0,
                  11.0
                ],
                [
                  1.0,
                  11.0
                ],
                [
                  1.0,
                  10.0
                ]
              ],
              [
                [
                  0.2,
                  10.2
                ],
                [
                  0.2,
                  10.8
                ],
                [
                  0.8,
                  10.8
                ],
                [
                  0.8,
                  12.0
                ]
              ]
            ]
          },
          {
            "type": "polygon",
            "coordinates": [
              [
                [
                  10.0,
                  -17.0
                ],
                [
                  15.0,
                  16.0
                ],
                [
                  0.0,
                  12.0
                ],
                [
                  -15.0,
                  16.0
                ],
                [
                  -10.0,
                  -17.0
                ],
                [
                  10.0,
                  -17.0
                ]
              ],
              [
                [
                  8.2,
                  18.2
                ],
                [
                  8.2,
                  -18.8
                ],
                [
                  -8.8,
                  -10.8
                ],
                [
                  8.8,
                  18.2
                ]
              ]
            ]
          },
          {
            "type": "multipolygon",
            "coordinates": [
              [
                [
                  [
                    10.0,
                    -17.0
                  ],
                  [
                    15.0,
                    16.0
                  ],
                  [
                    0.0,
                    12.0
                  ],
                  [
                    -15.0,
                    16.0
                  ],
                  [
                    -10.0,
                    -17.0
                  ],
                  [
                    10.0,
                    -17.0
                  ]
                ],
                [
                  [
                    8.2,
                    18.2
                  ],
                  [
                    8.2,
                    -18.8
                  ],
                  [
                    -8.8,
                    -10.8
                  ],
                  [
                    8.8,
                    18.2
                  ]
                ]
              ],
              [
                [
                  [
                    8.0,
                    -15.0
                  ],
                  [
                    15.0,
                    16.0
                  ],
                  [
                    0.0,
                    12.0
                  ],
                  [
                    -15.0,
                    16.0
                  ],
                  [
                    -10.0,
                    -17.0
                  ],
                  [
                    8.0,
                    -15.0
                  ]
                ]
              ]
            ]
          }
        ]
      }
    }
  }
}

Querying with Envelopeedit

Fluent DSL exampleedit

q
.GeoShape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.LocationShape)
    .Shape(s => s
        .Envelope(EnvelopeCoordinates)
    )
    .Relation(GeoShapeRelation.Intersects)
)

Object Initializer syntax exampleedit

new GeoShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.LocationShape),
    Shape = new EnvelopeGeoShape(EnvelopeCoordinates),
    Relation = GeoShapeRelation.Intersects,
}

Example json output.

{
  "geo_shape": {
    "_name": "named_query",
    "boost": 1.1,
    "locationShape": {
      "relation": "intersects",
      "shape": {
        "type": "envelope",
        "coordinates": [
          [
            -45.0,
            45.0
          ],
          [
            45.0,
            -45.0
          ]
        ]
      }
    }
  }
}

Querying with Circleedit

Available in Elasticsearch 7.7.0+

Fluent DSL exampleedit

q
.GeoShape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.LocationShape)
    .Shape(s => s
        .Circle(CircleCoordinates, "100m")
    )
    .Relation(GeoShapeRelation.Intersects)
)

Object Initializer syntax exampleedit

new GeoShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.LocationShape),
    Shape = new CircleGeoShape(CircleCoordinates, "100m"),
    Relation = GeoShapeRelation.Intersects,
}

Example json output.

{
  "geo_shape": {
    "_name": "named_query",
    "boost": 1.1,
    "locationShape": {
      "relation": "intersects",
      "shape": {
        "type": "circle",
        "radius": "100m",
        "coordinates": [
          45.0,
          -45.0
        ]
      }
    }
  }
}

Querying with an indexed shapeedit

The GeoShape Query supports using a shape which has already been indexed in another index and/or index type within a geoshape query. This is particularly useful for when you have a pre-defined list of shapes which are useful to your application and you want to reference this using a logical name (for example New Zealand), rather than having to provide their coordinates within the request each time.

See the Elasticsearch documentation on geoshape queries for more detail.

Fluent DSL exampleedit

q
.GeoShape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.LocationShape)
    .IndexedShape(p => p
        .Id(Project.Instance.Name)
        .Path(pp => pp.LocationShape)
        .Routing(Project.Instance.Name)
    )
    .Relation(GeoShapeRelation.Intersects)
)

Object Initializer syntax exampleedit

new GeoShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.LocationShape),
    IndexedShape = new FieldLookup
    {
        Id = Project.Instance.Name,
        Index = Infer.Index<Project>(),
        Path = Infer.Field<Project>(p => p.LocationShape),
        Routing = Project.Instance.Name
    },
    Relation = GeoShapeRelation.Intersects
}

Example json output.

{
  "geo_shape": {
    "_name": "named_query",
    "boost": 1.1,
    "locationShape": {
      "indexed_shape": {
        "id": "Durgan LLC",
        "index": "project",
        "path": "locationShape",
        "routing": "Durgan LLC"
      },
      "relation": "intersects"
    }
  }
}