c# - 在 Azure 搜索中存储 Edm.GeographyPoint 类型
问题描述
嗨,我正在尝试通过Kentico CustomAzureSearchModule在 Azure 搜索索引中创建 Edm.GeographyPoint 项。这是在 Kentico 中的索引重建时激活的。我不断收到错误消息为空间属性指定的值无效。您必须指定一个有效的空间值。. 我的代码如下:
public class CustomAzureSearchModule : Module
{
private string nodeguid = "";
public CustomAzureSearchModule() : base(nameof(CustomAzureSearchModule))
{
}
protected override void OnInit()
{
base.OnInit();
DataMapper.Instance.RegisterMapping(typeof(GeographyPoint), Microsoft.Azure.Search.Models.DataType.GeographyPoint);
DocumentFieldCreator.Instance.CreatingField.After += CreatingField_After;
DocumentCreator.Instance.AddingDocumentValue.Execute += AddingValue;
}
private void CreatingField_After(object sender, CreateFieldEventArgs e)
{
if (e.SearchField.FieldName == "GeoLocation")
{
//Change the field type to Edm.GeographyPoint for Azure Search
e.Field = new Microsoft.Azure.Search.Models.Field("geolocation", Microsoft.Azure.Search.Models.DataType.GeographyPoint);
}
}
private void AddingValue(object sender, AddDocumentValueEventArgs e)
{
if (e.Document.ContainsKey("nodeguid"))
{
nodeguid = e.Document["nodeguid"].ToString(); //Store NodeGuid
}
//}
if (e.AzureName == "geolocation")
{
//Collect nodeGuid and use to get page
TreeNode page = DocumentHelper.GetDocuments()
.WhereEquals("NodeGUID", nodeguid)
.OnCurrentSite()
.Culture("en-gb")
.TopN(1)
.FirstOrDefault();
if (page != null)
{
// Check page type is a service only
if (page.ClassName == ServicePage.CLASS_NAME)
{
//Check for Children
if (page.Children.Count > 0)
{
e.Value = GeographyPoint.Create(31.8, -5); //Add location data to index
}
}
}
}
}
}
}
将不胜感激任何帮助。最终希望使用多个地理编码在 Azure 索引中创建 Collection(EDM.GeographyPoint) 类型。按照这篇文章生成我的代码https://devnet.kentico.com/articles/customizing-azure-search-fields
解决方案
您的代码实际上看起来是正确的,但是您使用的是哪个版本的 Kentico 11 或 12,以及您使用的是哪个版本的 Azure 搜索 API?您是否尝试将坐标值转换为 Double 以确定?
另外,您是否添加了任何可能具有空值的 SearchDocuments?(喜欢if page == null
)?如果您只是在e.Value = GeographyPoint.Create(31.8, -5)
每次迭代该代码时进行设置以查看索引是否有效,会发生什么情况。
推荐阅读
- c# - 在 C# 中反序列化自定义 JSON
- ios - 如何防止 VoiceOver 在 iOS 上的 SwiftUI 文本视图中读取文本?
- python - 对象没有“get_category_display”
- python - 如何在训练期间在每个时期修改损失函数内的变量?
- discord.js - 无效的位域标志或数字:未定义
- reactjs - 简单的对等视频流未在近距离视频通话 webapp 中播放
- apache-spark - 执行合并操作时 databricks 增量表的性能改进 (upsert)
- reactjs - 在使用地图创建的特定 div 上添加类
- data-modeling - 维度建模事件层次结构
- api - 如何根据 window.location.protocol 设置 http 或 https