0

Hi im trying to create a Edm.GeographyPoint item in an Azure Search Index via the Kentico CustomAzureSearchModule.This is activated on an index rebuild in Kentico. I keep getting the error message The value specified for the spatial property was not valid. You must specify a valid spatial value.. My code is as follows:

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
                    }
                }
            }
        }
    }
}

}

Would appreciate any help. Would eventually like to create Collection(EDM.GeographyPoint) type in the Azure Index with multiple geocodes. Followed this article to produce my code https://devnet.kentico.com/articles/customizing-azure-search-fields

  • Your code looks correct actually, but which version of Kentico are you using 11 or 12 and which version of the Azure Search API are you using ? Have you tried to cast the coord values to a Double to just be sure ? – Mcbeev May 04 '20 at 16:26

1 Answers1

0

Your code looks correct actually, but which version of Kentico are you using 11 or 12, and which version of the Azure Search API are you using ? Have you tried to cast the coord values to a Double to just be sure ?

Also, are you adding any SearchDocuments with a possible null value? (Like if page == null) ? What would happen if you just set e.Value = GeographyPoint.Create(31.8, -5) every time it iterates through that code just to see if the indexing works.

Mcbeev
  • 1,519
  • 9
  • 9
  • Ok i managed to get it working. This is what i found. The c# code was correct. The issue was that i had put a Default value for the field in the Service Page page type, and had not put a field caption and field description. When i removed the default value and added the caption and description the code worked. – Charles Leek May 05 '20 at 08:59