dotnet | azure-cognitive-search

Azure Cognitive Search - GeographyPoint to GeoJSON in .NET 5

Microsoft.Spatial GeographyPoint to GeoJSON in .NET5 using System.Text.Json.

Abhith RajanMarch 13, 2021 · 2 min read · Last Updated:

Azure Cognitive Search Service supported data types includes Edm.GeographyPoint.

While creating index definition using attributes in C#, to specify the field type as Edm.GeographyPoint we can use the GeographyPoint as the type.

1using Azure.Search.Documents.Indexes;
2using Microsoft.Spatial;
3
4public class RestaurantIndexModel
5{
6 ...
7
8 [SimpleField(IsFilterable = true, IsSortable = true)]
9 public GeographyPoint Location { get; set; }
10
11 ...
12}

We need to have the NuGet package Microsoft.Spatial to use the GeographyPoint type.

1<PackageReference Include="Microsoft.Spatial" Version="7.8.3" />

And with that, you can create the index definition,

Index definition
Index definition

But when you try to index a document with the same type,

1restaurant.Location = GeographyPoint.Create(restaurantResult.Value.Latitude, restaurantResult.Value.Longitude);

You may encounter the following issue,

{“error”:{“code”:"",“message”:“The request is invalid. Details: parameters : Invalid GeoJSON. The ‘type’ member is required, but was not found.\r\n”}}

The way you can fix this is by,

  1. Add the following NuGet package,
1<PackageReference Include="Microsoft.Azure.Core.Spatial" Version="1.0.0" />

And then annotate your index model to use the MicrosoftSpatialGeoJsonConverter as the JsonConverter by,

1using System.Text.Json.Serialization;
2using Azure.Core.Serialization;
3using Azure.Search.Documents.Indexes;
4using Microsoft.Spatial;
5
6public class RestaurantIndexModel
7{
8 ...
9
10 [JsonConverter(typeof(MicrosoftSpatialGeoJsonConverter))]
11 [SimpleField(IsFilterable = true, IsSortable = true)]
12 public GeographyPoint Location { get; set; }
13
14 ...
15}

That will do.

Indexd document
Indexd document

Additional Resources

Written by Abhith Rajan
Abhith Rajan is an aspiring software engineer with more than 8 years of experience and proven successful track record of delivering technology-based products and services.
Buy me a coffee

Was this helpful?

Show some ❤️

Share this page on Twitter to appear on the webmentions* 💡

This page is open source. Noticed a typo? Or something unclear?
Improve this page on GitHub

Related VideosView All

Machine Learning for .NET Developers Introducing ML NET

Azure Search Tutorial

Azure Cognitive Search | Build semantic search into your apps

Related StoriesView All