在Azure搜索中存储Edm.GeographyPoint类型

问题描述 投票:0回答:1

您正在尝试通过Kentico CustomAzureSearchModule]在Azure搜索索引中创建Edm.GeographyPoint项目。这在Kentico中的索引重建上被激活。我不断收到错误消息为spatial属性指定的值无效。您必须指定一个有效的空间值。。我的代码如下:

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 CustomAzureSearchModule在Azure搜索索引中创建Edm.GeographyPoint项目。这是在Kentico中重建索引时激活的。我不断收到错误消息...

c# azure kentico kentico-12 kentico-mvc
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.