Ejemplo n.º 1
0
        /// <summary>
        /// 用户搜索
        /// </summary>
        /// <param name="userQuery">搜索条件</param>
        /// <returns>符合搜索条件的User分页集合</returns>
        public PagingDataSet <User> Search(UserFullTextQuery userQuery)
        {
            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(userQuery);

            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query  query  = null;
            Filter filter = null;
            Sort   sort   = null;

            searchBuilder.BuildQuery(out query, out filter, out sort);

            //调用SearchEngine.Search(),执行搜索
            PagingDataSet <Document> searchResult = searchEngine.Search(query, filter, sort, userQuery.PageIndex, userQuery.PageSize);
            IEnumerable <Document>   docs         = searchResult.ToList <Document>();

            //解析出搜索结果中的用户ID
            List <long> userIds = new List <long>();

            foreach (Document doc in docs)
            {
                long userId = long.Parse(doc.Get(UserIndexDocument.UserId));
                userIds.Add(userId);
            }

            //根据用户ID列表批量查询用户实例
            IEnumerable <User> userList = userService.GetFullUsers(userIds);

            //根据用户ID列表批量查询UserProfile实例,利用Repository的自动缓存机制,减少页面加载时的关联数据库查询次数
            //对于UserProfile的Tag、WorkExperience、EducationExperience,目前页面列表不显示,暂不处理
            userProfileService.GetUserProfiles(userIds);

            //组装分页对象
            PagingDataSet <User> users = new PagingDataSet <User>(userList)
            {
                TotalRecords  = searchResult.TotalRecords,
                PageSize      = searchResult.PageSize,
                PageIndex     = searchResult.PageIndex,
                QueryDuration = searchResult.QueryDuration
            };

            return(users);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 自动完成搜索
        /// </summary>
        /// <param name="userQuery"></param>
        /// <returns></returns>
        public IEnumerable <User> AutoCompleteSearch(UserFullTextQuery userQuery)
        {
            if (string.IsNullOrWhiteSpace(userQuery.Keyword))
            {
                return(new List <User>());
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(userQuery);

            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query  query  = null;
            Filter filter = null;
            Sort   sort   = null;

            searchBuilder.BuildQuery(out query, out filter, out sort);

            //调用SearchService.Search(),执行搜索
            IEnumerable <Document> docs = searchEngine.Search(query, filter, sort, userQuery.PageSize);

            //解析出搜索结果中的用户ID
            List <long> userIds = new List <long>();

            foreach (Document doc in docs)
            {
                long userId = long.Parse(doc.Get(UserIndexDocument.UserId));
                userIds.Add(userId);
            }

            //根据用户ID列表批量查询用户实例
            IEnumerable <User> users = userService.GetFullUsers(userIds);

            //根据用户ID列表批量查询用户相关实例,利用Repository的自动缓存机制,减少页面加载时的关联数据库查询次数
            IEnumerable <UserProfile> userProfiles = userProfileService.GetUserProfiles(userIds);

            return(users);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 根据用户搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="userQuery"></param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(UserFullTextQuery userQuery)
        {
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();

            //搜索词匹配范围
            Dictionary<string, BoostLevel> fieldNameAndBoosts = new Dictionary<string, BoostLevel>();
            switch (userQuery.SearchRange)
            {
                case UserSearchRange.NAME:
                    fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                    searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                    break;

                case UserSearchRange.TAG:
                    searchBuilder.WithPhrase(UserIndexDocument.TagName, userQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case UserSearchRange.SCHOOL:
                    searchBuilder.WithPhrase(UserIndexDocument.School, userQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case UserSearchRange.COMPANY:
                    searchBuilder.WithPhrase(UserIndexDocument.CompanyName, userQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case UserSearchRange.NOWAREACODE:
                    if (!string.IsNullOrEmpty(userQuery.Keyword))
                    {
                        fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                        fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                        fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                        fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                        searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                    }
                    if (!string.IsNullOrEmpty(userQuery.NowAreaCode))
                        searchBuilder.WithField(UserIndexDocument.NowAreaCode, userQuery.NowAreaCode.TrimEnd('0'), false, BoostLevel.Hight, false);
                    else
                        searchBuilder.WithFields(UserIndexDocument.NowAreaCode, new string[] { "1", "2", "3" }, false, BoostLevel.Hight, false);
                    break;

                case UserSearchRange.HOMEAREACODE:
                    if (!string.IsNullOrEmpty(userQuery.Keyword))
                    {
                        fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                        fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                        fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                        fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                        searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                    }
                    if (!string.IsNullOrEmpty(userQuery.HomeAreaCode))
                        searchBuilder.WithField(UserIndexDocument.HomeAreaCode, userQuery.HomeAreaCode.TrimEnd('0'), false, BoostLevel.Hight, false);
                    else
                        searchBuilder.WithFields(UserIndexDocument.HomeAreaCode, new string[] { "1", "2", "3" }, false, BoostLevel.Hight, false);
                    break;

                case UserSearchRange.Gender:
                    if (!string.IsNullOrEmpty(userQuery.Keyword))
                    {
                        fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                        fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                        fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                        fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                        searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                    }
                    searchBuilder.WithField(UserIndexDocument.Gender, ((int)userQuery.Gender).ToString(), false, BoostLevel.Hight, false);
                    break;

                case UserSearchRange.Age:
                    if (!string.IsNullOrEmpty(userQuery.Keyword))
                    {
                        fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                        fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                        fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                        fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                        searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                    }
                    if (userQuery.AgeMin > userQuery.AgeMax)
                    {
                        int temp = userQuery.AgeMin;
                        userQuery.AgeMin = userQuery.AgeMax;
                        userQuery.AgeMax = temp;
                    }
                    if (userQuery.AgeMin < 0)
                    {
                        userQuery.AgeMin = 0;
                    }
                    if (userQuery.AgeMax > 200)
                    {
                        userQuery.AgeMin = 200;
                    }

                    string yearMax = (DateTime.Now.Year - userQuery.AgeMin).ToString().PadLeft(3, '0');
                    string yearMin = (DateTime.Now.Year - userQuery.AgeMax).ToString().PadLeft(3, '0');

                    searchBuilder.WithinRange(UserIndexDocument.BirthdayYear, yearMin, yearMax, false);
                    break;

                default:
                    fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                    searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                    break;

            }

            //所在地区过滤
            if (!string.IsNullOrWhiteSpace(userQuery.NowAreaCode) && userQuery.SearchRange != UserSearchRange.NOWAREACODE)
            {
                searchBuilder.WithField(UserIndexDocument.NowAreaCode, userQuery.NowAreaCode.TrimEnd('0'), false, BoostLevel.Hight, true);
            }

            //家乡地区过滤
            if (!string.IsNullOrWhiteSpace(userQuery.HomeAreaCode) && userQuery.SearchRange != UserSearchRange.HOMEAREACODE)
            {
                searchBuilder.WithField(UserIndexDocument.HomeAreaCode, userQuery.HomeAreaCode.TrimEnd('0'), false, BoostLevel.Hight, true);
            }

            //性别过滤
            if (userQuery.Gender != GenderType.NotSet)
            {
                searchBuilder.WithField(UserIndexDocument.Gender, ((int)userQuery.Gender).ToString(), true, BoostLevel.Hight, true);
            }

            //年龄过滤
            if (userQuery.AgeMin > 0 || userQuery.AgeMax > 0)
            {
                if (userQuery.AgeMin > userQuery.AgeMax)
                {
                    int temp = userQuery.AgeMin;
                    userQuery.AgeMin = userQuery.AgeMax;
                    userQuery.AgeMax = temp;
                }
                if (userQuery.AgeMin < 0)
                {
                    userQuery.AgeMin = 0;
                }
                if (userQuery.AgeMax > 200)
                {
                    userQuery.AgeMin = 200;
                }

                string yearMax = (DateTime.Now.Year - userQuery.AgeMin).ToString().PadLeft(3, '0');
                string yearMin = (DateTime.Now.Year - userQuery.AgeMax).ToString().PadLeft(3, '0');

                searchBuilder.WithinRange(UserIndexDocument.BirthdayYear, yearMin, yearMax, true);
            }

            //按最后活动时间倒排序
            searchBuilder.SortByString(UserIndexDocument.LastActivityTime, true);

            return searchBuilder;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 用户搜索
        /// </summary>
        /// <param name="userQuery">搜索条件</param>
        /// <returns>符合搜索条件的User分页集合</returns>
        public PagingDataSet<User> Search(UserFullTextQuery userQuery)
        {
            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(userQuery);

            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query query = null;
            Filter filter = null;
            Sort sort = null;
            searchBuilder.BuildQuery(out query, out filter, out sort);

            //调用SearchEngine.Search(),执行搜索
            PagingDataSet<Document> searchResult = searchEngine.Search(query, filter, sort, userQuery.PageIndex, userQuery.PageSize);
            IEnumerable<Document> docs = searchResult.ToList<Document>();

            //解析出搜索结果中的用户ID
            List<long> userIds = new List<long>();
            foreach (Document doc in docs)
            {
                long userId = long.Parse(doc.Get(UserIndexDocument.UserId));
                userIds.Add(userId);
            }

            //根据用户ID列表批量查询用户实例
            IEnumerable<User> userList = userService.GetFullUsers(userIds);

            //根据用户ID列表批量查询UserProfile实例,利用Repository的自动缓存机制,减少页面加载时的关联数据库查询次数
            //对于UserProfile的Tag、WorkExperience、EducationExperience,目前页面列表不显示,暂不处理
            userProfileService.GetUserProfiles(userIds);

            //组装分页对象
            PagingDataSet<User> users = new PagingDataSet<User>(userList)
            {
                TotalRecords = searchResult.TotalRecords,
                PageSize = searchResult.PageSize,
                PageIndex = searchResult.PageIndex,
                QueryDuration = searchResult.QueryDuration
            };

            return users;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 自动完成搜索
        /// </summary>
        /// <param name="userQuery"></param>
        /// <returns></returns>
        public IEnumerable<User> AutoCompleteSearch(UserFullTextQuery userQuery)
        {
            if (string.IsNullOrWhiteSpace(userQuery.Keyword))
            {
                return new List<User>();
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(userQuery);

            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query query = null;
            Filter filter = null;
            Sort sort = null;
            searchBuilder.BuildQuery(out query, out filter, out sort);

            //调用SearchService.Search(),执行搜索
            IEnumerable<Document> docs = searchEngine.Search(query, filter, sort, userQuery.PageSize);

            //解析出搜索结果中的用户ID
            List<long> userIds = new List<long>();
            foreach (Document doc in docs)
            {
                long userId = long.Parse(doc.Get(UserIndexDocument.UserId));
                userIds.Add(userId);
            }

            //根据用户ID列表批量查询用户实例
            IEnumerable<User> users = userService.GetFullUsers(userIds);

            //根据用户ID列表批量查询用户相关实例,利用Repository的自动缓存机制,减少页面加载时的关联数据库查询次数
            IEnumerable<UserProfile> userProfiles = userProfileService.GetUserProfiles(userIds);

            return users;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 用户搜索自动完成
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public JsonResult UserSearchAutoComplete(string keyword)
        {
            UserFullTextQuery query = new UserFullTextQuery();
            query.SearchRange = UserSearchRange.NAME;
            query.Keyword = keyword;//查询关键字
            query.PageSize = 6;//最大记录数

            //调用搜索器进行搜索
            UserSearcher userSearcher = (UserSearcher)SearcherFactory.GetSearcher(UserSearcher.CODE);
            IEnumerable<User> users = userSearcher.AutoCompleteSearch(query);

            var jsonResult = Json(users.Select(user => new { UserId = user.UserId, DisplayName = user.DisplayName, DisplayNameWithHighlight = SearchEngine.Highlight(keyword, user.DisplayName, 100), Introduction = user.Profile == null ? "" : string.Join("", user.Profile.Introduction.Take(30)), AvatarImage = SiteUrls.Instance().UserAvatarUrl(user, AvatarSizeType.Small, true) }), JsonRequestBehavior.AllowGet);
            return jsonResult;
        }
Ejemplo n.º 7
0
        public ActionResult UserSearch(UserFullTextQuery query)
        {
            query.PageSize = 20;//每页记录数

            //调用搜索器进行搜索
            UserSearcher userSearcher = (UserSearcher)SearcherFactory.GetSearcher(UserSearcher.CODE);
            PagingDataSet<User> users = userSearcher.Search(query);
            IUser CurrentUser = UserContext.CurrentUser;

            //添加到用户搜索历史
            if (CurrentUser != null)
            {
                SearchHistoryService searchHistoryService = new SearchHistoryService();
                searchHistoryService.SearchTerm(CurrentUser.UserId, UserSearcher.CODE, query.Keyword);
            }
            //添加到热词
            if (!string.IsNullOrEmpty(query.Keyword))
            {
                SearchedTermService searchedTermService = new SearchedTermService();
                searchedTermService.SearchTerm(UserSearcher.CODE, query.Keyword);
            }

            //设置页面Meta
            if (string.IsNullOrWhiteSpace(query.Keyword))
            {
                pageResourceManager.InsertTitlePart("用户搜索");//设置页面Title
            }
            else
            {
                pageResourceManager.InsertTitlePart('“' + query.Keyword + '”' + "的相关用户");//设置页面Title
            }

            pageResourceManager.SetMetaOfKeywords("用户搜索");//设置Keyords类型的Meta
            pageResourceManager.SetMetaOfDescription("用户搜索");//设置Description类型的Meta

            return View(users);
        }
Ejemplo n.º 8
0
        public JsonResult SearchUsers(UserSelectorSearchScope searchScope)
        {
            string term = Request.QueryString.GetString("q", string.Empty);
            int topNumber = 8;
            if (string.IsNullOrEmpty(term))
                return Json(new { }, JsonRequestBehavior.AllowGet);
            term = WebUtility.UrlDecode(term);
            term = term.ToLower();
            long userId = 0;
            if (UserContext.CurrentUser != null)
                userId = UserContext.CurrentUser.UserId;
            IEnumerable<User> users = null;
            var followService = new FollowService();

            var followedUsers = followService.GetFollows(userId, null, Follow_SortBy.FollowerCount_Desc);
            if (searchScope == UserSelectorSearchScope.FollowedUser)
            {
                IUserService userService = DIContainer.Resolve<IUserService>();
                users = userService.GetFullUsers(followedUsers.Select(n => n.FollowedUserId))
                       .Where(n => n.TrueName.ToLower().Contains(term)
                                   || n.NickName.ToLower().Contains(term)
                                   || GetNoteName(followedUsers, n.UserId).ToLower().Contains(term)
                                   || n.UserName.ToLower().Contains(term))
                                   .Take(topNumber);
            }
            else
            {

                UserSearcher userSearcher = (UserSearcher)SearcherFactory.GetSearcher(UserSearcher.CODE);
                UserFullTextQuery query = new UserFullTextQuery();
                query.Keyword = term;
                query.PageIndex = 1;
                query.PageSize = topNumber;
                users = userSearcher.Search(query);
            }
            return Json(users
                   .Select(n => new
                   {
                       userId = n.UserId,
                       displayName = n.DisplayName,
                       userName = n.UserName,
                       trueName = n.TrueName,
                       nickName = n.NickName,
                       noteName = GetNoteName(followedUsers, n.UserId),
                       userAvatarUrl = SiteUrls.Instance().UserAvatarUrl(n, AvatarSizeType.Small)
                   }), JsonRequestBehavior.AllowGet);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 用户快捷搜索
        /// </summary>
        /// <param name="query"></param>
        /// <param name="topNumber"></param>
        /// <returns></returns>
        public ActionResult _UserQuickSearch(UserFullTextQuery query, int topNumber)
        {
            query.PageSize = topNumber;//每页记录数
            query.PageIndex = 1;
            query.SearchRange = UserSearchRange.NAME;

            //调用搜索器进行搜索
            UserSearcher userSearcher = (UserSearcher)SearcherFactory.GetSearcher(UserSearcher.CODE);
            PagingDataSet<User> user = userSearcher.Search(query);

            return PartialView(user);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 根据用户搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="userQuery"></param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(UserFullTextQuery userQuery)
        {
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();

            //搜索词匹配范围
            Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>();

            switch (userQuery.SearchRange)
            {
            case UserSearchRange.NAME:
                fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Low);
                fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                break;

            case UserSearchRange.TAG:
                searchBuilder.WithPhrase(UserIndexDocument.TagName, userQuery.Keyword, BoostLevel.Hight, false);
                break;

            case UserSearchRange.SCHOOL:
                searchBuilder.WithPhrase(UserIndexDocument.School, userQuery.Keyword, BoostLevel.Hight, false);
                break;

            case UserSearchRange.COMPANY:
                searchBuilder.WithPhrase(UserIndexDocument.CompanyName, userQuery.Keyword, BoostLevel.Hight, false);
                break;

            case UserSearchRange.NOWAREACODE:
                if (!string.IsNullOrEmpty(userQuery.Keyword))
                {
                    fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                    searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                }
                if (!string.IsNullOrEmpty(userQuery.NowAreaCode))
                {
                    searchBuilder.WithField(UserIndexDocument.NowAreaCode, userQuery.NowAreaCode.TrimEnd('0'), false, BoostLevel.Hight, false);
                }
                else
                {
                    searchBuilder.WithFields(UserIndexDocument.NowAreaCode, new string[] { "1", "2", "3" }, false, BoostLevel.Hight, false);
                }
                break;

            case UserSearchRange.HOMEAREACODE:
                if (!string.IsNullOrEmpty(userQuery.Keyword))
                {
                    fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                    searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                }
                if (!string.IsNullOrEmpty(userQuery.HomeAreaCode))
                {
                    searchBuilder.WithField(UserIndexDocument.HomeAreaCode, userQuery.HomeAreaCode.TrimEnd('0'), false, BoostLevel.Hight, false);
                }
                else
                {
                    searchBuilder.WithFields(UserIndexDocument.HomeAreaCode, new string[] { "1", "2", "3" }, false, BoostLevel.Hight, false);
                }
                break;

            case UserSearchRange.Gender:
                if (!string.IsNullOrEmpty(userQuery.Keyword))
                {
                    fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                    searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                }
                searchBuilder.WithField(UserIndexDocument.Gender, ((int)userQuery.Gender).ToString(), false, BoostLevel.Hight, false);
                break;

            case UserSearchRange.Age:
                if (!string.IsNullOrEmpty(userQuery.Keyword))
                {
                    fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                    searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                }
                if (userQuery.AgeMin > userQuery.AgeMax)
                {
                    int temp = userQuery.AgeMin;
                    userQuery.AgeMin = userQuery.AgeMax;
                    userQuery.AgeMax = temp;
                }
                if (userQuery.AgeMin < 0)
                {
                    userQuery.AgeMin = 0;
                }
                if (userQuery.AgeMax > 200)
                {
                    userQuery.AgeMin = 200;
                }

                string yearMax = (DateTime.Now.Year - userQuery.AgeMin).ToString().PadLeft(3, '0');
                string yearMin = (DateTime.Now.Year - userQuery.AgeMax).ToString().PadLeft(3, '0');

                searchBuilder.WithinRange(UserIndexDocument.BirthdayYear, yearMin, yearMax, false);
                break;

            default:
                fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                break;
            }

            //所在地区过滤
            if (!string.IsNullOrWhiteSpace(userQuery.NowAreaCode) && userQuery.SearchRange != UserSearchRange.NOWAREACODE)
            {
                searchBuilder.WithField(UserIndexDocument.NowAreaCode, userQuery.NowAreaCode.TrimEnd('0'), false, BoostLevel.Hight, true);
            }

            //家乡地区过滤
            if (!string.IsNullOrWhiteSpace(userQuery.HomeAreaCode) && userQuery.SearchRange != UserSearchRange.HOMEAREACODE)
            {
                searchBuilder.WithField(UserIndexDocument.HomeAreaCode, userQuery.HomeAreaCode.TrimEnd('0'), false, BoostLevel.Hight, true);
            }

            //性别过滤
            if (userQuery.Gender != GenderType.NotSet)
            {
                searchBuilder.WithField(UserIndexDocument.Gender, ((int)userQuery.Gender).ToString(), true, BoostLevel.Hight, true);
            }

            //年龄过滤
            if (userQuery.AgeMin > 0 || userQuery.AgeMax > 0)
            {
                if (userQuery.AgeMin > userQuery.AgeMax)
                {
                    int temp = userQuery.AgeMin;
                    userQuery.AgeMin = userQuery.AgeMax;
                    userQuery.AgeMax = temp;
                }
                if (userQuery.AgeMin < 0)
                {
                    userQuery.AgeMin = 0;
                }
                if (userQuery.AgeMax > 200)
                {
                    userQuery.AgeMin = 200;
                }

                string yearMax = (DateTime.Now.Year - userQuery.AgeMin).ToString().PadLeft(3, '0');
                string yearMin = (DateTime.Now.Year - userQuery.AgeMax).ToString().PadLeft(3, '0');

                searchBuilder.WithinRange(UserIndexDocument.BirthdayYear, yearMin, yearMax, true);
            }

            //按最后活动时间倒排序
            searchBuilder.SortByString(UserIndexDocument.LastActivityTime, true);

            return(searchBuilder);
        }