コード例 #1
0
ファイル: LuceneSearchBuilder.cs プロジェクト: x1987624/SNS
        /// <summary>
        /// 根据多个关键字添加PhraseQuery
        /// </summary>
        /// <param name="fieldName">待搜索的字段名称</param>
        /// <param name="phrases">待搜索的短语列表</param>
        /// <param name="boostLevel">权重级别</param>
        /// <param name="asFilter">是否作为过滤条件</param>
        /// <returns>LuceneSearchBuilder</returns>
        public LuceneSearchBuilder WithPhrases(string fieldName, IEnumerable <string> phrases, BoostLevel?boostLevel = null, bool asFilter = false)
        {
            BooleanQuery query = new BooleanQuery();

            foreach (string phrase in phrases)
            {
                string filteredPhrase = ClauseScrubber.LuceneKeywordsScrub(phrase);
                if (string.IsNullOrEmpty(filteredPhrase))
                {
                    continue;
                }

                if (filteredPhrase.Length == 1)
                {
                    Term  term = new Term(fieldName, filteredPhrase);
                    Query q    = new PrefixQuery(term);

                    if (boostLevel.HasValue)
                    {
                        SetBoost(q, boostLevel.Value);
                    }

                    query.Add(q, BooleanClause.Occur.SHOULD);

                    continue;
                }

                string[] nameSegments = ClauseScrubber.SegmentForPhraseQuery(filteredPhrase);

                PhraseQuery phraseQuery = new PhraseQuery();
                foreach (var nameSegment in nameSegments)
                {
                    phraseQuery.Add(new Term(fieldName, nameSegment));
                }

                phraseQuery.SetSlop(PhraseQuerySlop);

                if (boostLevel.HasValue)
                {
                    SetBoost(phraseQuery, boostLevel.Value);
                }

                query.Add(phraseQuery, BooleanClause.Occur.SHOULD);
            }


            if (asFilter)
            {
                filters.Add(new BooleanClause(query, BooleanClause.Occur.MUST));
            }
            else
            {
                clauses.Add(new BooleanClause(query, BooleanClause.Occur.MUST));
            }

            return(this);
        }
コード例 #2
0
ファイル: LuceneSearchBuilder.cs プロジェクト: x1987624/SNS
        /// <summary>
        /// 批量添加PhraseQuery
        /// </summary>
        /// <param name="phrase">待搜索的短语</param>
        /// <param name="fieldNameAndBoosts">字段名称及权重集合</param>
        /// <param name="occur">搜索条件间的关系</param>
        /// <param name="asFilter">是否作为过滤器条件</param>
        /// <returns></returns>
        public LuceneSearchBuilder WithPhrases(Dictionary <string, BoostLevel> fieldNameAndBoosts, string phrase, BooleanClause.Occur occur, bool asFilter = false)
        {
            string filteredPhrase = ClauseScrubber.LuceneKeywordsScrub(phrase);

            if (string.IsNullOrEmpty(filteredPhrase))
            {
                return(this);
            }

            string[] nameSegments = ClauseScrubber.SegmentForPhraseQuery(filteredPhrase);
            if (nameSegments.Length == 1)
            {
                return(WithFields(fieldNameAndBoosts, nameSegments[0], false, occur, asFilter));
            }
            else
            {
                BooleanQuery query = new BooleanQuery();
                foreach (var fieldNameAndBoost in fieldNameAndBoosts)
                {
                    PhraseQuery phraseQuery = new PhraseQuery();
                    foreach (var nameSegment in nameSegments)
                    {
                        phraseQuery.Add(new Term(fieldNameAndBoost.Key, nameSegment));
                    }

                    phraseQuery.SetSlop(PhraseQuerySlop);
                    SetBoost(phraseQuery, fieldNameAndBoost.Value);
                    query.Add(phraseQuery, occur);
                }

                if (asFilter)
                {
                    filters.Add(new BooleanClause(query, BooleanClause.Occur.MUST));
                }
                else
                {
                    clauses.Add(new BooleanClause(query, BooleanClause.Occur.MUST));
                }

                return(this);
            }
        }
コード例 #3
0
ファイル: LuceneSearchBuilder.cs プロジェクト: x1987624/SNS
        /// <summary>
        /// 添加PhraseQuery
        /// </summary>
        /// <param name="fieldName">待搜索的字段名称</param>
        /// <param name="phrase">待搜索的短语</param>
        /// <param name="boostLevel">权重级别</param>
        /// <param name="asFilter">是否作为过滤条件</param>
        /// <returns>LuceneSearchBuilder</returns>
        public LuceneSearchBuilder WithPhrase(string fieldName, string phrase, BoostLevel?boostLevel = null, bool asFilter = false)
        {
            string filteredPhrase = ClauseScrubber.LuceneKeywordsScrub(phrase);

            if (string.IsNullOrEmpty(filteredPhrase))
            {
                return(this);
            }

            if (filteredPhrase.Length == 1)
            {
                return(WithField(fieldName, filteredPhrase, false, boostLevel, asFilter));
            }

            string[] nameSegments = ClauseScrubber.SegmentForPhraseQuery(filteredPhrase);

            PhraseQuery phraseQuery = new PhraseQuery();

            foreach (var nameSegment in nameSegments)
            {
                phraseQuery.Add(new Term(fieldName, nameSegment));
            }

            phraseQuery.SetSlop(PhraseQuerySlop);

            if (boostLevel.HasValue)
            {
                SetBoost(phraseQuery, boostLevel.Value);
            }

            if (asFilter)
            {
                filters.Add(new BooleanClause(phraseQuery, BooleanClause.Occur.MUST));
            }
            else
            {
                clauses.Add(new BooleanClause(phraseQuery, BooleanClause.Occur.MUST));
            }

            return(this);
        }