Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using Lucene.Net.Store;
  6. using Lucene.Net.Index;
  7. using Lucene.Net.Analysis;
  8. using Lucene.Net.Analysis.Standard;
  9. using Lucene.Net.Documents;
  10. using Lucene.Net.Search;
  11. using Lucene.Net.QueryParsers;
  12. using Lucene.Net.Analysis.AR;
  13. using Lucene.Net.Search.Highlight;
  14.  
  15. /// <summary>
  16. /// Summary description for Searcher
  17. /// </summary>
  18. public class Searcher
  19. {
  20. public Searcher()
  21. {
  22. //
  23. // TODO: Add constructor logic here
  24. //
  25. }
  26.  
  27. private const int HITS_LIMIT = 25;
  28. private const int MAX_FRAGMENTS_NUMBER = 3;
  29.  
  30. private Directory _Directory;
  31. public Directory Directory
  32. {
  33. get
  34. {
  35. string path = HttpContext.Current.Server.MapPath("~/App_Data/Index");
  36. if (_Directory == null)
  37. _Directory = FSDirectory.Open(path);
  38.  
  39. return _Directory;
  40. }
  41. }
  42.  
  43. private Analyzer _Analyzer;
  44. public Analyzer Analyzer
  45. {
  46. get
  47. {
  48. if (_Analyzer == null)
  49. _Analyzer = new ArabicAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
  50.  
  51. return _Analyzer;
  52. }
  53. }
  54.  
  55. #region Mapping
  56.  
  57. private Document MapDataToLuceneDocument(Data data)
  58. {
  59. Document document = new Document();
  60. document.Add(new Field("ID", data.DataID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
  61. document.Add(new Field("Path", data.Path, Field.Store.YES, Field.Index.NOT_ANALYZED));
  62. document.Add(new Field("Title", data.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
  63. document.Add(new Field("Content", data.Content, Field.Store.NO, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
  64.  
  65. return document;
  66. }
  67.  
  68. private Data MapLuceneDocumentToData(Document document)
  69. {
  70. Data result = new Data()
  71. {
  72. DataID = int.Parse(document.Get("ID")),
  73. Path = document.Get("Path"),
  74. Title = document.Get("Title"),
  75. Content = document.Get("Content"),
  76. };
  77.  
  78. return result;
  79. }
  80.  
  81. #endregion
  82.  
  83. #region Indexing
  84.  
  85. private void _Index(Data data, IndexWriter writer)
  86. {
  87. Query query = new TermQuery(new Term("ID", data.DataID.ToString()));
  88. writer.DeleteDocuments(query);
  89.  
  90. writer.AddDocument(this.MapDataToLuceneDocument(data));
  91. }
  92.  
  93. public void Index(IEnumerable<Data> data)
  94. {
  95. IndexWriter writer = null;
  96. try
  97. {
  98. writer = new IndexWriter(this.Directory, this.Analyzer,false, IndexWriter.MaxFieldLength.UNLIMITED);
  99. }
  100. catch
  101. {
  102. writer = new IndexWriter(this.Directory, this.Analyzer,true, IndexWriter.MaxFieldLength.UNLIMITED);
  103. }
  104. foreach (var item in data)
  105. {
  106. this._Index(item, writer);
  107. }
  108. writer.Dispose();
  109. }
  110.  
  111. public void Index(Data data)
  112. {
  113. this.Index(new List<Data>() { data });
  114. }
  115.  
  116. #endregion
  117.  
  118. #region Searching
  119.  
  120. private List<Data> _Search(string searchPhrase, string searchField = "")
  121. {
  122. List<Data> searchResults = new List<Data>();
  123.  
  124. if (string.IsNullOrWhiteSpace(searchPhrase))
  125. return searchResults;
  126.  
  127. QueryParser parser;
  128. if (string.IsNullOrWhiteSpace(searchField))
  129. {
  130. parser = new MultiFieldQueryParser(Lucene.Net.Util.Version.LUCENE_30, new String[] { "Title", "Content" }, this.Analyzer);
  131. }
  132. else
  133. {
  134. parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, searchField, this.Analyzer);
  135. }
  136.  
  137. Query query;
  138. try
  139. {
  140. query = parser.Parse(searchPhrase.Trim());
  141. }
  142. catch (Exception exception)
  143. {
  144. query = parser.Parse(QueryParser.Escape( searchPhrase.Trim()));
  145. }
  146.  
  147. IndexSearcher searcher = new IndexSearcher(this.Directory);
  148. //QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "text", this.Analyzer);
  149. //Query query_ = parser.Parse(query);
  150.  
  151. TopDocs hits = searcher.Search(query, null, Searcher.HITS_LIMIT, Sort.RELEVANCE);
  152. foreach (var doc in hits.ScoreDocs)
  153. {
  154. Document document = searcher.Doc(doc.Doc);
  155. searchResults.Add(this.MapLuceneDocumentToData(document));
  156. }
  157.  
  158. return searchResults;
  159. }
  160.  
  161. public List<Data> Search(string searchPhrase, string searchField = "")
  162. {
  163. return this._Search(searchPhrase, searchField);
  164. }
  165.  
  166. #endregion
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement