修改Azure Cognitive Search索引:网站搜索功能异常求助
解决Azure Cognitive Search仅返回计数和分数、索引JSON修改丢失的问题
一、修复索引JSON修改后丢失的问题
- 不要直接在Azure门户的可视化索引编辑器里修改JSON,编辑器会覆盖手动修改内容。正确流程:
- 用Azure CLI导出当前索引定义到本地文件:
az search index show --name your-index-name --resource-group your-rg --service-name your-search-service --output json > index-definition.json - 在本地编辑器(如VS Code)中修改
index-definition.json,确保配置好需要返回的字段及属性 - 用CLI重新覆盖更新索引:
az search index create --name your-index-name --resource-group your-rg --service-name your-search-service --file index-definition.json --overwrite
- 用Azure CLI导出当前索引定义到本地文件:
二、配置索引与索引器,实现返回段落/标题及页面链接
1. 定义索引所需字段
索引必须包含可检索、可搜索的目标字段,示例JSON:
{ "name": "your-index-name", "fields": [ { "name": "id", "type": "Edm.String", "key": true, "retrievable": true }, { "name": "pageUrl", "type": "Edm.String", "retrievable": true }, { "name": "title", "type": "Edm.String", "retrievable": true, "searchable": true }, { "name": "content", "type": "Edm.String", "retrievable": true, "searchable": true }, { "name": "@search.score", "type": "Edm.Double", "retrievable": true } ] }
pageUrl:存储对应网站页面的链接title:提取自HTML页面的标题content:HTML解析后的文本内容,需设retrievable: true才能返回
2. 配置Blob索引器解析HTML并映射字段
索引器需启用HTML解析规则,将Blob元数据/内容映射到索引字段:
{ "name": "your-blob-indexer", "dataSourceName": "your-blob-datasource", "targetIndexName": "your-index-name", "parameters": { "configuration": { "parsingMode": "html", "indexedFileNameExtensions": ".html", "metadataStoragePath": "full" } }, "fieldMappings": [ { "sourceFieldName": "metadata_storage_path", "targetFieldName": "pageUrl", "mappingFunction": { "name": "replace", "parameters": { "pattern": "https://your-storage-account.blob.core.windows.net/your-container/", "replacement": "https://your-website.com/" } } }, { "sourceFieldName": "metadata_title", "targetFieldName": "title" }, { "sourceFieldName": "content", "targetFieldName": "content" } ] }
parsingMode: html:让索引器自动解析HTML提取文本和元数据replace映射函数:将Blob存储路径替换为实际网站页面URL
3. 调用搜索API时指定返回内容
发起搜索请求时,必须通过select参数指定要返回的字段,如需高亮关键词片段,可搭配highlight参数:
{ "search": "your-keyword", "select": "pageUrl,title", "highlight": "content", "highlightPreTag": "<mark>", "highlightPostTag": "</mark>", "top": 10 }
三、常见问题排查
- 字段未返回:检查索引字段的
retrievable是否设为true,且搜索请求的select参数包含该字段 - HTML标题未提取:确认HTML文件包含
<title>标签,若索引器无法自动抓取,可添加技能集从HTML内容中提取标题 - 页面链接映射错误:调整
replace函数的pattern和replacement参数,匹配你的存储路径与网站URL规则
内容的提问来源于stack exchange,提问作者Jacob Gallant
相关产品推荐
相关产品推荐

