Lucene全文检索-从零开始(2)
1、lucene索引的创建
1 /// <Contents> 2 /// 将 list数据传入创建索引 3 /// </Contents> 4 /// <param name="datalist"></param> 5 /// <returns></returns> 6 public bool CreateIndex(List<LuceneIndexArticle> datalist) 7 { 8 FSDirectory directory = FSDirectory.Open(new DirectoryInfo(IndexDic), new NativeFSLockFactory()); 9 IndexWriter writer = null; 10 bool isUpdate = IndexReader.IndexExists(directory); 11 if (isUpdate) 12 { 13 if (IndexWriter.IsLocked(directory)) 14 { 15 IndexWriter.Unlock(directory); 16 } 17 } 18 writer = new IndexWriter(IndexDic, PanGuAnalyzer, !isUpdate, IndexWriter.MaxFieldLength.LIMITED);//false表示追加(true表示删除之前的重新写入) 19 20 foreach (LuceneIndexArticle data in datalist) 21 { 22 CreateIndex(writer, data); 23 } 24 writer.Optimize(); 25 writer.Close(); 26 return true; 27 } 28 public bool CreateIndex(IndexWriter writer, LuceneIndexArticle data) 29 { 30 try 31 { 32 if (data == null) return false; 33 Document doc = new Document(); 34 Type type = data.GetType(); 35 PropertyInfo[] Propertys = type.GetProperties(); 36 for (int i = 0; i < Propertys.Length; i++) 37 { 38 PropertyInfo pi = Propertys[i]; 39 string name = pi.Name; 40 object objval = pi.GetValue(data, null); 41 string value = objval == null ? "" : objval.ToString(); //值 42 if (name == "ID")//id在写入索引时必是不分词,否则是模糊搜索和删除,会出现混乱 43 { 44 doc.Add(new Field(name, value, Field.Store.YES, Field.Index.NOT_ANALYZED));//id不分词 45 } 46 else 47 { 48 doc.Add(new Field(name, value, Field.Store.YES, Field.Index.ANALYZED)); 49 } 50 } 51 writer.AddDocument(doc); 52 } 53 catch (System.IO.FileNotFoundException fnfe) 54 { 55 throw fnfe; 56 } 57 return true; 58 }
索引存放目录及盘古配置文件存放
1 /// <Contents> 2 /// 索引存放目录 3 /// </Contents> 4 /// <Contents> 5 /// 盘古分词的配置文件 6 /// </Contents> 7 protected string PanGuXmlPath 8 { 9 get 10 { 11 return @"F:\DCIM\PanGu.xml"; 12 } 13 }
1 protected string IndexDic 2 { 3 get 4 { 5 return @"F:\DCIM"; 6 } 7 }
点击创建索引
索引创建完毕,这些是生成的磁盘文件。
下一节是如何从索引中检索数据