标签:
1 public static void GetNewsList() 2 { 3 4 while (true) 5 { 6 using (var client = RedisManager.ClientManager.GetClient()) 7 { 8 GoWrite(client); 9 } 10 Thread.Sleep(60000); 11 } 12 }
1 /// <summary> 2 /// 开始写 3 /// </summary> 4 /// <param name="writer"></param> 5 /// <param name="client"></param> 6 private static void GoWrite(ServiceStack.Redis.IRedisClient client) 7 { 8 FSDirectory directory = null; 9 IndexWriter writer = null; 10 11 try 12 { 13 string indexPath = "d:/index";//注意和磁盘上文件夹的大小写一致,否则会报错 14 15 directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory()); 16 17 //判断文件夹是否存在 18 bool IsExis = IndexReader.IndexExists(directory); 19 20 if (IsExis) 21 { 22 //如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁 23 if (IndexWriter.IsLocked(directory)) 24 { 25 IndexWriter.Unlock(directory); 26 } 27 } 28 29 writer = new IndexWriter(directory, new PanGuAnalyzer(), !IsExis, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED); 30 31 while (true) 32 { 33 //NID, NTitle, NTxt, NTime, NTID 34 string json = client.DequeueItemFromList("NewsIndex"); 35 36 //判断是否为NULL 37 if (json == null) 38 { 39 //休息1秒退出 40 Thread.Sleep(1000); 41 return; 42 } 43 else 44 { 45 JavaScriptSerializer jss = new JavaScriptSerializer(); 46 Dictionary<string, object> dict = (Dictionary<string, object>)jss.DeserializeObject(json); 47 48 //实例化News 49 News news = new News(); 50 news.NTitle = dict["NTitle"].ToString(); 51 news.NTxt = dict["NTxt"].ToString(); 52 news.NTime = dict["NTime"].ToString(); 53 news.NID = Convert.ToInt64(dict["NID"]); 54 news.NTID = Convert.ToInt32(dict["NTID"]); 55 56 //写入索引库 57 WriteIndex(news, writer); 58 } 59 } 60 } 61 finally 62 { 63 //关闭IndexWriter 64 if (writer != null) 65 { 66 writer.Close(); 67 } 68 69 //关闭FSDirectory 70 if (directory != null) 71 { 72 directory.Close(); 73 } 74 } 75 }
1 /// <summary> 2 /// 写入索引库 3 /// </summary> 4 /// <param name="news"></param> 5 private static void WriteIndex(News news, IndexWriter writer) 6 { 7 8 //写入数据 9 Document document = new Document(); 10 11 //删除相同的数据 12 writer.DeleteDocuments(new Term("NID", news.NID.ToString())); 13 //删除过时文件,需要将判断的字段设置为Field.Index.ANALYZED 14 writer.Optimize(); 15 16 document.Add(new Field("NTitle", news.NTitle, Field.Store.YES, Field.Index.NOT_ANALYZED)); 17 document.Add(new Field("NTxt", news.NTxt, Field.Store.YES, Field.Index.NOT_ANALYZED)); 18 document.Add(new Field("NID", news.NID.ToString(), Field.Store.YES, Field.Index.ANALYZED)); 19 document.Add(new Field("NTID", news.NTID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); 20 document.Add(new Field("NTime", news.NTime.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); 21 document.Add(new Field("ALL", news.NTitle + news.NTime + news.NTxt, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS)); 22 23 24 //执行写入 25 writer.AddDocument(document); 26 Console.WriteLine("索引完毕"); 27 }
标签:
原文地址:http://www.cnblogs.com/Darkon/p/5300962.html