我正在将关系数据库上的搜索功能卸载到Azure搜索。我的Products表包含serialNumber,PartNumber等列。(可以有多个serialNumber具有相同的partNumber)。
我想创建一个可以自动完成partNumbers的建议器。但在我的场景中,我在建议中得到了很多重复,因为partNumber匹配在多个条目中找到。
我该如何解决这个问题?
答案 0 :(得分:3)
Suggest API建议文档,而不是查询。如果重复索引中每个serialNumber的partNumber信息,然后根据partNumber建议,您将获得每个匹配文档的结果。通过在$ select参数中包含关键字段,您可以更清楚地看到这一点。 Azure搜索将消除同一文档中的重复项,但不会跨文档。您必须在客户端执行此操作,或者仅为建议构建partNumbers的二级索引。
请参阅this forum thread进行更深入的讨论。
另外,您可以this UserVoice item投票,以帮助我们优先改进建议。
答案 1 :(得分:0)
我自己也面临这个问题。我的解决方案不涉及新的索引(这只会变得混乱而且花费我们的钱)。
我对此的看法是一个while循环,将'UserIdentity'(在您的情况下,'partNumber')添加到过滤器,并重新搜索,直到满足我的take / top-limit或不再存在建议:
public async Task<List<MachineSuggestionDTO>> SuggestMachineUser(string searchText, int take, string[] searchFields)
{
var indexClientMachine = _searchServiceClient.Indexes.GetClient(INDEX_MACHINE);
var suggestions = new List<MachineSuggestionDTO>();
var sp = new SuggestParameters
{
UseFuzzyMatching = true,
Top = 100 // Get maximum result for a chance to reduce search calls.
};
// Add searchfields if set
if (searchFields != null && searchFields.Count() != 0)
{
sp.SearchFields = searchFields;
}
// Loop until you get the desired ammount of suggestions, or if under desired ammount, the maximum.
while (suggestions.Count < take)
{
if (!await DistinctSuggestMachineUser(searchText, take, searchFields, suggestions, indexClientMachine, sp))
{
// If no more suggestions is found, we break the while-loop
break;
}
}
// Since the list might me bigger then the take, we return a narrowed list
return suggestions.Take(take).ToList();
}
private async Task<bool> DistinctSuggestMachineUser(string searchText, int take, string[] searchFields, List<MachineSuggestionDTO> suggestions, ISearchIndexClient indexClientMachine, SuggestParameters sp)
{
var response = await indexClientMachine.Documents.SuggestAsync<MachineSearchDocument>(searchText, SUGGESTION_MACHINE, sp);
if(response.Results.Count > 0){
// Fix filter if search is triggered once more
if (!string.IsNullOrEmpty(sp.Filter))
{
sp.Filter += " and ";
}
foreach (var result in response.Results.DistinctBy(r => new { r.Document.UserIdentity, r.Document.UserName, r.Document.UserCode}).Take(take))
{
var d = result.Document;
suggestions.Add(new MachineSuggestionDTO { Id = d.UserIdentity, Namn = d.UserNamn, Hkod = d.UserHkod, Intnr = d.UserIntnr });
// Add found UserIdentity to filter
sp.Filter += $"UserIdentity ne '{d.UserIdentity}' and ";
}
// Remove end of filter if it is run once more
if (sp.Filter.EndsWith(" and "))
{
sp.Filter = sp.Filter.Substring(0, sp.Filter.LastIndexOf(" and ", StringComparison.Ordinal));
}
}
// Returns false if no more suggestions is found
return response.Results.Count > 0;
}
答案 2 :(得分:0)
public async Task<List<string>> SuggestionsAsync(bool highlights, bool fuzzy, string term)
{
SuggestParameters sp = new SuggestParameters()
{
UseFuzzyMatching = fuzzy,
Top = 100
};
if (highlights)
{
sp.HighlightPreTag = "<em>";
sp.HighlightPostTag = "</em>";
}
var suggestResult = await searchConfig.IndexClient.Documents.SuggestAsync(term, "mysuggestion", sp);
// Convert the suggest query results to a list that can be displayed in the client.
return suggestResult.Results.Select(x => x.Text).Distinct().Take(10).ToList();
}
进入前100名并使用与众不同的方法对我有用。
答案 3 :(得分:0)
您可以将自动完成API用于默认情况下的分组位置。但是,如果您需要更多字段以及结果(例如partNo plus描述),则不支持该字段。不过,partNo会有所不同。