public static FoundDocumentDto ToDto(this FoundDocument document) { if (document == null) { throw new ArgumentNullException(nameof(document)); } return(new FoundDocumentDto { Id = document.DocumentId.GetClientId(), FileName = document.FileName, Matches = document.Matches }); }
public static SummarySearchResult Search(SearchPostedData searchRequest, GlobalConfiguration globalConfiguration) { if (index == null) { return(new SummarySearchResult()); } SearchOptions searchOptions = new SearchOptions(); // Turn on fuzzy search on searchOptions.UseCaseSensitiveSearch = false; var searchQuery = searchRequest.GetQuery(); SearchResult result; foreach (char specialChar in specialCharsList) { if (searchQuery.Contains(specialChar)) { foundSpecialChars.Add(specialChar); } } if (searchQuery.Contains(" ")) { result = index.Search("\"" + searchQuery + "\"", searchOptions); } else if (foundSpecialChars.Count > 0) { foreach (char specialChar in foundSpecialChars) { searchQuery = searchQuery.Replace(specialChar, ' '); } foundSpecialChars.Clear(); result = index.Search("\"" + searchQuery + "\"", searchOptions); } else if (Path.HasExtension(searchQuery)) { searchQuery = searchQuery.Replace(".", " "); result = index.Search("\"" + searchQuery + "\"", searchOptions); } else { result = index.Search(searchQuery, searchOptions); } SummarySearchResult summaryResult = new SummarySearchResult(); List <SearchDocumentResult> foundFiles = new List <SearchDocumentResult>(); HighlightOptions options = new HighlightOptions { TermsBefore = 5, TermsAfter = 5, TermsTotal = 10, }; for (int i = 0; i < result.DocumentCount; i++) { SearchDocumentResult searchDocumentResult = new SearchDocumentResult(); FoundDocument document = result.GetFoundDocument(i); HtmlFragmentHighlighter highlighter = new HtmlFragmentHighlighter(); index.Highlight(document, highlighter, options); FragmentContainer[] fragmentContainers = highlighter.GetResult(); List <string> foundPhrases = new List <string>(); for (int j = 0; j < fragmentContainers.Length; j++) { FragmentContainer container = fragmentContainers[j]; string[] fragments = container.GetFragments(); if (fragments.Length > 0) { for (int k = 0; k < fragments.Length; k++) { foundPhrases.Add(fragments[k].Replace("<br>", string.Empty)); } } } searchDocumentResult.SetGuid(document.DocumentInfo.FilePath); searchDocumentResult.SetName(Path.GetFileName(document.DocumentInfo.FilePath)); searchDocumentResult.SetSize(new FileInfo(document.DocumentInfo.FilePath).Length); searchDocumentResult.SetOccurrences(document.OccurrenceCount); searchDocumentResult.SetFoundPhrases(foundPhrases.ToArray()); foundFiles.Add(searchDocumentResult); } summaryResult.SetFoundFiles(foundFiles.ToArray()); summaryResult.SetTotalOccurences(result.OccurrenceCount); summaryResult.SetTotalFiles(result.DocumentCount); string searchDurationString = result.SearchDuration.ToString(@"ss\.ff"); summaryResult.SetSearchDuration(searchDurationString.Equals("00.00") ? "< 1" : searchDurationString); summaryResult.SetIndexedFiles(Directory.GetFiles(globalConfiguration.GetSearchConfiguration().GetIndexedFilesDirectory(), "*", SearchOption.TopDirectoryOnly).Length); return(summaryResult); }
public FoundDocumentViewModel(LoadedIndexViewModel index, FoundDocument result) { this.index = index; this.result = result; }