首页 > 解决方案 > 在 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

标签: c#azurekenticokentico-12kentico-mvc

解决方案


您的代码实际上看起来是正确的,但是您使用的是哪个版本的 Kentico 11 或 12,以及您使用的是哪个版本的 Azure 搜索 API?您是否尝试将坐标值转换为 Double 以确定?

另外,您是否添加了任何可能具有空值的 SearchDocuments?(喜欢if page == null)?如果您只是在e.Value = GeographyPoint.Create(31.8, -5) 每次迭代该代码时进行设置以查看索引是否有效,会发生什么情况。


推荐阅读