建议查询需要很长时间

时间:2014-04-11 10:09:07

标签: c# elasticsearch nest

我正在使用NEST(c#)客户端与Elasticsearch进行通信。我使用建议API在我的网站中实现自动完成,这是我的查询代码示例

var completionResult = client.Search(
                body => body
                    .Type("myProject")
                    .Size(5)
                    .Fields("title","tags","control","platform")
                    .SuggestGlobalText(searchText)
                    .SuggestCompletion("SuggestCompletionResult", sfc => sfc
                        .OnField("control.suggestControl"))
                    .SuggestPhrase("SuggestPhraseResult", sfp => sfp
                        .OnField("control.suggestControl"))
                        );

我需要获得List<SuggestionList>的最终结果,我的建议列表类是

 public class SuggestionList
{
    public string Text { get; set; }
}

我执行的将结果转换为List<SuggestionList>的操作是

    List<SuggestionList> SuggestResultSet = new List<SuggestionList>();     

            if (completionResult != null)
            {
                if (completionResult.Suggest != null && completionResult.Suggest.Count > 0)
                {
                    var suggestionList = completionResult.Suggest.ToList();

                    if (suggestionList != null && suggestionList.Count > 0)
                    {
                        if (suggestionList[0].Value.ToList().Count() != 0 && suggestionList[0].Value.ToList()[0].Options.ToList().Count > 0)
                        {
                            foreach (var text in suggestionList[0].Value.ToList()[0].Options.ToList())
                            {
                                SuggestResultSet.Add(new SuggestionList { Text = text.Text });
                            }
                        }
                        if (suggestionList[1].Value.ToList().Count() != 0 && suggestionList[1].Value.ToList()[0].Options.ToList().Count > 0)
                        {
                            foreach (var text in suggestionList[1].Value.ToList()[0].Options.ToList())
                            {
                                SuggestResultSet.Add(new SuggestionList { Text = text.Text });
                            }
                        }

                    }
                }
            }

需要更长的时间才能获得最终结果,有没有办法在不影响性能的情况下做到这一点

1 个答案:

答案 0 :(得分:1)

我认为你的转换过程过于复杂,嵌套很高。请记住,这不是编译的,只是伪的。

List<SuggestionList> SuggestResultSet = new List<SuggestionList>();
if(completionResults != null)
{
    foreach(var suggestion in completionResults.Suggest)
    {
        SuggestsResultSet.Add(new SuggestionList {Text = suggestion.Text });
    }
}