public SearchInfo ProcessFile(string filePath) { FileInfo finfo = null; SearchInfo sinfo = null; try { finfo = new FileInfo(filePath); } catch { return null; } if(finfo.Length > 1 * 1024 * 1024) { return null; } string strContent = null; try { StreamReader reader = new StreamReader(filePath); strContent = reader.ReadToEnd(); reader.Close(); strContent = Regex.Replace(strContent, "<[^>]+>", string.Empty); } catch { return null; } string strTitle = finfo.Name; string strLauncher = "file:///" + filePath; sinfo = new SearchInfo(strTitle, strContent, strLauncher); return sinfo; }
/// <summary> /// Adds a document to the index /// </summary> /// <param name="info">Document to add</param> /// <returns>true if successful, false otherwise</returns> public bool AddDocument(SearchInfo info) { bool retVal = false; if (m_bSucess && (m_indexMode == IndexMode.CREATE || m_indexMode == IndexMode.APPEND)) { // README: This is so that < and > don't interfere with existing // tags in the browser string strText = info.Text.Replace("<", "<").Replace(">", ">"); Document doc = new Document(); doc.Add(Field.UnIndexed("title", info.Title)); doc.Add(Field.Text("content", strText)); doc.Add(Field.UnIndexed("launcher", info.Launcher)); m_indexWriter.AddDocument(doc); retVal = true; } return retVal; }
/// <summary> /// Searches an index opened in search mode for the search term(s) /// </summary> /// <param name="searchTerm">Term to search for. Multiple terms are separated by + </param> /// <returns>An array of search results</returns> public SearchInfo[] Search(string searchTerm) { SearchInfo[] retVal = null; if (m_bSucess && m_indexMode == IndexMode.SEARCH && searchTerm != null) { // TODO: Needs more work. haha searchTerm = searchTerm.Replace("+", " + "); Query query = QueryParser.Parse(searchTerm, "content", m_analyzer); Hits hits = m_indexSearcher.Search(query); int len = hits.Length(); if (len > 0) { retVal = new SearchInfo[len]; for (int i = 0; i < len; i++) { retVal[i] = new SearchInfo(hits.Doc(i).Get("title"), hits.Doc(i).Get("content"), hits.Doc(i).Get("launcher")); } } } return retVal; }
/// <summary> /// Decodes the file and returns a SearchInfo object for indexing /// </summary> /// <param name="filePath"></param> /// <returns></returns> public SearchInfo ProcessFile(string filePath) { SearchInfo indexvars = null; if (filePath.StartsWith(m_profilesPath, StringComparison.OrdinalIgnoreCase)) { if (filePath.Contains("Profiles") && filePath.Contains("Messages")) { Decoder d = new Decoder(filePath); string strText = d.Decode(true); string strTitle = string.Format(Resources.IndexTitle, d.LocalID, d.RemoteID); indexvars = new SearchInfo(strTitle, strText, "display.html?convo=" + filePath); } } return indexvars; }