在c#代码中定义AzureSearch索引字段映射

时间:2016-07-24 02:48:48

标签: azure search indexer azure-search

我想在C#中以编程方式定义JSON对象和Azure搜索索引之间的字段映射。我在Javascript这里找到了关于如何执行此操作的说明https://azure.microsoft.com/en-us/documentation/articles/search-howto-index-json-blobs/

 "fieldMappings" : [ 
{ "sourceFieldName" : "/article/text", "targetFieldName" : "text" },
{ "sourceFieldName" : "/article/datePublished", "targetFieldName" : "date" },
{ "sourceFieldName" : "/article/tags", "targetFieldName" : "tags" }
]

但是我需要c#等效代码。到目前为止我已尝试过:

      var index = new
      {
           name = "testindex",
           fields = new []
           { 
              //...
           },
           fieldMappings = new[]
           {
              new { sourceFieldName = "/status/text", targetFieldName = "text"}
           }
      }

但此代码崩溃并出现异常

{"Azure Search request failed:{\"error\":{\"code\":\"\",\"message\":\"The request is invalid. Details: index : The property 'fieldMappings' does not exist on type 'Microsoft.Azure.Search.V2015_02_28.IndexDefinition'. Make sure to only use property names that are defined by the type.\\r\\n\"}}"}

有关如何以编程方式在C#代码中定义这些字段映射的任何建议?另外,这些fieldMappings应该在索引定义的索引器中定义吗?谢谢!

@LaterEdit:从这篇文章Creating Collection in Azure Search Service using Indexer我发现这些映射必须在索引器上定义,但是如果我在定义索引器时添加它们,则属性fieldMappings不存在。< / p>

  var indexer = new
  {
      name = "textixr",
      dataSourceName = "testsdocdb",
      schedule = new { interval = "PT5M" }, // every 5 minutes
      targetIndexName = "test",
      fieldMappings = new []
      {
          new { sourceFieldName = "/status/text", targetFieldName = "text" }
      }
  };

1 个答案:

答案 0 :(得分:2)

您需要使用api-version 2015-02-28- 预览。 2015-02-28不提供字段映射。

顺便说一下,只有在使用blob索引器索引JSON blob时,才能使用带有JSON指针的字段映射(例如/status/text)。它们不能与DocumentDB索引器一起使用。要使用DocumentDB投影嵌套属性,请在数据源定义的container.query属性中使用类似SQL的查询。有关DocumentDB索引器的更多信息,请查看this article