public List <SearchResult> Search(string queryString, out int totalHits) { var l = new List <SearchResult>(); // Parse the query - assuming it's not a single term but an actual query string // Note the QueryParser used is using the same analyzer used for indexing var query = queryParser.Parse(queryString); var _totalHits = 0; // Execute the search with a fresh indexSearcher searcherManager.MaybeRefreshBlocking(); searcherManager.ExecuteSearch(searcher => { var topDocs = searcher.Search(query, 10); _totalHits = topDocs.TotalHits; foreach (var result in topDocs.ScoreDocs) { var doc = searcher.Doc(result.Doc); l.Add(new SearchResult { Name = doc.GetField("name")?.StringValue, Description = doc.GetField("description")?.StringValue, Url = doc.GetField("url")?.StringValue, // Results are automatically sorted by relevance Score = result.Score, }); } }, exception => { Console.WriteLine(exception.ToString()); }); totalHits = _totalHits; return(l); }
/// <summary> /// Performs a search to find information across the whole documentation. /// </summary> /// <param name="queryString"> The query of the search.</param> /// <returns>A list which contains all the pages which match to the query.</returns> public string Search(string queryString) { if (string.IsNullOrWhiteSpace(queryString)) { return(null); } var listSearchResult = new List <Page>(); // Parse the query - assuming it's not a single term but an actual query string var query = QueryParser.Parse(queryString); // Execute the search with a fresh indexSearcher SearcherManager.MaybeRefreshBlocking(); SearcherManager.ExecuteSearch(searcher => { var topDocs = searcher.Search(query, 10); foreach (var result in topDocs.ScoreDocs) { var doc = searcher.Doc(result.Doc); listSearchResult.Add(new Page { Name = doc.GetField("name")?.StringValue, Path = doc.GetField("path")?.StringValue, }); } }, exception => { Console.WriteLine(exception.ToString()); }); var listPagesContentResult = this.SearchInPagesContent(queryString); var listPagesContentResultUpdate = new List <Page>(); foreach (var result in listPagesContentResult) { if (!listSearchResult.Exists(x => x.Path == result.Path)) { listPagesContentResultUpdate.Add(result); } } return(this.markdownToHtml.CreateHtmlPageFromResults(queryString, listSearchResult, listPagesContentResultUpdate, this.PathIndexDirectory)); }