/// <summary> /// Performs a search in the wiki. /// </summary> /// <param name="query">The search query.</param> /// <param name="fullText">A value indicating whether to perform a full-text search.</param> /// <param name="searchFilesAndAttachments">A value indicating whether to search through files and attachments.</param> /// <param name="options">The search options.</param> /// <returns>The results collection.</returns> public static SearchResultCollection Search(string query, bool fullText, bool searchFilesAndAttachments, SearchOptions options) { // First, search regular page content... List<SearchResultCollection> allCollections = new List<SearchResultCollection>(3); foreach(IPagesStorageProviderV30 prov in Collectors.PagesProviderCollector.AllProviders) { SearchResultCollection currentResults = prov.PerformSearch(new SearchParameters(query, options)); if(!fullText) { // All non title-related matches must be removed SearchResultCollection filteredResults = new SearchResultCollection(10); foreach(SearchResult res in currentResults) { foreach(WordInfo word in res.Matches) { if(word.Location == WordLocation.Title) { filteredResults.Add(res); break; } } } allCollections.Add(filteredResults); } else allCollections.Add(currentResults); } // ... normalize relevance based on the number of providers float providerNormalizationFactor = 1F / (float)Collectors.PagesProviderCollector.AllProviders.Length; foreach(SearchResultCollection coll in allCollections) { foreach(SearchResult result in coll) { result.Relevance.NormalizeAfterFinalization(providerNormalizationFactor); } } if(searchFilesAndAttachments) { // ... then build a temporary index for files and attachments... StandardIndex temporaryIndex = new StandardIndex(); uint tempDocumentId = 1; uint tempWordId = 1; temporaryIndex.IndexChanged += delegate(object sender, IndexChangedEventArgs e) { if(e.Change == IndexChangeType.DocumentAdded) { List<WordId> ids = null; if(e.ChangeData.Words != null) { ids = new List<WordId>(20); foreach(DumpedWord d in e.ChangeData.Words) { ids.Add(new WordId(d.Text, tempWordId)); tempWordId++; } } e.Result = new IndexStorerResult(tempDocumentId, ids); tempDocumentId++; } }; temporaryIndex.SetBuildDocumentDelegate(DetectFileOrAttachment); foreach(IFilesStorageProviderV30 prov in Collectors.FilesProviderCollector.AllProviders) { TraverseDirectories(temporaryIndex, prov, null); string[] pagesWithAttachments = prov.GetPagesWithAttachments(); foreach(string page in pagesWithAttachments) { // Store attachments for the current page in the index PageInfo pageInfo = Pages.FindPage(page); // pageInfo can be null if the index is corrupted if(pageInfo != null) { foreach(string attachment in prov.ListPageAttachments(pageInfo)) { FileDetails details = prov.GetPageAttachmentDetails(pageInfo, attachment); temporaryIndex.StoreDocument(new PageAttachmentDocument(pageInfo, attachment, prov.GetType().FullName, details.LastModified), new string[0], "", null); } } } } // ... then search in the temporary index and normalize relevance SearchResultCollection filesAndAttachments = temporaryIndex.Search(new SearchParameters(query, options)); providerNormalizationFactor = 1F / (float)Collectors.FilesProviderCollector.AllProviders.Length; foreach(SearchResult result in filesAndAttachments) { result.Relevance.NormalizeAfterFinalization(providerNormalizationFactor); } allCollections.Add(filesAndAttachments); } return CombineCollections(allCollections); }
/// <summary> /// Performs a search in the wiki. /// </summary> /// <param name="query">The search query.</param> /// <param name="fullText">A value indicating whether to perform a full-text search.</param> /// <param name="searchFilesAndAttachments">A value indicating whether to search through files and attachments.</param> /// <param name="options">The search options.</param> /// <returns>The results collection.</returns> public static SearchResultCollection Search(string query, bool fullText, bool searchFilesAndAttachments, SearchOptions options) { // First, search regular page content... var allCollections = new List <SearchResultCollection>(3); foreach (IPagesStorageProviderV30 prov in Collectors.PagesProviderCollector.AllProviders) { SearchResultCollection currentResults = prov.PerformSearch(new SearchParameters(query, options)); if (!fullText) { // All non title-related matches must be removed var filteredResults = new SearchResultCollection(10); foreach (SearchResult res in currentResults) { foreach (WordInfo word in res.Matches) { if (word.Location == WordLocation.Title) { filteredResults.Add(res); break; } } } allCollections.Add(filteredResults); } else { allCollections.Add(currentResults); } } // ... normalize relevance based on the number of providers var providerNormalizationFactor = 1F / Collectors.PagesProviderCollector.AllProviders.Count; foreach (SearchResultCollection coll in allCollections) { foreach (SearchResult result in coll) { result.Relevance.NormalizeAfterFinalization(providerNormalizationFactor); } } if (searchFilesAndAttachments) { // ... then build a temporary index for files and attachments... var temporaryIndex = new StandardIndex(); uint tempDocumentId = 1; uint tempWordId = 1; temporaryIndex.IndexChanged += delegate(object sender, IndexChangedEventArgs e) { if (e.Change == IndexChangeType.DocumentAdded) { List <WordId> ids = null; if (e.ChangeData.Words != null) { ids = new List <WordId>(20); foreach (DumpedWord d in e.ChangeData.Words) { ids.Add(new WordId(d.Text, tempWordId)); tempWordId++; } } e.Result = new IndexStorerResult(tempDocumentId, ids); tempDocumentId++; } }; temporaryIndex.SetBuildDocumentDelegate(DetectFileOrAttachment); foreach (IFilesStorageProviderV30 prov in Collectors.FilesProviderCollector.AllProviders) { TraverseDirectories(temporaryIndex, prov, null); var pagesWithAttachments = prov.GetPagesWithAttachments(); foreach (var page in pagesWithAttachments) { // Store attachments for the current page in the index PageInfo pageInfo = Pages.FindPage(page); // pageInfo can be null if the index is corrupted if (pageInfo != null) { foreach (var attachment in prov.ListPageAttachments(pageInfo)) { FileDetails details = prov.GetPageAttachmentDetails(pageInfo, attachment); temporaryIndex.StoreDocument(new PageAttachmentDocument(pageInfo, attachment, prov.GetType().FullName, details.LastModified), new string[0], "", null); } } } } // ... then search in the temporary index and normalize relevance SearchResultCollection filesAndAttachments = temporaryIndex.Search(new SearchParameters(query, options)); providerNormalizationFactor = 1F / Collectors.FilesProviderCollector.AllProviders.Count; foreach (SearchResult result in filesAndAttachments) { result.Relevance.NormalizeAfterFinalization(providerNormalizationFactor); } allCollections.Add(filesAndAttachments); } return(CombineCollections(allCollections)); }