Example #1
0
        public async Task <ActionResult> ResultsAsHtml([FromUri(Name = "q")] string searchTerm)
        {
            try
            {
                Requests.LogRequest(this, searchTerm);
                searchTerm = HttpUtility.UrlDecode(searchTerm);

                if (string.IsNullOrWhiteSpace(searchTerm))
                {
                    //Still render view even if we have an invalid search term - it'll display a "results not found" message
                    Debug.WriteLine("GetSearchResult - searchTerm is null or whitespace");
                    return(PartialView());
                }

                if (searchTerm.StartsWith("`"))
                {
                    return(await TextSearchResults(searchTerm));
                }

                SymbolSearchResult searchResult = null;
                string             term;
                Classification?    classification;
                ParseSearchTerm(searchTerm, out term, out classification);

                Responses.PrepareResponse(Response);

                searchResult = await storage.SearchAsync(this.GetSearchRepos(), searchTerm);

                if (searchResult.Total == 0)
                {
                    return(await TextSearchResults(searchTerm));
                }

                return(PartialView(searchResult));
            }
            catch (Exception ex)
            {
                return(Responses.Exception(ex));
            }
        }
Example #2
0
        static void Search()
        {
            ElasticsearchStorage storage = new ElasticsearchStorage(elasticSearchServer);

            string[] repos = new string[] { repoName.ToLowerInvariant() };

            string line = null;

            Console.WriteLine("Please enter symbol short name: ");
            while ((line = Console.ReadLine()) != null)
            {
                if (line.Contains("`"))
                {
                    //results = storage.SearchAsync(repos, line, classification: null).Result;
                    var results = ((ElasticsearchStorage)storage).TextSearchAsync(repos, line.TrimStart('`')).Result;
                    Console.WriteLine($"Found {results.Count} matches");
                    foreach (var result in results)
                    {
                        Console.WriteLine($"{result.File} ({result.Span.LineNumber}, {result.Span.LineSpanStart})");
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine($"{result.ReferringFilePath} in '{result.ReferringProjectId}'");
                        Console.ForegroundColor = ConsoleColor.Gray;

                        var bsf = storage.GetBoundSourceFileAsync(result.ReferringProjectId, result.ReferringFilePath).Result;

                        if (!string.IsNullOrEmpty(result.Span.LineSpanText))
                        {
                            Console.Write(result.Span.LineSpanText.Substring(0, result.Span.LineSpanStart));
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.Write(result.Span.LineSpanText.Substring(result.Span.LineSpanStart, result.Span.Length));
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine(result.Span.LineSpanText.Substring(result.Span.LineSpanStart + result.Span.Length));
                        }
                    }
                }
                else if (line.Contains("|"))
                {
                    var parts     = line.Split('|');
                    var symbolId  = SymbolId.UnsafeCreateWithValue(parts[0]);
                    var projectId = parts[1];

                    var results = storage.GetReferencesToSymbolAsync(repos, new Symbol()
                    {
                        Id = symbolId, ProjectId = projectId
                    }).GetAwaiter().GetResult();

                    var relatedDefinitions = storage.GetRelatedDefinitions(repos,
                                                                           symbolId.Value,
                                                                           projectId).GetAwaiter().GetResult();

                    var definition = results.Entries
                                     .Where(e => e.Span.Reference.ReferenceKind == nameof(ReferenceKind.Definition))
                                     .Select(e => e.Span.Reference)
                                     .FirstOrDefault();

                    if (definition != null)
                    {
                        var relatedDefs = storage.Provider.GetRelatedDefinitions(repos, definition.Id.Value, definition.ProjectId)
                                          .GetAwaiter().GetResult();
                    }

                    Console.WriteLine($"Found {results.Total} matches");
                    foreach (var result in results.Entries)
                    {
                        Console.WriteLine($"{result.File} ({result.Span.LineNumber}, {result.Span.LineSpanStart})");
                        if (!string.IsNullOrEmpty(result.Span.LineSpanText))
                        {
                            Console.Write(result.Span.LineSpanText.Substring(0, result.Span.LineSpanStart));
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.Write(result.Span.LineSpanText.Substring(result.Span.LineSpanStart, result.Span.Length));
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine(result.Span.LineSpanText.Substring(result.Span.LineSpanStart + result.Span.Length));
                        }
                    }

                    if (results.Entries.Count != 0)
                    {
                        var result = results.Entries[0];
                        Console.WriteLine($"Retrieving source file {result.ReferringFilePath} in {result.ReferringProjectId}");

                        var stopwatch  = Stopwatch.StartNew();
                        var sourceFile = storage.GetBoundSourceFileAsync(repos, result.ReferringProjectId, result.ReferringFilePath).GetAwaiter().GetResult();
                        var elapsed    = stopwatch.Elapsed;

                        Console.WriteLine($"Retrieved source file in {elapsed.TotalMilliseconds} ms");

                        Console.WriteLine($"Source file has { sourceFile?.ClassificationSpans.Count ?? -1 } classifications");
                        if (sourceFile.ClassificationSpans != null)
                        {
                            ConcurrentDictionary <string, int> classificationCounters = new ConcurrentDictionary <string, int>();
                            foreach (var cs in sourceFile.ClassificationSpans)
                            {
                                classificationCounters.AddOrUpdate(cs.Classification, 1, (k, v) => v + 1);
                            }

                            foreach (var counter in classificationCounters)
                            {
                                Console.WriteLine($"Source file has {counter.Value} {counter.Key} classifications");
                            }
                        }

                        Console.WriteLine($"Source file has { sourceFile?.References.Count ?? -1 } references");
                        Console.WriteLine($"Source file has { sourceFile?.Definitions.Count ?? -1 } definitions");
                    }
                }
                else
                {
                    //results = storage.SearchAsync(repos, line, classification: null).Result;
                    var results = storage.SearchAsync(repos, line, null).Result;
                    Console.WriteLine($"Found {results.Total} matches");
                    foreach (var result in results.Entries)
                    {
                        Console.WriteLine($"{result.File} ({result.Span.LineNumber}, {result.Span.LineSpanStart})");
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine($"{result.Symbol.Id}|{result.Symbol.ProjectId}");
                        Console.ForegroundColor = ConsoleColor.Gray;

                        var symbol = result.Symbol;
                        int index  = result.DisplayName.IndexOf(symbol.ShortName);
                        if (index >= 0)
                        {
                            result.Span.LineSpanText  = symbol.DisplayName;
                            result.Span.LineSpanStart = index;
                            result.Span.Length        = symbol.ShortName.Length;
                        }

                        if (!string.IsNullOrEmpty(result.Span.LineSpanText))
                        {
                            Console.Write(result.Span.LineSpanText.Substring(0, result.Span.LineSpanStart));
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            Console.Write(result.Span.LineSpanText.Substring(result.Span.LineSpanStart, result.Span.Length));
                            Console.ForegroundColor = ConsoleColor.Gray;
                            Console.WriteLine(result.Span.LineSpanText.Substring(result.Span.LineSpanStart + result.Span.Length));
                        }
                    }
                }
            }
        }