DataTable search(string textSearch) { var table = Sample.Clone(); var Index = createIndex(Sample); using (var reader = IndexReader.Open(Index, true)) using (var searcher = new IndexSearcher(reader)) { using (Analyzer analyzer = new Lucene.Net.Analysis.Snowball.SnowballAnalyzer(Lucene.Net.Util.Version.LUCENE_30, "English")) { var queryParser = new QueryParser(Version.LUCENE_30, "FullText", analyzer); queryParser.AllowLeadingWildcard = true; var query = queryParser.Parse(textSearch); var collector = TopScoreDocCollector.Create(1000, true); searcher.Search(query, collector); var matches = collector.TopDocs().ScoreDocs; foreach (var item in matches) { var id = item.Doc; var doc = searcher.Doc(id); var row = table.NewRow(); row["relevance"] = doc.GetField("relevance").StringValue; row["prefix"] = doc.GetField("prefix").StringValue; row["exact"] = doc.GetField("exact").StringValue; row["suffix"] = doc.GetField("suffix").StringValue; row["Description_Id"] = doc.GetField("Description_Id").StringValue; table.Rows.Add(row); } } } return(table); }
Directory createIndex(DataTable table) { var directory = new RAMDirectory(); using (Analyzer analyzer = new Lucene.Net.Analysis.Snowball.SnowballAnalyzer(Lucene.Net.Util.Version.LUCENE_30, "English")) using (var writer = new IndexWriter(directory, analyzer, new IndexWriter.MaxFieldLength(1000))) { // the writer and analyzer will popuplate the directory with documents foreach (DataRow row in table.Rows) { var document = new Document(); document.Add(new Field("relevance", row["relevance"].ToString(), Field.Store.YES, Field.Index.ANALYZED)); document.Add(new Field("prefix", row["prefix"].ToString(), Field.Store.YES, Field.Index.ANALYZED)); document.Add(new Field("exact", row["exact"].ToString(), Field.Store.YES, Field.Index.ANALYZED)); document.Add(new Field("suffix", row["suffix"].ToString(), Field.Store.YES, Field.Index.ANALYZED)); document.Add(new Field("Description_Id", row["Description_Id"].ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); //var date = row["BirthDate"].ToString(); //date = string.Format("{0} {1}", date, date.Replace("/", " / ")); document.Add(new Field("FullText", string.Format("{0} {1} {2} {3} {4}", row["relevance"], row["prefix"], row["exact"], row["suffix"], row["Description_Id"]) , Field.Store.YES, Field.Index.ANALYZED)); writer.AddDocument(document); } writer.Optimize(); writer.Flush(true, true, true); } return(directory); }
// TODO: Allow language also to be passed public SnowballAnalyzer() { Analyzer = new Lucene.Net.Analysis.Snowball.SnowballAnalyzer(SearchSettings.Instance.LuceneVersion, SearchSettings.Instance.Language, StopWords.DefaultEnglish); }
public SnowballAnalyzer(SearchSettings settings) { this._settings = settings; Analyzer = new Lucene.Net.Analysis.Snowball.SnowballAnalyzer(this._settings.LuceneVersion, this._settings.Language, StopWords.DefaultEnglish); }