Пример #1
0
        public LuceneResults Search(LuceneSearchContext searchContext)
        {
            Requires.NotNull("LuceneQuery", searchContext.LuceneQuery);
            Requires.NotNull("LuceneQuery.Query", searchContext.LuceneQuery.Query);
            Requires.PropertyNotEqualTo("LuceneQuery", "PageSize", searchContext.LuceneQuery.PageSize, 0);
            Requires.PropertyNotEqualTo("LuceneQuery", "PageIndex", searchContext.LuceneQuery.PageIndex, 0);

            var luceneResults = new LuceneResults();
            //TODO - Explore simple highlighter as it does not give partial words
            var highlighter = FastHighlighter;
            var fieldQuery  = highlighter.GetFieldQuery(searchContext.LuceneQuery.Query);

            var maxResults = searchContext.LuceneQuery.PageIndex * searchContext.LuceneQuery.PageSize;
            var minResults = maxResults - searchContext.LuceneQuery.PageSize + 1;

            var searcher = GetSearcher();
            var searchSecurityTrimmer = new SearchSecurityTrimmer(new SearchSecurityTrimmerContext
            {
                Searcher        = searcher,
                SecurityChecker = searchContext.SecurityCheckerDelegate,
                LuceneQuery     = searchContext.LuceneQuery,
                SearchQuery     = searchContext.SearchQuery
            });

            searcher.Search(searchContext.LuceneQuery.Query, null, searchSecurityTrimmer);
            luceneResults.TotalHits = searchSecurityTrimmer.TotalHits;

            if (Logger.IsDebugEnabled)
            {
                var sb = GetSearcResultExplanation(searchContext.LuceneQuery, searchSecurityTrimmer.ScoreDocs, searcher);
                Logger.Trace(sb);
            }

            //Page doesn't exist
            if (luceneResults.TotalHits < minResults)
            {
                return(luceneResults);
            }

            luceneResults.Results = searchSecurityTrimmer.ScoreDocs.Select(match =>
                                                                           new LuceneResult
            {
                Document           = searcher.Doc(match.Doc),
                Score              = match.Score,
                DisplayScore       = GetDisplayScoreFromMatch(match.ToString()),
                TitleSnippet       = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.TitleTag, searchContext.LuceneQuery.TitleSnippetLength),
                BodySnippet        = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.BodyTag, searchContext.LuceneQuery.BodySnippetLength),
                DescriptionSnippet = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.DescriptionTag, searchContext.LuceneQuery.TitleSnippetLength),
                TagSnippet         = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.Tag, searchContext.LuceneQuery.TitleSnippetLength),
                AuthorSnippet      = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.AuthorNameTag, searchContext.LuceneQuery.TitleSnippetLength),
                ContentSnippet     = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.ContentTag, searchContext.LuceneQuery.TitleSnippetLength)
            }).ToList();
            return(luceneResults);
        }
Пример #2
0
        public LuceneResults Search(LuceneSearchContext searchContext)
        {
            Requires.NotNull("LuceneQuery", searchContext.LuceneQuery);
            Requires.NotNull("LuceneQuery.Query", searchContext.LuceneQuery.Query);
            Requires.PropertyNotEqualTo("LuceneQuery", "PageSize", searchContext.LuceneQuery.PageSize, 0);
            Requires.PropertyNotEqualTo("LuceneQuery", "PageIndex", searchContext.LuceneQuery.PageIndex, 0);

            var luceneResults = new LuceneResults();

            //validate whether index folder is exist and contains index files, otherwise return null.
            if (!ValidateIndexFolder())
            {
                return luceneResults;
            }

            //TODO - Explore simple highlighter as it does not give partial words
            var highlighter = FastHighlighter;
            var fieldQuery = highlighter.GetFieldQuery(searchContext.LuceneQuery.Query);

            var maxResults = searchContext.LuceneQuery.PageIndex * searchContext.LuceneQuery.PageSize;
            var minResults = maxResults - searchContext.LuceneQuery.PageSize + 1;

            var searcher = GetSearcher();
            var searchSecurityTrimmer = new SearchSecurityTrimmer(new SearchSecurityTrimmerContext
                {
                    Searcher = searcher,
                    SecurityChecker = searchContext.SecurityCheckerDelegate,
                    LuceneQuery = searchContext.LuceneQuery,
                    SearchQuery = searchContext.SearchQuery
                });
            searcher.Search(searchContext.LuceneQuery.Query, null, searchSecurityTrimmer);
            luceneResults.TotalHits = searchSecurityTrimmer.TotalHits;

            if (Logger.IsDebugEnabled)
            {
                var sb = GetSearcResultExplanation(searchContext.LuceneQuery, searchSecurityTrimmer.ScoreDocs, searcher);
		        Logger.Trace(sb);
            }

            //Page doesn't exist
            if (luceneResults.TotalHits < minResults)
                return luceneResults;

            luceneResults.Results  = searchSecurityTrimmer.ScoreDocs.Select(match =>
                new LuceneResult
                    {
                        Document = searcher.Doc(match.Doc),
                        Score = match.Score,
                        DisplayScore = GetDisplayScoreFromMatch(match.ToString()),
                        TitleSnippet = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.TitleTag, searchContext.LuceneQuery.TitleSnippetLength),
                        BodySnippet = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.BodyTag, searchContext.LuceneQuery.BodySnippetLength),
                        DescriptionSnippet = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.DescriptionTag, searchContext.LuceneQuery.TitleSnippetLength),
                        TagSnippet = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.Tag, searchContext.LuceneQuery.TitleSnippetLength),
                        AuthorSnippet = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.AuthorNameTag, searchContext.LuceneQuery.TitleSnippetLength),
                        ContentSnippet = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.ContentTag, searchContext.LuceneQuery.TitleSnippetLength)
                    }).ToList();
            return luceneResults;
        }
Пример #3
0
        public LuceneResults Search(LuceneSearchContext searchContext)
        {
            Requires.NotNull("LuceneQuery", searchContext.LuceneQuery);
            Requires.NotNull("LuceneQuery.Query", searchContext.LuceneQuery.Query);
            Requires.PropertyNotEqualTo("LuceneQuery", "PageSize", searchContext.LuceneQuery.PageSize, 0);
            Requires.PropertyNotEqualTo("LuceneQuery", "PageIndex", searchContext.LuceneQuery.PageIndex, 0);

            var luceneResults = new LuceneResults();

            // validate whether index folder is exist and contains index files, otherwise return null.
            if (!this.ValidateIndexFolder())
            {
                return(luceneResults);
            }

            var highlighter = this.FastHighlighter;
            var fieldQuery  = highlighter.GetFieldQuery(searchContext.LuceneQuery.Query);

            var maxResults = searchContext.LuceneQuery.PageIndex * searchContext.LuceneQuery.PageSize;
            var minResults = maxResults - searchContext.LuceneQuery.PageSize + 1;

            for (var i = 0; i < this._searchRetryTimes; i++)
            {
                try
                {
                    var searcher = this.GetSearcher();
                    var searchSecurityTrimmer = new SearchSecurityTrimmer(new SearchSecurityTrimmerContext
                    {
                        Searcher        = searcher,
                        SecurityChecker = searchContext.SecurityCheckerDelegate,
                        LuceneQuery     = searchContext.LuceneQuery,
                        SearchQuery     = searchContext.SearchQuery,
                    });
                    searcher.Search(searchContext.LuceneQuery.Query, null, searchSecurityTrimmer);
                    luceneResults.TotalHits = searchSecurityTrimmer.TotalHits;

                    if (Logger.IsDebugEnabled)
                    {
                        var sb = GetSearcResultExplanation(searchContext.LuceneQuery, searchSecurityTrimmer.ScoreDocs, searcher);
                        Logger.Trace(sb);
                    }

                    // Page doesn't exist
                    if (luceneResults.TotalHits < minResults)
                    {
                        break;
                    }

                    luceneResults.Results = searchSecurityTrimmer.ScoreDocs.Select(match =>
                                                                                   new LuceneResult
                    {
                        Document           = searcher.Doc(match.Doc),
                        Score              = match.Score,
                        DisplayScore       = this.GetDisplayScoreFromMatch(match.ToString()),
                        TitleSnippet       = this.GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.TitleTag, searchContext.LuceneQuery.TitleSnippetLength),
                        BodySnippet        = this.GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.BodyTag, searchContext.LuceneQuery.BodySnippetLength),
                        DescriptionSnippet = this.GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.DescriptionTag, searchContext.LuceneQuery.TitleSnippetLength),
                        TagSnippet         = this.GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.Tag, searchContext.LuceneQuery.TitleSnippetLength),
                        AuthorSnippet      = this.GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.AuthorNameTag, searchContext.LuceneQuery.TitleSnippetLength),
                        ContentSnippet     = this.GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.ContentTag, searchContext.LuceneQuery.TitleSnippetLength),
                    }).ToList();
                    break;
                }
                catch (Exception ex) when(ex is IOException || ex is AlreadyClosedException)
                {
                    this.DisposeReaders();
                    this.DisposeWriter(false);

                    if (i == this._searchRetryTimes - 1)
                    {
                        throw;
                    }

                    Logger.Error(ex);
                    Logger.Error($"Search Index Folder Is Not Available: {ex.Message}, Retry {i + 1} time(s).");
                    Thread.Sleep(100);
                }
            }

            return(luceneResults);
        }
Пример #4
0
        public IEnumerable<LuceneResult> Search(
            LuceneQuery luceneQuery, out int totalHits, SecurityCheckerDelegate securityChecker = null)
        {
            Requires.NotNull("LuceneQuery", luceneQuery);
            Requires.NotNull("LuceneQuery.Query", luceneQuery.Query);
            Requires.PropertyNotEqualTo("LuceneQuery", "PageSize", luceneQuery.PageSize, 0);
            Requires.PropertyNotEqualTo("LuceneQuery", "PageIndex", luceneQuery.PageIndex, 0);

            //TODO - Explore simple highlighter as it does not give partial words
            var highlighter = FastHighlighter;
            var fieldQuery = highlighter.GetFieldQuery(luceneQuery.Query);

            var maxResults = luceneQuery.PageIndex*luceneQuery.PageSize;
            var minResults = maxResults - luceneQuery.PageSize + 1;

            var searcher = GetSearcher();
            var topDocs = new SearchSecurityTrimmer(searcher, securityChecker, luceneQuery);
            searcher.Search(luceneQuery.Query, null, topDocs);
            totalHits = topDocs.TotalHits;

            if (Logger.IsDebugEnabled)
            {
                var sb = GetSearcResultExplanation(luceneQuery, topDocs.ScoreDocs, searcher);
                Logger.Debug(sb);
            }

            //Page doesn't exist
            if (totalHits < minResults)
                return new List<LuceneResult>();

            return topDocs.ScoreDocs.Select(match =>
                new LuceneResult
                    {
                        Document = searcher.Doc(match.Doc),
                        Score = match.Score,
                        DisplayScore = GetDisplayScoreFromMatch(match.ToString()),
                        TitleSnippet = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.TitleTag, luceneQuery.TitleSnippetLength),
                        BodySnippet = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.BodyTag, luceneQuery.BodySnippetLength),
                        DescriptionSnippet = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.DescriptionTag, luceneQuery.TitleSnippetLength),
                        TagSnippet = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.Tag, luceneQuery.TitleSnippetLength),
                        AuthorSnippet = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.AuthorNameTag, luceneQuery.TitleSnippetLength),
                        ContentSnippet = GetHighlightedText(highlighter, fieldQuery, searcher, match, Constants.ContentTag, luceneQuery.TitleSnippetLength)
                    }).ToList();
        }