azure 认知搜索 - 地理点问题

azure cognitive search - geography point problem

我指定一个字段如下:

    [SimpleField(IsFilterable = true, IsSortable = true)]
    public  Microsoft.Spatial.GeographyPoint Location { get; set; }

在我的索引中,我可以看到它已成功创建并且内容正确,但是当我尝试使用 geo.distance 进行搜索时,它会抛出以下错误:

$filter=geo.distance(Location, geography'POINT(-82.51571 31.89063)') le 30

错误:

“无效的表达式:名称为 'geo.distance' 的函数没有函数签名与指定的参数匹配。考虑的函数签名是:geo.distance(Edm.GeographyPoint Nullable=true, Edm.GeographyPoint 可空=真); geo.distance(Edm.GeometryPoint 可空=真, Edm.GeometryPoint 可空=真).\r\nParameter 名称: $过滤器"

Azure SDK 致力于综合空间类型以在服务之间共享。目前,需要一个单独的包来支持 Microsoft.Spatial。如果您使用 System.Text.Json(与“Azure.*”匹配的 Azure SDK 包的默认设置),请使用 https://www.nuget.org/packages/Microsoft.Azure.Core.Spatial/1.0.0-beta.1. If you're using Json.NET (i.e. Newtonsoft.Json), use https://www.nuget.org/packages/Microsoft.Azure.Core.Spatial.NewtonsoftJson/1.0.0-beta.1.

后者见https://github.com/Azure/azure-sdk-for-net/blob/Microsoft.Azure.Core.Spatial_1.0.0-beta.1/sdk/core/Microsoft.Azure.Core.Spatial.NewtonsoftJson/README.md for an example for how to use the former, and https://github.com/Azure/azure-sdk-for-net/blob/Microsoft.Azure.Core.Spatial.NewtonsoftJson_1.0.0-beta.1/sdk/core/Microsoft.Azure.Core.Spatial.NewtonsoftJson/README.md

您需要使用它们来生成 SearchIndex 并重新发布,以便空间 OData 过滤器正常工作。

对您发送的源进行一些修改(没有资源名称和 API 键 - 使用环境变量是个好主意,即使这些资源是临时的),您将使用这样的东西:

            Uri serviceEndpoint = new Uri($"https://{serviceName}.search.windows.net/");
            var credential = new AzureKeyCredential(apiKey);

            JsonSerializerOptions serializerOptions = new JsonSerializerOptions
            {
                Converters =
                {
                    new MicrosoftSpatialGeoJsonConverter()
                },
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            };
            SearchClientOptions clientOptions = new SearchClientOptions
            {
                Serializer = new JsonObjectSerializer(serializerOptions)
            };

            var adminClient = new SearchIndexClient(serviceEndpoint, credential, clientOptions);
            var searchClient = new SearchClient(serviceEndpoint, indexName, credential, clientOptions);

            FieldBuilder fieldBuilder = new FieldBuilder
            {
                Serializer = clientOptions.Serializer
            };

            var definition = new SearchIndex(indexName)
            {
                Fields = fieldBuilder.Build(typeof(Sample))
            };

            adminClient.CreateOrUpdateIndex(definition);

            IndexDocumentsBatch<Sample> batch = IndexDocumentsBatch.Create(
                new IndexDocumentsAction<Sample>(IndexActionType.MergeOrUpload, new Sample { Id = "1", Location = GeographyPoint.Create(0, 0) }
            ));

            try
            {
                IndexDocumentsResult result = searchClient.IndexDocuments(batch);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
                // If for some reason any documents are dropped during indexing, you can compensate by delaying and
                // retrying. This simple demo just logs the failed document keys and continues.
                Console.WriteLine("Failed to index some of the documents: {0}");
            }

            Console.WriteLine("Hello World!");

你的模特:

    public class Sample
    {
        [SimpleField(IsKey = true, IsFilterable = true, IsSortable = true)]
        public string Id { get; set; }

        [SimpleField(IsFilterable = true, IsSortable = true)]
        public GeographyPoint Location { get; set; }
    }

即使您最初没有使用 FieldBuilder,您也为字段指定了驼峰命名法,但使用 PascalCase 声明了这些字段。请注意,Azure 认知搜索区分大小写,包括字段名称。