azure - 在 C# 代码中定义 AzureSearch 索引字段映射

标签 azure search indexer azure-cognitive-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# 代码中以编程方式定义这些字段映射,有什么建议吗?另外,这些 fieldMapping 是否应该在索引定义的索引器内定义?谢谢!

@LaterEdit:摘自本文Creating Collection in Azure Search Service using Indexer我发现这些映射必须在索引器上定义,但如果我在定义索引器时添加它们,属性 fieldMappings 仍然不存在。

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

最佳答案

您需要使用 api 版本 2015-02-28-预览版。字段映射在 2015 年 2 月 28 日不可用。

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

关于azure - 在 C# 代码中定义 AzureSearch 索引字段映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38548270/

相关文章:

node.js - Material-ui 官方 SSR 示例在生产服务器中不起作用

Azure函数-定时器未计划的调用原因

algorithm - 谷歌如何知道我输入的 redflower.jpg 我指的是红花?

C# Type as object with 索引器

c# - 索引器是否应该始终引用离散序列?

Azure Bot 服务与 Azure 逻辑应用程序通信 - 401 未经授权

azure - 创建可以处理多个队列的 Azure.Storage.Queues 客户端

search - 如何隐藏在 liferay 搜索容器中显示结果文本?

search - 波兰人寻找 sphinx ?

workflow - 当 Eclipse CDT 索引器卡住时我该怎么办?