【发布时间】:2019-08-21 06:42:03
【问题描述】:
我正在使用 azure search api 尝试按某个字段值进行过滤:businesstype = store.它总是返回 3 家商店,即使我应该有数千家。我无法确定索引中的内容。在 Azure 搜索门户网站中,我键入 businessType eq 'store',它给了我两个商店,然后开始返回 businesstype = restaurant。不知道发生了什么。我们在过滤器工作的其他项目中有其他实现。这是我在使用 ASP.NET Web API 调用时正在执行的代码
var indexClient = new SearchIndexClient(GlobalSettings.SearchServiceName, $"businesses{GlobalSettings.Environment}", new SearchCredentials(GlobalSettings.SearchServiceAdminApiKey));
if (latitude == null && longitude == null)
{
//chicago
latitude = 41.8333925;
longitude = -88.0121478;
}
// get all attributes and camel case them
var attributes = typeof(BusinessSearchItem).GetProperties().Select(x => char.ToLowerInvariant(x.Name[0]) + x.Name.Substring(1)).ToList();
var parameters = new SearchParameters
{
Select = attributes,
QueryType = QueryType.Full,
Top = take,
Skip = skip,
IncludeTotalResultCount = true,
OrderBy = new List<string>() { $"geo.distance(location, geography'POINT({longitude} {latitude})')" }
};
// filters
string filter = "";
if (!string.IsNullOrEmpty(businessType))
{
switch (businessType.ToLower())
{
case "restaurant":
filter += "businessType eq 'Restaurant'";
break;
case "store":
filter += "businessType eq 'Store'";
break;
}// end switch on business type
}
parameters.Filter = filter;
try
{
// run the search
var results = indexClient.Documents.Search<BusinessSearchItem>(q, parameters);
Logger.Log.Info($"Search conducted. Query: {q} Business Type: {businessType} Lat: {latitude} Long: {longitude} User: {username}");
var businessDTOs = results.Results.Select(x => new BusinessDTO
{
.........
).ToList()
}).ToList();
模型 BusinessSearchItem 有一个字符串字段 BusinessType,它具有可搜索的属性。跳过为0,取40。
【问题讨论】:
-
您提到 BusinessType 被标记为可搜索。它是否也标记为可过滤?如果不是,我希望 Search 抛出 CloudException。
-
另外,Store 和 Restaurant 的大小写在您的问题中不一致。过滤区分大小写,因此如何大写文字很重要。您能否确认这些值的大小写在索引中是否一致?一种检查方法是在没有过滤器的情况下对 businessType 进行分面,假设它是可分面的。至于门户,如果您共享您使用的查询字符串会有所帮助。
-
布鲁斯,感谢您的回复。 BusinessType 也被标记为 IsFilterable。当我填充索引时,我在 BusinessType 枚举上调用 ToString,所以我确定他们都有带有大写 S 的“Store”。在门户中,查询如下所示:https://... ..search.windows.net/indexes/businessesstaging/docs?api-version=2019-05-06&search=businessType%20eq%20'Store'
-
我突然想到,我不知道您在提到外壳时是在谈论属性 NAME 还是 VALUE。 serach 模型有一个公共字符串 BusinessType,它具有可过滤和可搜索的属性。我看到代码用小 b 调用 businesstype = 'Store',这是你的意思吗?
-
@PatrickGoode 如果您的问题已经解决,请将答案添加到答案部分。那么这个issue就可以关闭了。谢谢。