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);
        }
        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;
        }
Esempio n. 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);
        }