public static Search ( string queryString, IEnumerable |
||
queryString | string | |
indexers | IEnumerable |
|
maxResults | int | |
return |
/// <summary> /// Executes the search using the currently entered text /// </summary> private void LaunchSearch(bool interactive) { if (indexes.Count == 0) { return; } searchStatusLabel.Text = String.Format(Properties.Resources.SearchingForTerm, searchBox.Text); searchLaunched = true; searchIdentifier++; if (!interactive) { SearchItem si = new SearchItem(); si.SearchIdentifier = searchIdentifier; si.SearchText = searchBox.Text; ThreadPool.QueueUserWorkItem(BackgroundSearch, si); return; } HitCollection hits = Indexer.Search(searchBox.Text, indexes.Values, Indexer.MAX_SEARCH_HITS); searchStatusLabel.Text = String.Empty; if (String.IsNullOrEmpty(hits.ErrorMessages)) { loadingResults = true; hitsBox.DataSource = hits; hitsBox.SelectedItem = null; loadingResults = false; if (hits.HadMoreHits) { searchStatusLabel.Text = String.Format(Properties.Resources.ShowingXTopResults, Indexer.MAX_SEARCH_HITS.ToString()); } if (hits.Count > 0) { hitsBox.SetSelected(0, true); PageInfo page = hitsBox.SelectedItem as PageInfo; webBrowser.Navigate(WebServer.Instance.GenerateUrl(page)); } } else { MessageBox.Show(this, hits.ErrorMessages, Properties.Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Does the Lucene search in background and returns the results, if any /// </summary> /// <param name="state">The state object</param> private void BackgroundSearch(object state) { SearchItem si = (SearchItem)state; try { si.Hits = Indexer.Search(si.SearchText, indexes.Values, Indexer.MAX_SEARCH_HITS); } catch (Exception ex) { si.Hits = new HitCollection(); si.Hits.ErrorMessages = ex.Message; } Invoke(new BackgroundSearchFinishedDelegate(BackgroundSearchFinished), si); }
/// <summary> /// Gets called whenever the browser control requests a URL from the web server /// </summary> /// <param name="sender">Web server instance</param> /// <param name="e">Request parameters</param> private void Web_UrlRequested(object sender, UrlRequestedEventArgs e) { string response = "Not found"; string redirect = String.Empty; if (!String.IsNullOrEmpty(e.TeXEquation)) { return; } string topic = e.TopicName.Replace('_', ' ').Trim(); if (topic.Contains("#")) { topic = topic.Substring(0, topic.IndexOf('#')).Trim(); } PageInfo page = hitsBox.SelectedItem as PageInfo; if (page != null && topic.Equals(page.Name, StringComparison.InvariantCultureIgnoreCase) && e.IndexName.Equals(page.Indexer.File, StringComparison.InvariantCultureIgnoreCase) && !IsCircularRedirect(page)) { response = page.GetFormattedContent(); redirect = page.RedirectToTopic; } else { List <Indexer> searchArea = new List <Indexer>(); // This is an internal link if (String.IsNullOrEmpty(e.IndexName)) { if (page != null) { searchArea.Add(page.Indexer); } else { foreach (Indexer ixr in indexes.Values) { searchArea.Add(ixr); } } } else { foreach (Indexer ixr in indexes.Values) { if (ixr.File.Equals(e.IndexName, StringComparison.InvariantCultureIgnoreCase)) { searchArea.Add(ixr); break; } } } if (searchArea.Count > 0) { HitCollection hits = Indexer.Search(topic, searchArea, 100); bool exactTopicLocated = false; foreach (PageInfo pi in hits) { if (pi.Name.Trim().Equals(topic, StringComparison.InvariantCultureIgnoreCase) && !IsCircularRedirect(pi)) { response = pi.GetFormattedContent(); redirect = pi.RedirectToTopic; exactTopicLocated = true; break; } } if (hits.Count > 0 && !exactTopicLocated) { foreach (PageInfo pi in hits) { if (String.IsNullOrEmpty(pi.RedirectToTopic)) { response = pi.GetFormattedContent(); redirect = pi.RedirectToTopic; exactTopicLocated = true; break; } } if (!exactTopicLocated) { foreach (PageInfo pi in hits) { if (!IsCircularRedirect(pi)) { response = pi.GetFormattedContent(); redirect = pi.RedirectToTopic; break; } } } } } } e.Redirect = !String.IsNullOrEmpty(redirect); e.RedirectTarget = redirect; e.Response = Encoding.UTF8.GetBytes(response); e.MimeType = "text/html"; if (String.IsNullOrEmpty(redirect)) { autoRedirects.Clear(); } else { autoRedirects.Push(redirect); } }