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

Azure.Search.Documents向量搜索成功但返回结果属性为null求助

问题描述

我正按照《Vector Search Using Vectorized Query》的代码指南执行向量搜索。已创建索引、上传文档且能执行搜索,请求返回状态码200,但搜索结果的属性始终为null,不过在非公开成员的RawResponse->Content中可找到预期结果。自定义的AzureSearchDocument类属性与索引字段完全匹配,但怀疑响应未正确反序列化。

自定义类代码

public class AzureSearchDocument 
{ 
    public string Id { get; set; } 
    public string Title { get; set; }     
    public string Product { get; set; }     
    public ReadOnlyMemory<float> TextEmbedding { get; set; } 
}

搜索执行代码

SearchClient searchClient = new(new Uri(_azureSearchConfig.ApiUrl), _azureSearchConfig.IndexName, new AzureKeyCredential(_keyVaultService.GetSecret(KeyVaultKeys.Azure.AISearchApiKey)));

SearchResults<AzureSearchDocument> response = await searchClient.SearchAsync<AzureSearchDocument>(
    new SearchOptions
    {
        VectorSearch = new()
        {
            Queries = { new VectorizedQuery(questionEmbedding) { KNearestNeighborsCount = maxResults, Fields = { "textEmbedding" } } }
        },
        Filter = $"product eq '{product}'"
    });

int count = 0;
await foreach (SearchResult<AzureSearchDocument> result in response.GetResultsAsync())
{
    count++;
    AzureSearchDocument doc = result.Document;
    _logger.LogInformation($"[AzureSearchDocument] {doc.Id}: {doc.Title}");
}
_logger.LogInformation($"Total number of search results:{count}");
可行解决思路
  • 检查属性与索引字段的大小写匹配:Azure Search字段名区分大小写,确认类中属性名(如Id、Title)和索引字段名完全一致。可通过[JsonPropertyName]特性显式映射,示例:

    [JsonPropertyName("id")]
    public string Id { get; set; }
    
  • 显式指定返回字段:在SearchOptions中添加Select配置,明确列出需要返回的字段,确保这些字段被包含在响应中:

    new SearchOptions
    {
        Select = { "id", "title", "product", "textEmbedding" },
        // 其他向量搜索、过滤配置...
    }
    
  • 替换ReadOnlyMemory<float>类型:Azure SDK对ReadOnlyMemory<float>的反序列化可能存在兼容问题,尝试将TextEmbedding改为float[]类型,测试是否能正常反序列化:

    public float[] TextEmbedding { get; set; }
    
  • 调试原始响应的反序列化:手动读取并解析RawResponse的内容,排查反序列化异常:

    string rawContent = await response.GetRawResponse().Content.ReadAsStringAsync();
    _logger.LogInformation("原始响应内容: {Content}", rawContent);
    // 手动尝试反序列化
    var serializer = new JsonSerializer();
    try
    {
        var doc = serializer.Deserialize<AzureSearchDocument>(rawContent);
        _logger.LogInformation("手动反序列化结果: {Id}, {Title}", doc.Id, doc.Title);
    }
    catch (Exception ex)
    {
        _logger.LogError("反序列化失败: {Message}", ex.Message);
    }
    
  • 确认索引字段的可检索性:检查索引中目标字段的retrievable属性是否设置为true,若字段不可检索,SDK不会将其反序列化到对象中。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 16:46:15