【问题标题】:Define AzureSearch index fieldmappings in c# code在 C# 代码中定义 AzureSearch 索引字段映射
【发布时间】:2016-11-27 15:29:45
【问题描述】:

我想在 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是否应该在索引定义的indexer里面定义呢?谢谢!

@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" }
      }
  };

【问题讨论】:

    标签: azure search indexer azure-cognitive-search


    【解决方案1】:

    您需要使用 api-version 2015-02-28-Preview。字段映射在 2015-02-28 中不可用。

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

    【讨论】:

    • 感谢您的回复。 2015-02-28-Preview 没有给出错误。但是,我不确定如何在 container.query 属性中使用类似 Sql 的查询。你能给我举个例子,或者给我指一篇提到这个的文章吗?
    • 您需要在定义数据源以展平文档时使用 SQL 查询,例如 SELECT c.id, c._ts, c.status.text AS text FROM集合 c WHERE c._ts >= @HighWaterMark。我链接到的文章有示例。
    • 谢谢!我目前正在 C# 中以编程方式创建 DocumentDb 数据库、AzureSearch 索引器和索引器,有没有办法在 C# 中调用此语法?
    • 您是指使用 .NET SDK 为数据源指定查询吗?是的,使用DataContainer.Query 属性:dataSource.Container.Query = "a query"。
    猜你喜欢
    • 1970-01-01
    • 2012-02-20
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多