You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Cognitive Search如何正确索引Blob中的复杂JSON数据?

Azure Cognitive Search 索引嵌套JSON文档解决方案

核心问题分析

你的JSON文档是嵌套数组结构,默认Blob索引器会将整个文件视为单个文档,且无法自动展开metadata键值对和嵌套的alternateQuestions。需要通过调整索引器解析模式、重构索引结构、配置字段映射来实现需求。


1. 调整索引器的JSON解析规则

让索引器识别itemSet中的每个对象为独立搜索文档,需设置解析模式为jsonArray并指定文档根路径:

  • 如果使用导入向导:在「配置索引器」步骤的「高级选项」中,选择解析模式:JSON数组,并填写文档根路径:/itemSet
  • 如果使用REST API创建索引器,核心配置片段:
{
  "name": "blob-indexer",
  "dataSourceName": "你的Blob数据源名称",
  "targetIndexName": "azureblob-index",
  "parsingMode": "jsonArray",
  "documentRoot": "/itemSet",
  "fieldMappings": [] // 后续添加字段映射规则
}

2. 重构索引结构(替换现有索引)

删除当前索引,重新创建包含以下字段的索引,覆盖你的需求:

{
  "name": "azureblob-index",
  "fields": [
    // 用metadata中Name="id"的Value作为文档主键
    {
      "name": "document_id",
      "type": "Edm.String",
      "key": true,
      "retrievable": true,
      "filterable": true
    },
    // 原answer字段,支持全文搜索
    {
      "name": "answer",
      "type": "Edm.String",
      "searchable": true,
      "filterable": true,
      "retrievable": true,
      "analyzer": "standard.lucene"
    },
    // questions数组,存为可搜索的字符串集合
    {
      "name": "questions",
      "type": "Edm.Collection(Edm.String)",
      "searchable": true,
      "filterable": true,
      "retrievable": true
    },
    // alternateQuestions提取question文本,存为可搜索集合
    {
      "name": "alternate_questions",
      "type": "Edm.Collection(Edm.String)",
      "searchable": true,
      "filterable": true,
      "retrievable": true
    },
    // metadata中的people字段,支持过滤和分面
    {
      "name": "people",
      "type": "Edm.String",
      "filterable": true,
      "facetable": true,
      "retrievable": true
    },
    // metadata中的linkUris字段
    {
      "name": "link_uris",
      "type": "Edm.String",
      "filterable": true,
      "retrievable": true
    },
    // 保留存储路径元数据(可选)
    {
      "name": "metadata_storage_path",
      "type": "Edm.String",
      "retrievable": true
    }
  ],
  "similarity": {
    "@odata.type": "#Microsoft.Azure.Search.BM25Similarity"
  }
}

3. 配置索引器的字段映射

使用jsonpath函数从嵌套结构中提取对应值,添加到索引器的fieldMappings数组:

"fieldMappings": [
  // 提取metadata中Name="id"的Value到主键字段
  {
    "sourceFieldName": "/metadata[?(@.Name == 'id')].Value",
    "targetFieldName": "document_id",
    "mappingFunction": {
      "name": "jsonpath",
      "parameters": { "path": "$[0]" } // 取第一个匹配项(每个item的metadata中id唯一)
    }
  },
  // 提取metadata中Name="people"的Value
  {
    "sourceFieldName": "/metadata[?(@.Name == 'people')].Value",
    "targetFieldName": "people",
    "mappingFunction": {
      "name": "jsonpath",
      "parameters": { "path": "$[0]" }
    }
  },
  // 提取metadata中Name="linkUris"的Value
  {
    "sourceFieldName": "/metadata[?(@.Name == 'linkUris')].Value",
    "targetFieldName": "link_uris",
    "mappingFunction": {
      "name": "jsonpath",
      "parameters": { "path": "$[0]" }
    }
  },
  // 提取所有alternateQuestions的question文本到集合字段
  {
    "sourceFieldName": "/learningBits/alternateQuestions/*/question",
    "targetFieldName": "alternate_questions",
    "mappingFunction": {
      "name": "jsonpath",
      "parameters": { "path": "$" } // 取所有匹配的数组元素
    }
  },
  // 直接映射questions数组
  {
    "sourceFieldName": "questions",
    "targetFieldName": "questions"
  },
  // 直接映射answer字段
  {
    "sourceFieldName": "answer",
    "targetFieldName": "answer"
  }
]

4. 验证效果

保存索引器配置后,手动触发索引运行,通过以下测试确认结果:

  • 搜索alternate_questions:"alt 1?",验证是否能返回对应文档
  • 过滤people:person1,验证metadata字段是否生效
  • 查看搜索结果,确认每个itemSet中的对象都成为独立的搜索文档

内容的提问来源于stack exchange,提问作者Snowy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.07 23:28:09