Kentico 无法将 EDM.GeographyPoint 类型设置为可过滤 n Azure 索引

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

GeographyPoint*类型为可过滤,这样我就可以在Azure中进行近似搜索。当我在Kentico中构建索引时,我使用的是 CustomAzureSearchModule 中的数据类型从EDM.String改为EDM.GeographyPoint。DocumentFieldCreator.Instance.CreatingField.After.DocumentFieldCreator。 事件。该字段在kentico页面构建器搜索字段中定义为Retrievable,Searchable,Filterable。当构建索引时,Azure类型被设置为EDM.GeographyPoint,但它没有被设置为可过滤.它需要在Azure搜索索引中可过滤,否则像$filter=geo.distance(cafegeolocation, geography'POINT(-71.071138 42.300101)') le 300这样的近似搜索无法工作,并抛出Azure错误。"无效表达式:'geolocation2'不是可过滤字段。只有可过滤的字段才可用于过滤表达式.参数名称:$filter"

下面是代码。

[汇编:RegisterModule(typeof(CustomAzureSearchModule))]

namespace Nas.KenticoSites.Domain.GlobalEventModules{ 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);
        DocumentCreator.Instance.AddingDocumentValue.Execute += AddingValue;

        // Assigns a handler to the CreatingField.After event for Azure Search indexes
        DocumentFieldCreator.Instance.CreatingField.After += CreatingLocationField_After;
    }

    private void CreatingLocationField_After(object sender, CreateFieldEventArgs e)
    {
        if (e.SearchField.FieldName == "GeoLocation2")
        {
            //Change the field type to Edm.GeographyPoint for Azure Search
            e.Field = new Microsoft.Azure.Search.Models.Field("geolocation2", Microsoft.Azure.Search.Models.DataType.GeographyPoint);
        }
    }

    private void AddingValue(object sender, AddDocumentValueEventArgs e)
    {
        if (e.Document.ContainsKey("nodeguid"))
        {
            nodeguid = e.Document["nodeguid"].ToString();
        }

        if (e.AzureName == "geolocation2")
        {
            //Collect nodeGuid and use to get page
            TreeNode page = DocumentHelper.GetDocuments()
                      .WhereEquals("NodeGUID", nodeguid)
                      .OnCurrentSite()
                      .Culture("en-gb")
                      .TopN(1)
                      .FirstOrDefault();

            if (page != null)
            {
                if (page.ClassName == ServicePage.CLASS_NAME) // Check page type is a service only
                {
                    if (page.Children.Count > 0)
                    {
                        foreach (TreeNode location in page.Children.WithAllData.Where(n => n.ClassName == Location.CLASS_NAME).ToList())
                        {
                            Location locationPage = (Location)location;

                            e.Value = GeographyPoint.Create(Convert.ToDouble(locationPage.Latitude), Convert.ToDouble(locationPage.Longitude));
                        }
                    }
                }
            }
        }
    }
}

}

从这个例子来看 https:/devnet.kentico.comarticlescustomizing-azure-search-fields。

c# asp.net-mvc azure kentico
1个回答
1
投票

你可以在你的 azure 搜索索引中检查该字段是否可过滤?

enter image description here

如果您的'GeoLocation2'字段没有选中可过滤复选框,您可以尝试在'CreatingLocationField_After'方法中添加以下代码。

if (e.SearchField.FieldName == "GeoLocation2")
{
    //Change the field type to Edm.GeographyPoint for Azure Search
    e.Field = new Microsoft.Azure.Search.Models.Field("geolocation2", Microsoft.Azure.Search.Models.DataType.GeographyPoint);

    e.Field.IsFilterable = true;
}

这将使该字段在 azure 搜索中可被过滤,然后你的查询就可以了。

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