如何在C#中创建不返回部分搜索结果的关键字搜索

时间:2017-01-24 19:55:22

标签: c#

我遇到的问题是,我创建了一个关键字搜索栏,这也会导致部分搜索结果。一个例子是,如果有人输入关键字“艺术教师”,不仅仅是“艺术老师”的成绩。填充,但任何结果与'艺术'在他们中,例如,兼职教师'或者'特许教师'。我只想要“艺术老师”的搜索结果。然而,填充。

 //filter by keywords
        if (!string.IsNullOrEmpty(param.keywords))
        {
            param.keywords = UtilityHelper.CleanKeywords(param.keywords.Trim());
            string pattern = @"\S+";
            Regex re = new Regex(pattern);
            MatchCollection matches = re.Matches(UtilityHelper.CleanKeywords(param.keywords));
            string[] words = new string[matches.Count];
            if (matches.Count == 1)
            {
                filteredList = FilterKeywords(filteredList, UtilityHelper.CleanKeywords(param.keywords));
            }
            else
            {
                if (param.searchType.ToLower() == "all" || string.IsNullOrEmpty(param.searchType))
                {
                    for (int i = 0; i < matches.Count; i++)
                    {
                        words[i] = UtilityHelper.CleanKeywords(matches[i].Value);
                        filteredList = FilterKeywords(filteredList, UtilityHelper.CleanKeywords(words[i]));
                    }

                    //var searchPredicate = PredicateBuilder.False<JobResultModel>();
                    //for (int i = 0; i < matches.Count; i++)
                    //{
                    //    words[i] = UtilityHelper.CleanKeywords(matches[i].Value);
                    //    searchPredicate = searchPredicate.Or(c => UtilityHelper.CleanKeywords(c.positionTitle).Contains(words[i]));
                    //}
                    //for (int i = 0; i < matches.Count; i++)
                    //{
                    //    words[i] = UtilityHelper.CleanKeywords(matches[i].Value);
                    //    searchPredicate = searchPredicate.Or(c => UtilityHelper.CleanKeywords(c.city).Contains(words[i]));
                    //}
                    //for (int i = 0; i < matches.Count; i++)
                    //{
                    //    words[i] = UtilityHelper.CleanKeywords(matches[i].Value);
                    //    searchPredicate = searchPredicate.Or(c => UtilityHelper.CleanKeywords(c.districtName).Contains(words[i]));
                    //}
                    //for (int i = 0; i < matches.Count; i++)
                    //{
                    //    words[i] = UtilityHelper.CleanKeywords(matches[i].Value);
                    //    searchPredicate = searchPredicate.Or(c => UtilityHelper.CleanKeywords(c.countyName).Contains(words[i]));
                    //}
                    //for (int i = 0; i < matches.Count; i++)
                    //{
                    //    words[i] = UtilityHelper.CleanKeywords(matches[i].Value);
                    //    searchPredicate = searchPredicate.Or(c => UtilityHelper.CleanKeywords(c.fullCountyName).Contains(words[i]));
                    //}
                    //filteredList = filteredList
                    //    .Where(searchPredicate);
                }
                else
                {
                    for (int i = 0; i < matches.Count; i++)
                    {
                        words[i] = UtilityHelper.CleanKeywords(matches[i].Value);
                        filteredList = FilterKeywords(filteredList, UtilityHelper.CleanKeywords(words[i]));
                    }

                    var searchPredicate = PredicateBuilder.False<JobResultModel>();
                    for (int i = 0; i < matches.Count; i++)
                    {
                        words[i] = UtilityHelper.CleanKeywords(matches[i].Value);
                        searchPredicate = searchPredicate.Or(c => UtilityHelper.CleanKeywords(c.positionTitle).Contains(words[i]));
                        searchPredicate = searchPredicate.Or(c => UtilityHelper.CleanKeywords(c.city).Contains(words[i]));
                        searchPredicate = searchPredicate.Or(c => UtilityHelper.CleanKeywords(c.districtName).Contains(words[i]));
                        searchPredicate = searchPredicate.Or(c => UtilityHelper.CleanKeywords(c.countyName).Contains(words[i]));
                        searchPredicate = searchPredicate.Or(c => UtilityHelper.CleanKeywords(c.fullCountyName).Contains(words[i]));
                        searchPredicate = searchPredicate.Or(c => UtilityHelper.CleanKeywords(c.jobType).Contains(words[i]));
                    }
                    filteredList = filteredList
                        .Where(searchPredicate);
                }
            }
        }

0 个答案:

没有答案