Azure搜索索引器无法从DocumentDB中的文档中检索GeographyPoint

时间:2016-08-19 16:38:22

标签: azure azure-cosmosdb azure-search

我在尝试索引存储在DocumentDb集合中的文档时遇到问题。

索引器的数据源是使用自定义SQL定义的,用于检索已更改的文档。我要索引的文档有一个名为LocationGP的属性,它是一个Microsoft.Spatial.GeographyPoint,并映射到索引的一个字段,其名称相同,并定义为DataType.GeographyPoint

尝试创建索引器时出现的错误是:

列' LocationGP'是JObject类型,与索引中的Edm.GeographyPoint类型字段不兼容

有什么想法吗?

这是DataSource的定义:

  return new DataSource()
                {
                    Name = "opportunities-datasource",
                    Container = new DataContainer()
                    {
                        Name = "Companies",
                        Query = @"SELECT    o.id,
                                            o.CompanyName,
                                            o.LocationGP,
                                            o.Location.CityName AS LocationCity,
                                            o.Location.StateName AS LocationState,
                                            o.Location.CountryName AS LocationCountry,
                                            o._ts
                                FROM Companies o WHERE o.DocType = 1 AND o._ts > @HighWaterMark"
                    },
                    Type = "documentdb",
                    Credentials = new DataSourceCredentials()
                    {
                        ConnectionString = String.Format("AccountEndpoint={0};AccountKey={1};Database=CompaniesDb", DocumentDbEndpointUri, DocumentDbPrimaryKey)
                    },
                    DataChangeDetectionPolicy = new HighWaterMarkChangeDetectionPolicy("_ts"),
                    DataDeletionDetectionPolicy = new SoftDeleteColumnDeletionDetectionPolicy("Status", "2")
                };

这是文件:

[{
    "id": "088e1e97-6d59-40ad-a9be-620fdc7938c7",
    "CompanyName": "Neptune",
    "LocationGP": {
      "Latitude": 39.8010482788086,
      "Longitude": -89.6436004638672,
      "IsEmpty": false,
      "Z": null,
      "M": null,
      "CoordinateSystem": {
        "EpsgId": 4326,
        "Id": "4326",
        "Name": "WGS84"
      }
    },
    "Location": {
      "CityName": "Springfield",
      "CountryName": "US",
      "StateName": "IL"
    },
    "Status": 1,
    "DocType": 1,
    "Timestamp": "2016-08-19T16:08:46.0481948Z",
    "_ts": 1471622922
  }]

3 个答案:

答案 0 :(得分:2)

以下是Azure Search上的另外两个文档,用于说明接受哪种类型的地理实例:

基本上它需要采用GeoJSON“Point”类型格式。

答案 1 :(得分:0)

问题是function isNaN(x){ return x !== x; } 的格式与Edm.GeographyPoint不同。

为了使它工作,我刚刚创建了一个名为Microsoft.Spatial.GeographyPoint的类:

EdmGeograpyPoint

然后我将LocationGP属性的类型替换为EdmLocation。

也许有一个更好的解决方案,但文档围绕这个主题混淆: https://azure.microsoft.com/en-us/documentation/articles/search-howto-dotnet-sdk/

答案 2 :(得分:0)

我对这个答案有点迟了,但我希望有人能从中受益。

我认为,解决此问题的最干净方法是继续使用“内置”类/框架,而不必创建自己的复杂类型。

我解决此问题的方法是使用一个利用Microsoft.Azure.Search.Serialization命名空间的JsonConverter。此名称空间具有GeographyPoint扩展,即'ReadGeoJsonPoint()'和'WriteJson()'扩展。

我做了以下事情:

 //    1. Create a JsonConverter
   using Microsoft.Azure.Search.Serialization;
   public class GeographyPointJsonConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(GeographyPoint);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return reader.ReadGeoJsonPoint();
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            writer.WriteGeoJsonPoint(value as GeographyPoint);
        }
    }

    //2. Added the convert to the property I was looking to index
    [JsonConverter(typeof(GeographyPointJsonConverter))]