Пример #1
0
        /// <summary>
        /// 照片搜索
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagingDataSet <Photo> Search(PhotoFullTextQuery query)
        {
            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(query);

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

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

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

            //解析出PhotoId,组装Photo实体
            List <long> photoIds = new List <long>();

            foreach (var doc in docs)
            {
                long photoId = long.Parse(doc.Get(PhotoIndexDocument.PhotoId));
                photoIds.Add(photoId);
            }
            IEnumerable <Photo> photoList = photoService.GetPhotos(photoIds);

            //组装分页集合
            PagingDataSet <Photo> photos = new PagingDataSet <Photo>(photoList)
            {
                TotalRecords  = searchResult.TotalRecords,
                PageIndex     = searchResult.PageIndex,
                PageSize      = searchResult.PageSize,
                QueryDuration = searchResult.QueryDuration
            };

            return(photos);
        }
Пример #2
0
        /// <summary>
        /// 根据照片搜索条件构建lucene查询条件
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>lucene条件</returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(PhotoFullTextQuery query)
        {
            LuceneSearchBuilder             searchBuilder      = new LuceneSearchBuilder();
            Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>();

            if (query.UserId > 0)
            {
                searchBuilder.WithField(PhotoIndexDocument.UserId, query.UserId.ToString(), true, BoostLevel.Hight, false);
            }

            if (!string.IsNullOrEmpty(query.Keyword))
            {
                switch (query.Filter)
                {
                case PhotoSearchRange.DESCRIPTION:
                    searchBuilder.WithPhrase(PhotoIndexDocument.Description, query.Keyword, BoostLevel.Hight, false);
                    break;

                case PhotoSearchRange.AUTHOR:
                    searchBuilder.WithPhrase(PhotoIndexDocument.Author, query.Keyword, BoostLevel.Hight, false);
                    break;

                case PhotoSearchRange.TAG:
                    searchBuilder.WithPhrase(PhotoIndexDocument.Tag, query.Keyword, BoostLevel.Hight, false);
                    break;

                default:
                    fieldNameAndBoosts.Add(PhotoIndexDocument.Tag, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(PhotoIndexDocument.Description, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(PhotoIndexDocument.Author, BoostLevel.Medium);
                    searchBuilder.WithPhrases(fieldNameAndBoosts, query.Keyword, BooleanClause.Occur.SHOULD, false);
                    break;
                }
            }

            //筛选 全部、某人的照片
            if (query.UserId > 0)
            {
                searchBuilder.WithField(PhotoIndexDocument.UserId, query.UserId.ToString(), true, BoostLevel.Hight, true);
            }

            //过滤审核状态和隐私状态
            if (!query.IgnoreAuditAndPrivacy)
            {
                searchBuilder.NotWithField(PhotoIndexDocument.PrivacyStatus, ((int)PrivacyStatus.Private).ToString());
                switch (publiclyAuditStatus)
                {
                case PubliclyAuditStatus.Again:
                case PubliclyAuditStatus.Fail:
                case PubliclyAuditStatus.Pending:
                case PubliclyAuditStatus.Success:
                    searchBuilder.WithField(PhotoIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), true, BoostLevel.Hight, true);
                    break;

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

            return(searchBuilder);
        }