Lucene的基本用法
创建lucene索引
View Code
1 //1. path 2 string indexPath=@"/ui/index/"; 3 4 //2. StandarAnalyzer 5 Lucene.Net.Analysis.Standard.StandarAnalyzer sa=new StandarAnalyzer(); 6 7 //3. IndexWriter 8 IndexWriter iw=new IndexWriter(mapPath(indexPath),sa,true); 9 10 //4. Document 11 Document doc=new Document(); 12 13 //5. Field 14 Field f=newFiled("id",val,Field.Store.Yes,field.index.UN_TOKENIZED); 15 16 doc.Add(f); 17 iw.addDocument(doc);
读取lucene索引数据
View Code
1. //Path,IndexReader,field_key IndexReader reader=IndexReader.Open(MapPath(indexPath); string field="id"; // Searcher 2.Search se=new IndexSearcher(reader); //Analyzer 3.Analyzer an=new StandarAnalyzer(); //QueryParser 4.QueryParser p=new QueryParser(field,an); p2=new QueryParser(field2,an); //BooleanQuery 5.BooleanQuery b=new BooleanQuery(); //Query Term //Term t=new Term("id",filed); q=new WildcardQuery(t); Query q=p.parse(filed); q2=p2.parse(filed2); b.add(q,booleanClause.Occur.Must/Should); //Hit 6. Hits myhit=se.search(b); for循环{ Docment doc=myHit.Doc(i); doc.Get("id").tostring(); }