Beispiel #1
0
        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;
        }
Beispiel #2
0
        public void IndexFile(string filePath)
        {
            PropertyDescriptors descriptors = new PropertyDescriptors();
            descriptors.LoadData(System.Windows.Forms.Application.StartupPath + "\\PropertyDescriptors.xml");
            Analyzer a = new Lucene.Net.Analysis.Standard.StandardAnalyzer();
            bool create = !(System.IO.Directory.Exists(_idxDir) && IndexReader.IndexExists(_idxDir));
            IndexWriter iw = new IndexWriter(_idxDir, a, create);
            iw.SetUseCompoundFile(true);

            AdDataStream adStream = new AdDataStream(filePath);
            adStream.LoadData();
            foreach (Advert ad in adStream.FetchAd())
            {
                Document doc = new Document();
                foreach (string s in ad.GetDictionary().Keys)
                {
                    string temp = descriptors.GetIndexableFormat(descriptors[s], ad[s]);
                    doc.Add(Field.Text(s, temp));

                }
                iw.AddDocument(doc);
                if (_updateCallback != null)
                {
                    _updateCallback("Added Document: " + ad["Title"]);

                }
            }
            iw.Optimize();
            iw.Close();
        }