问题描述
使用AI Search中的索引器(Indexer), 需要把storage account blob的文件属性中的base64编码的内容进行解码,但是在执行索引器的时候,却无法成功
Indexer执行失败截图

Indexer的配置如下:
...
{
"sourceFieldName": "test_content_code",
"targetFieldName": "test_content_code",
"mappingFunction": {
"name": "base64Decode",
"parameters": null
}
}
...
错误信息
Value cannot be null. (Parameter 'bytes')
Could not parse document. Could not apply mapping function 'base64Decode' to field 'test_content_code'.
这个问题应该如何解决呢?
问题解答
查看AI Search关于索引器的官方文档,找到了对于base64编码的介绍。
Azure AI 搜索支持 URL 安全的 Base64 编码和普通 Base64 编码,在编码和解码的时候需要保持参数一致。
- 编码时使用:useHttpServerUtilityUrlTokenEncode
- 解码时使用:useHttpServerUtilityUrlTokenDecode
默认状态useHttpServerUtilityUrlTokenEncode 和 useHttpServerUtilityUrlTokenDecode 都设置为 true。
在本文的问题中,文件属性 test_content_code 的值为普遍 base64编码内容,当使用默认的url 解码方法解码时,就会报错。
解决方法就是在索引器中为base64Decode函数添加useHttpServerUtilityUrlTokenDecode 参数并设置为false
{
"sourceFieldName": "test_content_code",
"targetFieldName": "test_content_code",
"mappingFunction": {
"name": "base64Decode",
"parameters": {
"useHttpServerUtilityUrlTokenDecode": false
}
}
}
注意:因为索引器对文档的扫描是增量扫描。如果需要它对同一文件进行多次扫描,需要对该文件进行更新操作。
等待索引器执行成功后,再次查询,就可以得到解码后的真实内容:

基于验证,在添加useHttpServerUtilityUrlTokenDecode参数后,成功解决问题!
参考资料
base64Decode function : https://learn.microsoft.com/en-us/azure/search/search-indexer-field-mappings?tabs=rest#base64decode-function