Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. public class Test
  2. {
  3. private static object syncObj = new object();
  4.  
  5. private System.Threading.Timer timer;
  6.  
  7. private Searcher searcher;
  8.  
  9. private RAMDirectory idx = new RAMDirectory();
  10.  
  11. public Test()
  12. {
  13. this.timer = new System.Threading.Timer(this.Timer_Elapsed, null, TimeSpan.Zero, TimeSpan.FromMinutes(3));
  14. }
  15.  
  16.  
  17. private Searcher ESearcher
  18. {
  19. get
  20. {
  21. return this.searcher;
  22. }
  23.  
  24. set
  25. {
  26. lock (syncObj)
  27. {
  28. this.searcher = value;
  29. }
  30. }
  31. }
  32.  
  33. public Document CreateDocument(string title, string content)
  34. {
  35. Document doc = new Document();
  36. doc.Add(new Field("A", title, Field.Store.YES, Field.Index.NO));
  37. doc.Add(new Field("B", content, Field.Store.YES, Field.Index.ANALYZED));
  38. return doc;
  39. }
  40.  
  41. public List<Document> Search(Searcher searcher, string queryString)
  42. {
  43. List<Document> documents = new List<Document>();
  44. QueryParser parser = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "B", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30));
  45. Query query = parser.Parse(queryString);
  46. int hitsPerPage = 5;
  47. TopScoreDocCollector collector = TopScoreDocCollector.Create(2 * hitsPerPage, true);
  48. this.ESearcher.Search(query, collector);
  49.  
  50. ScoreDoc[] hits = collector.TopDocs().ScoreDocs;
  51.  
  52. int hitCount = collector.TotalHits > 10 ? 10 : collector.TotalHits;
  53. for (int i = 0; i < hitCount; i++)
  54. {
  55. ScoreDoc scoreDoc = hits[i];
  56. int docId = scoreDoc.Doc;
  57. float docScore = scoreDoc.Score;
  58. Document doc = searcher.Doc(docId);
  59. documents.Add(doc);
  60. }
  61.  
  62. return documents;
  63. }
  64.  
  65. private void Timer_Elapsed(object sender)
  66. {
  67. this.Log("Started Updating the Search Indexing");
  68. // Get New data to Index
  69. using (IndexWriter writer = new IndexWriter(this.idx, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), true, IndexWriter.MaxFieldLength.LIMITED))
  70. {
  71. foreach (var e in es)
  72. {
  73. writer.AddDocument(this.CreateDocument(e.Value.ToString(), e.Key));
  74. }
  75.  
  76. writer.Optimize();
  77. }
  78.  
  79. this.ESearcher = new IndexSearcher(this.idx);
  80. this.Log("Completed Updating the Search Indexing");
  81. }
  82.  
  83. public Result ServeRequest()
  84. {
  85. var documents = this.Search(this.EntitySearcher, searchTerm);
  86. //somelogic
  87. return result;
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement