azure认知搜索-地理点问题

Thi*_*dio 1 azure azure-cognitive-search azure-search-.net-sdk

我指定一个字段如下:

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

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

$filter=geo.distance(Location, geography'POINT(-82.51571 31.89063)') le 30
Run Code Online (Sandbox Code Playgroud)

错误:

“无效表达式:名称为“geo.distance”的函数没有与指定参数匹配的函数签名。考虑的函数签名为:geo.distance(Edm.GeographyPoint Nullable=true, Edm.GeographyPoint Nullable=true); geo.distance (Edm.GeometryPoint Nullable=true, Edm.GeometryPoint Nullable=true)。\r\n参数名称:$filter"

Hea*_*ath 5

Azure SDK 致力于开发综合空间类型以跨服务共享。目前,需要一个单独的包来支持 Microsoft.Spatial。如果使用 System.Text.Json(与“Azure.*”匹配的 Azure SDK 包的默认值),请使用https://www.nuget.org/packages/Microsoft.Azure.Core.Spatial/1.0.0-测试版.1。如果您使用 Json.NET(即 Newtonsoft.Json),请使用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有关如何使用前者的示例,以及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!");
Run Code Online (Sandbox Code Playgroud)

还有你的型号:

    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; }
    }
Run Code Online (Sandbox Code Playgroud)

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