lucene.net 2.9 学习笔记

最近在写一些搜索的东西,用LUCENE.NET2.9,学得不多,但是有进步。好吧,我表示,未来我可以写的更好!

  1 using System;
  2 using System.IO;
  3 using Lucene.Net.QueryParsers;
  4 using Lucene.Net.Analysis;
  5 using Lucene.Net.Analysis.Standard;
  6 using Lucene.Net.Documents;
  7 using Lucene.Net.Index;
  8 using Lucene.Net.Search;
  9 using Lucene.Net.Store;
 10 using System.Xml;
 11 namespace DianJiangTai.searchEngine
 12 {
 13     public class SearchTools
 14     {
 15         //public Analyzer analyzer;
 16        // public IndexReader reader;
 17       //  public TopScoreDocCollector collector;
 18         public IndexSearcher isearcher;
 19         public static int searchItemCount=1000;//搜索时提取结果中前100条记录
 20         public static int startIndex = 0;//每页开始的位置
 21         public static int endIndex = 0;//每页结束的位置
 22         public static int pageSize =6;//页容量
 23         public static string[] resQuee = new string[50];
 24         public static int front = 0, rear = 0,Queelength=0;
 25         private int resCount;
 26 
 27 /*        private string readIndexPath()
 28         {
 29            //   string path=System.Configuration.ConfigurationSettings.AppSettings.Get("IndexPath");
 30          //   return path;
 31              XmlDocument xmlDoc = new XmlDocument();
 32                 xmlDoc.Load("http://www.cnblogs.com/../searchIndex.xml");
 33                 XmlNode root = xmlDoc.SelectSingleNode("indexPath");
 34                 XmlNodeList subNode = root.ChildNodes;
 35                 XmlElement xe2 = (XmlElement)subNode[0];
 36                 XmlNode sub = xe2.FirstChild;
 37                 return sub.InnerText.Trim();
 38         }
 39 */
 40          
 41         public SearchTools()
 42         {
 43             //string path = readIndexPath();
 44         }     
 45         private bool push(string res)
 46         {
 47             if ((rear + 1) % 50 != front)
 48             {
 49                 resQuee[rear] = res;
 50                 rear=(rear+1)%50;
 51                 Queelength++;
 52                 return true;
 53             }
 54             else
 55                 return false;
 56         }
 57         private string pop()
 58         {
 59             if (rear != front)
 60             {
 61                 string temp= resQuee[front];
 62                 front = (front + 1) % 50;
 63                 Queelength--;
 64                 return temp;
 65             }
 66             else
 67                 return null;
 68         }
 69        /*
 70         private string pop(int id)
 71         {
 72             if (rear != front)
 73             {
 74                 string temp = resQuee[id];
 75                 return temp;
 76             }
 77             else
 78                 return null;
 79         }
 80         private bool ifQueeNULL()
 81         {
 82             if (Queelength <= 0)
 83                 return true;
 84             else return false;
 85         }
 86         public static bool ifNeedAnotherRead=false;
 87        /*
 88         private void searchAll(string[] querystr)
 89         {
 90             bool ifNULL = true;
 91             int length = querystr.Length;
 92             if (length <= 0) return;
 93             isearcher = new IndexSearcher(reader);
 94             ScoreDoc[] hits;
 95             int j;
 96 
 97       if(!ifNeedAnotherRead)
 98       {
 99           BooleanQuery query = new BooleanQuery();
100           for (j = length; j >= 2; j--)
101             {
102                 for (int i = 0; i < j; i++)
103                 { 
104                     TermQuery tempTerm = new TermQuery(new Term("indexItem", querystr[i]));
105                     query.Add(tempTerm, BooleanClause.Occur.SHOULD);
106                 }
107                 collector = TopScoreDocCollector.create(isearcher.MaxDoc(), false);
108                 isearcher.Search(query, collector);
109                hits = collector.TopDocs().scoreDocs;
110                 if (hits.Length <= 0)
111                     continue;
112                 else
113                 {
114                    if (hits.Length <= 3*endIndex)
115                        {
116                            endIndex = hits.Length;
117                            ifNeedAnotherRead = true;
118                            WriteIntoCache(isearcher,hits,startIndex,endIndex);
119                            startIndex = 0;
120                            endIndex = startIndex + pageSize;
121                        }
122                        else
123                         {                 
124                              ifNeedAnotherRead = false;
125                              WriteIntoCache(isearcher,hits,startIndex,endIndex * 3);
126                              startIndex = endIndex * 3;
127                              endIndex = startIndex + pageSize;          
128                           }
129                   ifNULL = false;
130                 }
131             }
132       }
133       if(ifNeedAnotherRead || ifNULL)
134       {
135           QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "indexItem", analyzer);//indexItem
136             for (j = 0; j < length; j++)
137             {
138                 if (querystr[j].Trim().Equals("") || querystr[j] == null)
139                     continue;
140                 Query querySingle = parser.Parse(querystr[j]);
141                 collector = TopScoreDocCollector.create(isearcher.MaxDoc(), false);
142                 isearcher.Search(querySingle, collector);
143                 hits = collector.TopDocs().scoreDocs;
144                 if (hits.Length <= 0)
145                 {
146                     continue;
147                 }
148                 else
149                 {
150                      int leftSize=50-Queelength;
151                       if (hits.Length >leftSize)
152                        {
153                           WriteIntoCache(isearcher,hits,startIndex,startIndex+leftSize);                    
154                           startIndex =leftSize;//要记下结束的位置
155                           endIndex = startIndex + pageSize;         
156                        }
157                        else
158                       {   
159                           WriteIntoCache(isearcher,hits,startIndex,hits.Length);   
160                           startIndex = 0;//要记下结束的位置
161                           endIndex = startIndex + pageSize;
162                       }
163                     ifNULL = false;
164                 }
165             }
166       }
167             isearcher.Close();
168             if (ifNULL)
169             {
170                 Console.WriteLine("搜索失败!");
171             }
172         }
173         */
174         public string[] StartSearch(string[] queryString, int searchType, int currentPage)
175         {
176             if (queryString.Length <= 0) return null;
177 
178     //        string path = System.Configuration.ConfigurationManager.AppSettings.Get("IndexPath");
179    //         if (path == null || path.Equals("")||!File.Exists(path))
180     //            return null;
181             string path = "G:/project/index";
182             front = 0; rear = 0; Queelength = 0; resCount = 0;
183             startIndex = (currentPage - 1) * pageSize;
184             endIndex = startIndex + pageSize * 2;
185             Analyzer analyzer=null;
186             IndexReader reader=null;
187             try
188             {
189 
190                 reader = IndexReader.Open(FSDirectory.Open(new System.IO.DirectoryInfo(path)), true);
191                 analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
192                 isearcher = new IndexSearcher(reader);
193             QueryParser parser = null;
194             switch (searchType)
195             {
196                 case 1: parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "indexItem", analyzer);//search anthing containing any part of the key word
197                         searchByall(queryString, "indexItem");
198                         searchByItem(queryString, parser);
199                         break;
200                 case 2: parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "DrugName", analyzer); //search by product
201                         searchByItem(queryString, parser);
202                         break;
203                 case 3: parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_29, "OrgName", analyzer); //search by company
204                         searchByItem(queryString, parser);
205                         break;
206             }
207             }
208             catch (Exception e)
209             {}
210 
211             isearcher.Close();
212             analyzer.Close();
213             reader.Clone();
214 
215             string res = null;
216             while (Queelength > 0)
217             {
218                 res = res + pop() + "~";
219             }
220             res = res + resCount.ToString()+"~"+"end";
221             if (res == null)
222                 return null;
223             else
224                 return res.Split('~');
225         }
226 
227         private void searchByall(string[] querystr,string fieldName)
228         {    
229             int length = querystr.Length;
230           //  bool ifNULL = true;
231             ScoreDoc[] hits;
232             int j;
233             BooleanQuery query;
234             TopScoreDocCollector collector;
235                 for (j = length-1; j >= 2; j--)
236                 {
237                     query = new BooleanQuery();
238                     for (int i = 0; i < j; i++)
239                    {
240                        TermQuery tempTerm = new TermQuery(new Term("indexItem", querystr[i]));
241                        BooleanClause clause=new BooleanClause(tempTerm ,BooleanClause.Occur.SHOULD);
242                        query.Add(clause);
243                        //query.Add(tempTerm, BooleanClause.Occur.SHOULD);
244                    }
245                     collector = TopScoreDocCollector.create(isearcher.MaxDoc(), false);
246                     isearcher.Search(query ,collector);
247                     hits = collector.TopDocs().scoreDocs;
248                     resCount =resCount+ hits.Length;
249                     if (hits.Length < startIndex)
250                     {
251                         return;
252                     }
253                     else
254                    if (hits.Length <= 0)
255                         continue;
256                     else
257                     {
258                         if (hits.Length < endIndex)
259                         {
260                             WriteIntoCache(isearcher, hits, startIndex, hits.Length);
261                         }
262                         else
263                         {
264                             WriteIntoCache(isearcher, hits, startIndex, endIndex);
265                         }
266                       //  ifNULL = false;
267                     }
268                 }
269             isearcher.Close();
270           //  if (ifNULL)
271           //  {
272            //     Console.WriteLine("搜索失败!");
273           //  }
274         
275         }
276         private void searchByItem(string[] Productstring, QueryParser parser)
277         {
278             int length = Productstring.Length;
279          //   bool ifNULL = true;
280             ScoreDoc[] hits;
281             TopScoreDocCollector collector;
282             int j;
283             for (j = 0; j < length; j++)
284             {
285                 if (Productstring[j].Trim().Equals("") || Productstring[j] == null)
286                     continue;
287                 Query querySingle = parser.Parse(Productstring[j]);
288                 collector = TopScoreDocCollector.create(isearcher.MaxDoc(), false);
289                 isearcher.Search(querySingle, collector);
290                 hits = collector.TopDocs().scoreDocs;
291                 resCount = resCount + hits.Length;
292                 if (hits.Length < startIndex)
293                 {
294                     return;
295                 }
296                 else
297                 if (hits.Length <= 0)
298                 {
299                     continue;
300                 }
301                 else
302                 {
303                     if (hits.Length > endIndex)
304                        {
305                           WriteIntoCache(isearcher, hits, startIndex, endIndex);
306                        }
307                        else
308                       {
309                           WriteIntoCache(isearcher, hits, startIndex, hits.Length);
310                       }
311                    // ifNULL = false;
312                 }
313             }
314             isearcher.Close();
315         //    if (ifNULL)
316         //    {
317          //       Console.WriteLine("搜索失败!");
318          //   }  
319         }
320 
321         private void WriteIntoCache(IndexSearcher isearcher, ScoreDoc[] hits, int start, int end)
322         {
323             string temp = "";
324                 for (int i = start; i < end; i++)
325                 {
326                     try 
327                     {
328                         Document hitDoc = isearcher.Doc(hits[i].doc);
329                         temp = hitDoc.Get("ObjectID") + "^" + hitDoc.Get("Title") + "^" + hitDoc.Get("Summary") + "^" + hitDoc.Get("JingBiaoStartTime") + "^" + hitDoc.Get("CreateTime") + "^" + hitDoc.Get("OrgName") + "^" + hitDoc.Get("Status");
330                         if (!push(temp))
331                             break;
332                     }
333                     catch(Exception e)
334                     {
335                         return;
336                     }       
337                 }
338         }
339         /*
340         public static int pageCount;
341         public string[] getSearchResult(string[] query, int page,int ifNextPageOrNewSearch)
342         {
343 
344             if (ifQueeNULL() || ifNextPageOrNewSearch == 1)
345            {
346                 front = 0;
347                 rear = 0; 
348                 Queelength = 0;
349                 searchAll(query);
350                 pageCount = Queelength / pageSize;
351                 if (Queelength % pageSize != 0)
352                     pageCount++;
353             }
354             string res = null;
355             if (page <= pageCount)
356             {
357                // if (page > 5)
358                   //  search(query);
359                 int pos = (front + (page - 1) * pageSize) % 50;
360                 for (int i = pos; i < pos + pageSize; i++)
361                 {
362                     if (i >= Queelength) break;
363                     res = res + pop(i) + "~";
364                 }
365                 return res.Split('~');
366             }
367             else
368                return null;
369         }*/
370     }
371 }

 

 

posted @ 2013-04-27 20:29  shenghaishiweini  阅读(303)  评论(0编辑  收藏  举报