Shape Query Usage

edit

Like geo_shape, Elasticsearch supports the ability to index arbitrary two dimension (non Geospatial) geometries making it possible to map out virtual worlds, sporting venues, theme parks, and CAD diagrams. The shape field type supports points, lines, polygons, multi-polygons, envelope, etc.

See the Elasticsearch documentation on shape queries for more detail.

Querying with Point

edit

Fluent DSL example

edit
q
.Shape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.ArbitraryShape)
    .Shape(s => s
        .Point(PointCoordinates)
    )
    .Relation(ShapeRelation.Intersects)
)

Object Initializer syntax example

edit
new ShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.ArbitraryShape),
    Shape = new PointGeoShape(PointCoordinates),
    Relation = ShapeRelation.Intersects
}

Example json output.

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

Querying with MultiPoint

edit

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

Fluent DSL example

edit
q
.Shape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.ArbitraryShape)
    .Shape(s => s
        .MultiPoint(MultiPointCoordinates)
    )
    .Relation(ShapeRelation.Intersects)
)

Object Initializer syntax example

edit
new ShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.ArbitraryShape),
    Shape = new MultiPointGeoShape(MultiPointCoordinates),
    Relation = ShapeRelation.Intersects,
}

Example json output.

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

Querying with LineString

edit

Fluent DSL example

edit
q
.Shape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.ArbitraryShape)
    .Shape(s => s
        .LineString(LineStringCoordinates)
    )
    .Relation(ShapeRelation.Intersects)
)

Object Initializer syntax example

edit
new ShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.ArbitraryShape),
    Shape = new LineStringGeoShape(LineStringCoordinates),
    Relation = ShapeRelation.Intersects,
}

Example json output.

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

Querying with MultiLineString

edit

Fluent DSL example

edit
q
.Shape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.ArbitraryShape)
    .Shape(s => s
        .MultiLineString(MultiLineStringCoordinates)
    )
    .Relation(ShapeRelation.Intersects)
)

Object Initializer syntax example

edit
new ShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.ArbitraryShape),
    Shape = new MultiLineStringGeoShape(MultiLineStringCoordinates),
    Relation = ShapeRelation.Intersects,
}

Example json output.

{
  "shape": {
    "_name": "named_query",
    "boost": 1.1,
    "arbitraryShape": {
      "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 Polygon

edit

Fluent DSL example

edit
q
.Shape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.ArbitraryShape)
    .Shape(s => s
        .Polygon(PolygonCoordinates)
    )
    .IgnoreUnmapped()
    .Relation(ShapeRelation.Intersects)
)

Object Initializer syntax example

edit
new ShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.ArbitraryShape),
    Shape = new PolygonGeoShape(PolygonCoordinates),
    IgnoreUnmapped = true,
    Relation = ShapeRelation.Intersects,
}

Example json output.

{
  "shape": {
    "_name": "named_query",
    "boost": 1.1,
    "ignore_unmapped": true,
    "arbitraryShape": {
      "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 MultiPolygon

edit

Fluent DSL example

edit
q
.Shape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.ArbitraryShape)
    .Shape(s => s
        .MultiPolygon(MultiPolygonCoordinates)
    )
    .Relation(ShapeRelation.Intersects)
)

Object Initializer syntax example

edit
new ShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.ArbitraryShape),
    Shape = new MultiPolygonGeoShape(MultiPolygonCoordinates),
    Relation = ShapeRelation.Intersects,
}

Example json output.

{
  "shape": {
    "_name": "named_query",
    "boost": 1.1,
    "arbitraryShape": {
      "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 GeometryCollection

edit

Fluent DSL example

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

Object Initializer syntax example

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

Example json output.

{
  "shape": {
    "_name": "named_query",
    "boost": 1.1,
    "arbitraryShape": {
      "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 Envelope

edit

Fluent DSL example

edit
q
.Shape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.ArbitraryShape)
    .Shape(s => s
        .Envelope(EnvelopeCoordinates)
    )
    .Relation(ShapeRelation.Intersects)
)

Object Initializer syntax example

edit
new ShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.ArbitraryShape),
    Shape = new EnvelopeGeoShape(EnvelopeCoordinates),
    Relation = ShapeRelation.Intersects,
}

Example json output.

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

Querying with Circle

edit

Available in Elasticsearch 7.7.0+

Fluent DSL example

edit
q
.Shape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.ArbitraryShape)
    .Shape(s => s
        .Circle(CircleCoordinates, "100m")
    )
    .Relation(ShapeRelation.Intersects)
)

Object Initializer syntax example

edit
new ShapeQuery
{
    Name = "named_query",
    Boost = 1.1,
    Field = Infer.Field<Project>(p => p.ArbitraryShape),
    Shape = new CircleGeoShape(CircleCoordinates, "100m"),
    Relation = ShapeRelation.Intersects,
}

Example json output.

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

Querying with an indexed shape

edit

The Query also supports using a shape which has already been indexed in another index. 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 each time. In this situation it is only necessary to provide:

See the Elasticsearch documentation on https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-shape-query.html for more detail.

Fluent DSL example

edit
q
.Shape(c => c
    .Name("named_query")
    .Boost(1.1)
    .Field(p => p.ArbitraryShape)
    .IndexedShape(p => p
        .Id(Project.Instance.Name)
        .Path(pp => pp.ArbitraryShape)
        .Routing(Project.Instance.Name)
    )
    .Relation(ShapeRelation.Intersects)
)

Object Initializer syntax example

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

Example json output.

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