Example #1
0
        public FetchResult <IEnumerable <CodeSource> > GetCodeSources(string searchQuery, bool preview, string contentQuery = "", int?showResults = 0)
        {
            FetchResult <IEnumerable <CodeSource> > result;

            try
            {
                searchQuery.RequireNotNullOrEmpty(nameof(searchQuery));

                var showResultsValue = showResults.HasValue && showResults.Value <= 100 && showResults.Value > 0 ? showResults.Value : 1000;

                result = new FetchResult <IEnumerable <CodeSource> >
                {
                    Result = SearchCodeSource(searchQuery, out var query, showResultsValue),
                    Status = new Status
                    {
                        Success = true
                    }
                };

                var queryForContent           = string.IsNullOrWhiteSpace(contentQuery) ? null : generator.GetQueryFromStr(contentQuery);
                var maxContentHighlightLength = codeIndexConfiguration.MaxContentHighlightLength;

                if (maxContentHighlightLength <= 0)
                {
                    maxContentHighlightLength = Constants.DefaultMaxContentHighlightLength;
                }

                if (preview)
                {
                    foreach (var item in result.Result)
                    {
                        item.Content = CodeIndexSearcher.GenerateHtmlPreviewText(queryForContent, item.Content, 30, generator.Analyzer, maxContentHighlightLength: maxContentHighlightLength);
                    }
                }
                else if (!preview)
                {
                    foreach (var item in result.Result)
                    {
                        item.Content = CodeIndexSearcher.GenerateHtmlPreviewText(queryForContent, item.Content, int.MaxValue, generator.Analyzer, returnRawContentWhenResultIsEmpty: true, maxContentHighlightLength: maxContentHighlightLength);
                    }
                }

                log.Debug($"Request: '{searchQuery}' successful");
            }
            catch (Exception ex)
            {
                result = new FetchResult <IEnumerable <CodeSource> >
                {
                    Status = new Status
                    {
                        Success    = false,
                        StatusDesc = ex.ToString()
                    }
                };

                log.Error(ex.ToString());
            }

            return(result);
        }
Example #2
0
        public void TestGenerateHtmlPreviewText_ContentTooLong()
        {
            var generator = new QueryGenerator();
            var content   = "My ABC\r\nIs A ABC CONTENT\r\nIt's abc in lowercase\r\nIt's Abc in mix\r\nNot AB with C";
            var result    = CodeIndexSearcher.GenerateHtmlPreviewText(generator.GetQueryFromStr("ABC"), content, int.MaxValue, LucenePool.GetAnalyzer(), maxContentHighlightLength: 20);

            Assert.AreEqual(@"Content is too long to highlight", result);
        }
Example #3
0
        public void TestGenerateHtmlPreviewText_ReturnRawContent()
        {
            var generator = new QueryGenerator();
            var content   = "My ABC\r\nIs A ABC CONTENT\r\nIt's abc in lowercase\r\nIt's Abc in mix\r\nNot AB with C";
            var result    = CodeIndexSearcher.GenerateHtmlPreviewText(generator.GetQueryFromStr("NotExistWord"), content, int.MaxValue, LucenePool.GetAnalyzer());

            Assert.IsEmpty(result);

            result = CodeIndexSearcher.GenerateHtmlPreviewText(generator.GetQueryFromStr("NotExistWord"), content, 10, LucenePool.GetAnalyzer(), returnRawContentWhenResultIsEmpty: true);
            Assert.AreEqual(HttpUtility.HtmlEncode(content), result);

            result = CodeIndexSearcher.GenerateHtmlPreviewText(null, content, 10, LucenePool.GetAnalyzer(), returnRawContentWhenResultIsEmpty: true);
            Assert.AreEqual(HttpUtility.HtmlEncode(content), result);
        }
Example #4
0
        public void TestGenerateHtmlPreviewText()
        {
            var generator = new QueryGenerator();
            var content   = "My ABC\r\nIs A ABC CONTENT\r\nIt's abc in lowercase\r\nIt's Abc in mix\r\nNot AB with C";
            var result    = CodeIndexSearcher.GenerateHtmlPreviewText(generator.GetQueryFromStr("ABC"), content, int.MaxValue, LucenePool.GetAnalyzer());

            Assert.AreEqual(@"My <label class='highlight'>ABC</label>
Is A <label class='highlight'>ABC</label> CONTENT
It&#39;s <label class='highlight'>abc</label> in lowercase
It&#39;s <label class='highlight'>Abc</label> in mix
Not AB with C", result);

            result = CodeIndexSearcher.GenerateHtmlPreviewText(generator.GetQueryFromStr("ABC"), content, 10, LucenePool.GetAnalyzer());
            Assert.AreEqual(@"My <label class='highlight'>ABC</label>
Is A <label class='highlight'>ABC</label>...
It&#39;s <label class='highlight'>Abc</label>", result);
        }