Пример #1
0
        /// <summary>
        /// 问答分页搜索
        /// </summary>
        /// <param name="askQuery">搜索条件</param>
        /// <returns>符合条件的分页集合</returns>
        public PagingDataSet <AskQuestion> Search(AskFullTextQuery askQuery)
        {
            if (string.IsNullOrWhiteSpace(askQuery.Keyword) && askQuery.IsRelation == false && askQuery.IsAlike == false)
            {
                return(new PagingDataSet <AskQuestion>(new List <AskQuestion>()));
            }
            LuceneSearchBuilder searchBuilder = BuilderLuceneSearchBuilder(askQuery);

            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, askQuery.PageIndex, askQuery.PageSize);
            IEnumerable <Document>   docs         = searchResult.ToList <Document>();

            //问题ids
            List <long> questionIds = new List <long>();

            foreach (Document doc in docs)
            {
                long questionId = long.Parse(doc.Get(AskIndexDocument.QuestionId));
                questionIds.Add(questionId);
            }

            //根据ids批量获取问题实体
            IEnumerable <AskQuestion> questionList = askService.GetQuestions(questionIds);

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

            return(questions);
        }
Пример #2
0
        /// <summary>
        /// 构建lucene查询条件
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        private LuceneSearchBuilder BuilderLuceneSearchBuilder(AskFullTextQuery query)
        {
            LuceneSearchBuilder             searchBuilder      = new LuceneSearchBuilder();
            Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>();

            if (query.IsAlike)
            {
                searchBuilder.WithPhrases(AskIndexDocument.Subject, query.Keywords, BoostLevel.Hight, false);
            }
            else if (query.IsRelation)
            {
                fieldNameAndBoosts.Add(AskIndexDocument.Subject, BoostLevel.Hight);
                fieldNameAndBoosts.Add(AskIndexDocument.Tag, BoostLevel.Hight);
                searchBuilder.WithPhrases(fieldNameAndBoosts, query.Keywords, BooleanClause.Occur.SHOULD, false);
            }
            else
            {
                //筛选
                switch (query.Range)
                {
                case AskSearchRange.SUBJECT:
                    fieldNameAndBoosts.Add(AskIndexDocument.Subject, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(AskIndexDocument.Body, BoostLevel.Medium);
                    searchBuilder.WithPhrases(fieldNameAndBoosts, query.Keyword, BooleanClause.Occur.SHOULD, false);

                    break;

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

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

                case AskSearchRange.ANSWER:
                    searchBuilder.WithPhrase(AskIndexDocument.Answer, query.Keyword, BoostLevel.Hight, false);
                    break;

                default:
                    fieldNameAndBoosts.Add(AskIndexDocument.Subject, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(AskIndexDocument.Tag, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(AskIndexDocument.Body, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(AskIndexDocument.Answer, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(AskIndexDocument.Author, BoostLevel.Low);
                    searchBuilder.WithPhrases(fieldNameAndBoosts, query.Keyword, BooleanClause.Occur.SHOULD, false);
                    break;
                }
            }

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

            case PubliclyAuditStatus.Again_GreaterThanOrEqual:
            case PubliclyAuditStatus.Pending_GreaterThanOrEqual:
                searchBuilder.WithinRange(AskIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), ((int)PubliclyAuditStatus.Success).ToString(), true);
                break;
            }
            if (query.sortBy == SortBy_AskQuestion.DateCreated_Desc)
            {
                searchBuilder.SortByString(AskIndexDocument.DateCreated, true);
            }

            return(searchBuilder);
        }