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

Azure Search的search.in语法正确写法是什么?

解决Azure搜索中Edm.Int32字段多值过滤的问题

我太懂你翻遍文档、反复啃评论却找不到正确语法的憋屈了!针对你用PrimaryUnitTypeId(Edm.Int32类型)做多值过滤的需求,分两种常用场景给你具体方案:

1. Azure门户搜索资源管理器中的查询语法

直接用in操作符匹配多个整数取值,语法非常直观:

PrimaryUnitTypeId in (1, 3, 5)

注意这里的数值不要加引号,因为字段类型是Edm.Int32,加引号会触发类型不匹配的错误。你可以把括号里的数值换成你需要的任意多个Int32值。

2. .NET Client库中的实现方式

如果你用的是Azure.Search.Documents客户端库,有两种靠谱写法:

方式一:强类型过滤(推荐,避免拼写错误)

借助SearchFilter.Create方法生成类型安全的过滤条件:

using Azure.Search.Documents;
using Azure.Search.Documents.Models;

// 假设你已经初始化好SearchClient实例
SearchClient searchClient = ...;

// 构建多值过滤规则
SearchFilter filter = SearchFilter.Create(nameof(PrimaryUnitTypeId), 1, 3, 5);

// 执行搜索
SearchOptions options = new SearchOptions { Filter = filter };
Response<SearchResults<YourDocumentModel>> response = await searchClient.SearchAsync<YourDocumentModel>("*", options);

方式二:手动构造过滤字符串

如果更习惯手写逻辑,也可以直接构建符合OData语法的字符串:

string filter = $"{nameof(PrimaryUnitTypeId)} in (1, 3, 5)";
SearchOptions options = new SearchOptions { Filter = filter };
var response = await searchClient.SearchAsync<YourDocumentModel>("*", options);

额外避坑提示

  • 确保传入的所有值都是整数类型,别传字符串形式的数字,否则会触发类型兼容错误。
  • 如果需要动态生成多值列表,可以用string.Join拼接,但要注意避免注入风险(优先用强类型方法更安全):
    List<int> targetIds = new List<int> { 2, 4, 6 };
    string filter = $"{nameof(PrimaryUnitTypeId)} in ({string.Join(",", targetIds)})";
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:46:02