azure - 无法使用 REST API 将 cosmos db 的嵌套数据源字段映射到 Azure 索引器的根索引字段

标签 azure azure-cosmosdb azure-cognitive-search

我有一个 mongo 数据库集合users,具有以下数据格式

{
    "name": "abc",
    "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fa9b9899ba828380d4999597" rel="noreferrer noopener nofollow">[email protected]</a>"
    "address": {
        "city": "Gurgaon",
        "state": "Haryana"
    }
}

现在,我正在使用 Azure Rest api 为此集合创建数据源、索引和索引器。

数据源

def create_datasource():
  request_body = {
      "name": 'users-datasource',
      "description": "",
      "type": "cosmosdb",
      "credentials": {
          "connectionString": "<db conenction url>"
      },
      "container": {"name": "users"},
      "dataChangeDetectionPolicy": {
          "@odata.type": "#Microsoft.Azure.Search.HighWaterMarkChangeDetectionPolicy",
          "highWaterMarkColumnName": "_ts"
      }
  }
  resp = requests.post(url="<create-datasource-api-url>", data=json.dumps(request_body), 
    headers=headers)

上述数据源的索引

def create_index(config):

  request_body = {
      'name': "users-index",
      'fields': [
          {
              'name': 'name',
              'type': 'Edm.String'
          },
          {
              'name': 'email',
              'type': 'Edm.DateTimeOffset'
          },
          {
              'name': 'address',
              'type': 'Edm.String'
          },
          {
              'name': 'doc_id',
              'type': 'Edm.String',
              'key': True
          }
      ]
  }
  resp = requests.post(url="<azure-create-index-api-url>", data=json.dumps(request_body), 
    headers=config.headers)

现在是上述数据源和索引的索引器

def create_interviews_indexer(config):
  request_body = {
    "name": "users-indexer",
    "dataSourceName": "users-datasource",
    "targetIndexName": users-index,
    "schedule": {"interval": "PT5M"},
    "fieldMappings": [
        {"sourceFieldName": "address.city", "targetFieldName": "address"},
    ]
  }
  resp = requests.post("create-indexer-pi-url", data=json.dumps(request_body), 
      headers=config.headers)

这会创建索引器,没有任何异常,但是当我在 Azure 门户中检查 users-indexer 中检索到的数据时,address 字段为 null 并且没有从创建索引器时提供的 address.city 字段映射中获取任何值。

我还尝试了以下代码作为映射,但它也不起作用。

"fieldMappings": [
        {"sourceFieldName": "/address/city", "targetFieldName": "address"},
    ]

Azure 文档也没有提及任何有关这种映射的内容。因此,如果有人能在这方面帮助我,我将非常感激。

最佳答案

数据源定义中的

container 元素允许您指定可用于展平 JSON 文档的 query (引用: https://learn.microsoft.com/en-us/rest/api/searchservice/create-data-source ),而不是执行列映射在索引器定义中,您可以编写查询并以所需格式获取输出。

在这种情况下创建数据源的代码是:

def create_datasource():
  request_body = {
      "name": 'users-datasource',
      "description": "",
      "type": "cosmosdb",
      "credentials": {
          "connectionString": "<db conenction url>",
      },
      "container": {
        "name": "users",
        "query": "SELECT a.name, a.email, a.address.city as address FROM a",
      },
      "dataChangeDetectionPolicy": {
          "@odata.type": "#Microsoft.Azure.Search.HighWaterMarkChangeDetectionPolicy",
          "highWaterMarkColumnName": "_ts"
      }
  }
  resp = requests.post(url="<create-datasource-api-url>", data=json.dumps(request_body), 
    headers=headers)

关于azure - 无法使用 REST API 将 cosmos db 的嵌套数据源字段映射到 Azure 索引器的根索引字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58264046/

相关文章:

node.js - nodejs + azure 函数应用程序 - 处理数据库连接的最佳实践?

azure - 当我将核心从 3.1 更新到 6 时,在 .net6 中找不到 TableClientConfiguration

c# - 查询表达式无效 cosmosdb

Azure Blob 索引器元数据字段、编码

azure-cognitive-search - Azure 搜索评分

Azure 数据工厂 - 如何禁用管道?

azure - Azure 函数中的内存缓存

list - 如何列出 Azure Blob 存储中的子目录

c# - 无法删除触发 Azure Function Blob 输入绑定(bind)的 Blob

node.js - QNA Maker 未返回精确匹配项