2015-08-25 95 views
4

我將關係數據庫上的搜索功能卸載到Azure搜索。我的產品表包含serialNumber,PartNumber等列。(可以有多個具有相同partNumber的serialNumbers)。Azure搜索建議中的獨特值?

我想創建一個可以自動完成partNumbers的提示器。但在我的情況下,我得到了很多重複的建議,因爲partNumber匹配找到了多個條目。

我該如何解決這個問題?

回答

3

Suggest API建議使用文檔,而不是查詢。如果您重複索引中每個serialNumber的partNumber信息,然後根據partNumber進行建議,則會得到每個匹配文檔的結果。通過在$ select參數中包含關鍵字段,可以更清楚地看到這一點。 Azure搜索將消除同一文檔中的重複內容,但不會跨文檔。您必須在客戶端執行該操作,或者僅爲建議而構建partNumbers的二級索引。

請參閱this forum thread進行更深入的討論。

此外,隨時對this UserVoice item投票,以幫助我們優先考慮改進建議。

0

我自己正面臨着這個問題。我的解決方案不涉及一個新的索引(這隻會變得雜亂,並且花費我們的錢)。

我接受這是一個while循環在過濾器中添加'UserIdentity'(在您的情況下,'partNumber'),並重新搜索,直到我的獲取/上限被滿足或者沒有更多的建議存在:

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; 
}