gpt4 book ai didi

azure认知搜索-地理点问题

转载 作者:行者123 更新时间:2023-12-02 07:38:33 24 4
gpt4 key购买 nike

我指定一个字段,如下所示:

    [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 Nullable=true); geo.distance(Edm.GeometryPoint Nullable=true, Edm.GeometryPoint Nullable=true)。\r\n参数名称:$filter"

最佳答案

Azure SDK 正在研究综合空间类型以跨服务共享。目前,需要一个单独的包来支持 Microsoft.Spatial。如果使用 System.Text.Json(与“Azure.*”匹配的 Azure SDK 包的默认值),请使用 https://www.nuget.org/packages/Microsoft.Azure.Core.Spatial/1.0.0-beta.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 key - 使用环境变量是个好主意,即使这些资源是临时的),您可以使用如下内容:

            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 认知搜索区分大小写,包括字段名称。

关于azure认知搜索-地理点问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65064331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com