码迷,mamicode.com
首页 > 其他好文 > 详细

利用Redis队列进行循环写入搜索索引

时间:2016-03-21 11:55:15      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 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         }    
写入索引库

 

利用Redis队列进行循环写入搜索索引

标签:

原文地址:http://www.cnblogs.com/Darkon/p/5300962.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!