コード例 #1
0
ファイル: GroupSearcher.cs プロジェクト: ClaytonWang/Dw3cSNS
        /// <summary>
        /// 根据帖吧搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="Query">搜索条件</param>
        /// <param name="interestGroup">是否是查询可能感兴趣的群组</param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(GroupFullTextQuery groupQuery, bool interestGroup = false)
        {
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();
            Dictionary<string, BoostLevel> fieldNameAndBoosts = new Dictionary<string, BoostLevel>();
            //关键字为空就是在搜地区时关键字为空
            if (groupQuery.KeywordIsNull)
            {
                if (!string.IsNullOrEmpty(groupQuery.NowAreaCode))
                    searchBuilder.WithField(GroupIndexDocument.AreaCode, groupQuery.NowAreaCode.TrimEnd('0'), false, BoostLevel.Hight, false);
                else
                    searchBuilder.WithFields(GroupIndexDocument.AreaCode, new string[] { "1", "2", "3" }, false, BoostLevel.Hight, false);
            }

            if (!string.IsNullOrEmpty(groupQuery.Keyword))
            {
                //范围
                switch (groupQuery.Range)
                {
                    case GroupSearchRange.GROUPNAME:
                        searchBuilder.WithPhrase(GroupIndexDocument.GroupName, groupQuery.Keyword, BoostLevel.Hight, false);
                        break;
                    case GroupSearchRange.DESCRIPTION:
                        searchBuilder.WithPhrase(GroupIndexDocument.Description, groupQuery.Keyword, BoostLevel.Hight, false);
                        break;
                    case GroupSearchRange.TAG:
                        searchBuilder.WithPhrase(GroupIndexDocument.Tag, groupQuery.Keyword, BoostLevel.Hight, false);
                        break;
                    case GroupSearchRange.CATEGORYNAME:
                        searchBuilder.WithPhrase(GroupIndexDocument.CategoryName, groupQuery.Keyword, BoostLevel.Hight, false);
                        break;
                    default:
                            fieldNameAndBoosts.Add(GroupIndexDocument.GroupName, BoostLevel.Hight);
                            fieldNameAndBoosts.Add(GroupIndexDocument.Description, BoostLevel.Medium);
                            fieldNameAndBoosts.Add(GroupIndexDocument.Tag, BoostLevel.Medium);
                            fieldNameAndBoosts.Add(GroupIndexDocument.CategoryName, BoostLevel.Medium);
                            searchBuilder.WithPhrases(fieldNameAndBoosts, groupQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                        break;
                }
            }

            //根据标签搜索可能感兴趣的群组
            if (interestGroup)
            {
                searchBuilder.WithPhrases(GroupIndexDocument.Tag, groupQuery.Tags, BoostLevel.Hight, false);
                searchBuilder.NotWithFields(GroupIndexDocument.GroupId, groupQuery.GroupIds);
            }

            //筛选
            //某地区
            if (!string.IsNullOrEmpty(groupQuery.NowAreaCode))
            {
                searchBuilder.WithField(GroupIndexDocument.AreaCode, groupQuery.NowAreaCode.TrimEnd('0'), false, BoostLevel.Hight, true);
            }

            //某分类
            if (groupQuery.CategoryId != 0)
            {

                //fixed by wanf:发现群组已经不再用全文检索了

                CategoryService categoryService = new CategoryService();
                IEnumerable<Category> categories = categoryService.GetDescendants(groupQuery.CategoryId);
                List<string> categoryIds = new List<string> { groupQuery.CategoryId.ToString() };
                if (categories != null && categories.Count() > 0)
                {
                    categoryIds.AddRange(categories.Select(n => n.CategoryId.ToString()));
                }

                searchBuilder.WithFields(GroupIndexDocument.CategoryId, categoryIds, true, BoostLevel.Hight, true);
            }

            //公开的群组
            searchBuilder.WithField(GroupIndexDocument.IsPublic, "1", true, BoostLevel.Hight, true);

            //过滤可以显示的群组
            switch (publiclyAuditStatus)
            {
                case PubliclyAuditStatus.Again:
                case PubliclyAuditStatus.Fail:
                case PubliclyAuditStatus.Pending:
                case PubliclyAuditStatus.Success:
                    searchBuilder.WithField(GroupIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), true, BoostLevel.Hight, true);
                    break;
                case PubliclyAuditStatus.Again_GreaterThanOrEqual:
                case PubliclyAuditStatus.Pending_GreaterThanOrEqual:
                    searchBuilder.WithinRange(GroupIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), ((int)PubliclyAuditStatus.Success).ToString(), true);
                    break;
            }

            if (groupQuery.sortBy.HasValue)
            {
                switch (groupQuery.sortBy.Value)
                {
                    case SortBy_Group.DateCreated_Desc:
                        searchBuilder.SortByString(GroupIndexDocument.DateCreated, true);
                        break;
                    case SortBy_Group.MemberCount_Desc:
                        searchBuilder.SortByString(GroupIndexDocument.MemberCount, true);
                        break;
                    case SortBy_Group.GrowthValue_Desc:
                        searchBuilder.SortByString(GroupIndexDocument.GrowthValue, true);
                        break;
                }
            }
            else
            {
                //时间倒序排序
                searchBuilder.SortByString(GroupIndexDocument.DateCreated, true);
            }

            return searchBuilder;
        }
コード例 #2
0
ファイル: GroupSearcher.cs プロジェクト: x1987624/SNS
        /// <summary>
        /// 根据帖吧搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="Query">搜索条件</param>
        /// <param name="interestGroup">是否是查询可能感兴趣的群组</param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(GroupFullTextQuery groupQuery, bool interestGroup = false)
        {
            LuceneSearchBuilder             searchBuilder      = new LuceneSearchBuilder();
            Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>();

            //关键字为空就是在搜地区时关键字为空
            if (groupQuery.KeywordIsNull)
            {
                if (!string.IsNullOrEmpty(groupQuery.NowAreaCode))
                {
                    searchBuilder.WithField(GroupIndexDocument.AreaCode, groupQuery.NowAreaCode.TrimEnd('0'), false, BoostLevel.Hight, false);
                }
                else
                {
                    searchBuilder.WithFields(GroupIndexDocument.AreaCode, new string[] { "1", "2", "3" }, false, BoostLevel.Hight, false);
                }
            }

            if (!string.IsNullOrEmpty(groupQuery.Keyword))
            {
                //范围
                switch (groupQuery.Range)
                {
                case GroupSearchRange.GROUPNAME:
                    searchBuilder.WithPhrase(GroupIndexDocument.GroupName, groupQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case GroupSearchRange.DESCRIPTION:
                    searchBuilder.WithPhrase(GroupIndexDocument.Description, groupQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case GroupSearchRange.TAG:
                    searchBuilder.WithPhrase(GroupIndexDocument.Tag, groupQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case GroupSearchRange.CATEGORYNAME:
                    searchBuilder.WithPhrase(GroupIndexDocument.CategoryName, groupQuery.Keyword, BoostLevel.Hight, false);
                    break;

                default:
                    fieldNameAndBoosts.Add(GroupIndexDocument.GroupName, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(GroupIndexDocument.Description, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(GroupIndexDocument.Tag, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(GroupIndexDocument.CategoryName, BoostLevel.Medium);
                    searchBuilder.WithPhrases(fieldNameAndBoosts, groupQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                    break;
                }
            }

            //根据标签搜索可能感兴趣的群组
            if (interestGroup)
            {
                searchBuilder.WithPhrases(GroupIndexDocument.Tag, groupQuery.Tags, BoostLevel.Hight, false);
                searchBuilder.NotWithFields(GroupIndexDocument.GroupId, groupQuery.GroupIds);
            }

            //筛选
            //某地区
            if (!string.IsNullOrEmpty(groupQuery.NowAreaCode))
            {
                searchBuilder.WithField(GroupIndexDocument.AreaCode, groupQuery.NowAreaCode.TrimEnd('0'), false, BoostLevel.Hight, true);
            }

            //某分类
            if (groupQuery.CategoryId != 0)
            {
                //fixed by wanf:发现群组已经不再用全文检索了

                CategoryService        categoryService = new CategoryService();
                IEnumerable <Category> categories      = categoryService.GetDescendants(groupQuery.CategoryId);
                List <string>          categoryIds     = new List <string> {
                    groupQuery.CategoryId.ToString()
                };
                if (categories != null && categories.Count() > 0)
                {
                    categoryIds.AddRange(categories.Select(n => n.CategoryId.ToString()));
                }

                searchBuilder.WithFields(GroupIndexDocument.CategoryId, categoryIds, true, BoostLevel.Hight, true);
            }

            //公开的群组
            searchBuilder.WithField(GroupIndexDocument.IsPublic, "1", true, BoostLevel.Hight, true);

            //过滤可以显示的群组
            switch (publiclyAuditStatus)
            {
            case PubliclyAuditStatus.Again:
            case PubliclyAuditStatus.Fail:
            case PubliclyAuditStatus.Pending:
            case PubliclyAuditStatus.Success:
                searchBuilder.WithField(GroupIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), true, BoostLevel.Hight, true);
                break;

            case PubliclyAuditStatus.Again_GreaterThanOrEqual:
            case PubliclyAuditStatus.Pending_GreaterThanOrEqual:
                searchBuilder.WithinRange(GroupIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), ((int)PubliclyAuditStatus.Success).ToString(), true);
                break;
            }

            if (groupQuery.sortBy.HasValue)
            {
                switch (groupQuery.sortBy.Value)
                {
                case SortBy_Group.DateCreated_Desc:
                    searchBuilder.SortByString(GroupIndexDocument.DateCreated, true);
                    break;

                case SortBy_Group.MemberCount_Desc:
                    searchBuilder.SortByString(GroupIndexDocument.MemberCount, true);
                    break;

                case SortBy_Group.GrowthValue_Desc:
                    searchBuilder.SortByString(GroupIndexDocument.GrowthValue, true);
                    break;
                }
            }
            else
            {
                //时间倒序排序
                searchBuilder.SortByString(GroupIndexDocument.DateCreated, true);
            }

            return(searchBuilder);
        }
コード例 #3
0
ファイル: GroupSearcher.cs プロジェクト: ClaytonWang/Dw3cSNS
        //fixed by wanf
        /// <summary>
        /// 群组分页搜索
        /// </summary>
        /// <param name="groupQuery">搜索条件</param>
        /// <param name="interestGroup">是否是查询可能感兴趣的群组</param>
        /// <returns>符合搜索条件的分页集合</returns>
        public PagingDataSet<GroupEntity> Search(GroupFullTextQuery groupQuery, bool interestGroup = false)
        {
            if (!interestGroup)
            {
                if (string.IsNullOrWhiteSpace(groupQuery.Keyword) && !groupQuery.KeywordIsNull)
                {
                    return new PagingDataSet<GroupEntity>(new List<GroupEntity>());
                }
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(groupQuery, interestGroup);

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

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

            //解析出搜索结果中的群组ID
            List<long> groupIds = new List<long>();
            //获取索引中群组的标签
            Dictionary<long, IEnumerable<string>> groupTags = new Dictionary<long, IEnumerable<string>>();
            //获取索引中群组的分类名
            Dictionary<long, string> categoryNames = new Dictionary<long, string>();

            foreach (Document doc in docs)
            {
                long groupId = long.Parse(doc.Get(GroupIndexDocument.GroupId));
                groupIds.Add(groupId);
                groupTags[groupId]=doc.GetValues(GroupIndexDocument.Tag).ToList<string>();
                categoryNames[groupId]=doc.Get(GroupIndexDocument.CategoryName);
            }

            //根据群组ID列表批量查询群组实例
            IEnumerable<GroupEntity> groupList = groupService.GetGroupEntitiesByIds(groupIds);

            foreach (var group in groupList)
            {
                if (groupTags.ContainsKey(group.GroupId))
                {
                    group.TagNames = groupTags[group.GroupId];
                }
                if (categoryNames.ContainsKey(group.GroupId))
                {
                    group.CategoryName = categoryNames[group.GroupId];
                }
            }

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

            return GroupEntitys;
        }
コード例 #4
0
ファイル: GroupSearcher.cs プロジェクト: x1987624/SNS
        //fixed by wanf
        /// <summary>
        /// 群组分页搜索
        /// </summary>
        /// <param name="groupQuery">搜索条件</param>
        /// <param name="interestGroup">是否是查询可能感兴趣的群组</param>
        /// <returns>符合搜索条件的分页集合</returns>
        public PagingDataSet <GroupEntity> Search(GroupFullTextQuery groupQuery, bool interestGroup = false)
        {
            if (!interestGroup)
            {
                if (string.IsNullOrWhiteSpace(groupQuery.Keyword) && !groupQuery.KeywordIsNull)
                {
                    return(new PagingDataSet <GroupEntity>(new List <GroupEntity>()));
                }
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(groupQuery, interestGroup);

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

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

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

            //解析出搜索结果中的群组ID
            List <long> groupIds = new List <long>();
            //获取索引中群组的标签
            Dictionary <long, IEnumerable <string> > groupTags = new Dictionary <long, IEnumerable <string> >();
            //获取索引中群组的分类名
            Dictionary <long, string> categoryNames = new Dictionary <long, string>();

            foreach (Document doc in docs)
            {
                long groupId = long.Parse(doc.Get(GroupIndexDocument.GroupId));
                groupIds.Add(groupId);
                groupTags[groupId]     = doc.GetValues(GroupIndexDocument.Tag).ToList <string>();
                categoryNames[groupId] = doc.Get(GroupIndexDocument.CategoryName);
            }

            //根据群组ID列表批量查询群组实例
            IEnumerable <GroupEntity> groupList = groupService.GetGroupEntitiesByIds(groupIds);

            foreach (var group in groupList)
            {
                if (groupTags.ContainsKey(group.GroupId))
                {
                    group.TagNames = groupTags[group.GroupId];
                }
                if (categoryNames.ContainsKey(group.GroupId))
                {
                    group.CategoryName = categoryNames[group.GroupId];
                }
            }

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

            return(GroupEntitys);
        }