public List<Advert> SearchAds(string query) { if (_searcher == null) return null; ICollection fields = _searcher.Reader.GetFieldNames(IndexReader.FieldOption.ALL); List<string> fldList = new List<string>(); foreach (DictionaryEntry f in fields) { fldList.Add(f.Key.ToString()); } List<Advert> adverts = new List<Advert>(); MultiFieldQueryParser parser = new MultiFieldQueryParser(fldList.ToArray(), _analyzer); Query q = parser.Parse(query); Hits hits = _searcher.Search(q); PropertyDescriptors desc = new PropertyDescriptors(); desc.LoadData(System.Windows.Forms.Application.StartupPath + "\\PropertyDescriptors.xml"); for (int i = 0; i < hits.Length(); i++) { Advert ad = new Advert(); Document doc = hits.Doc(i); foreach (Field f in doc.Fields()) { string temp = desc.GetDisplayableFormat(f.Name(), f.StringValue()); ad[f.Name()] = temp; } adverts.Add(ad); } return adverts; }
private void ExtractAds(string contents) { if (contents.Trim() == string.Empty) return; const string STARTAD = "<<STARTAD>>"; const string ENDAD = "<<ENDAD>>"; int startindex = 0; int endindex = 0; while (startindex != -1) { startindex = contents.IndexOf("<<STARTAD>>", endindex + ENDAD.Length); endindex = contents.IndexOf("<<ENDAD>>", startindex + STARTAD.Length); if (startindex <= endindex) { string ad = contents.Substring(startindex + STARTAD.Length, endindex - startindex); string[] lines = ad.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); Advert advert = new Advert(); foreach (string s in lines) { string st = s.Trim(); if (st == string.Empty) continue; string[] tokens = st.Split(":".ToCharArray(), 2); //Debug.Assert(tokens.Length < 2); if (tokens.Length < 2) { continue; } if (tokens[1].Trim() == string.Empty) { continue; } advert[tokens[0]] = tokens[1]; } _adverts.Add(advert); } } }