-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLuceneStore.cs
143 lines (128 loc) · 4.33 KB
/
LuceneStore.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Store;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
namespace CdrIndexer
{
class LuceneStore
{
private static readonly Lucene.Net.Util.Version Version = Lucene.Net.Util.Version.LUCENE_30;
private static LuceneStore current;
private IndexWriter writer;
private FSDirectory luceneIndexDirectory;
private Analyzer analyzer;
private IndexSearcher searcher;
public static LuceneStore Current
{
get
{
if (current == null)
{
current = new LuceneStore();
}
return current;
}
}
private LuceneStore()
{
string indexPath = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"LuceneIndex");
this.luceneIndexDirectory = FSDirectory.Open(indexPath);
this.analyzer = new StandardAnalyzer(Version);
this.writer = new IndexWriter(
luceneIndexDirectory,
this.analyzer,
!System.IO.Directory.Exists(indexPath),
IndexWriter.MaxFieldLength.UNLIMITED);
this.searcher = new IndexSearcher(this.luceneIndexDirectory);
}
public Entry Find(string path)
{
var query = new TermQuery(FindTerm(path));
TopDocs docs = this.searcher.Search(query, n: 1);
return ToEntries(docs).FirstOrDefault();
}
public void Insert(Entry entry)
{
this.writer.AddDocument(entry.ToDocument());
this.writer.Flush(triggerMerge: false, flushDocStores: true, flushDeletes: true);
}
public void Update(Entry entry)
{
this.writer.UpdateDocument(FindTerm(entry.Path), entry.ToDocument());
this.writer.Flush(triggerMerge: false, flushDocStores: true, flushDeletes: true);
}
public void ReopenDirectory()
{
this.Close();
LuceneStore.current = new LuceneStore();
}
public int TotalCount()
{
int count;
using (IndexReader reader = IndexReader.Open(this.luceneIndexDirectory, readOnly: true))
{
count = reader.NumDocs();
}
return count;
}
public List<string> AllPaths()
{
var paths = new List<string>();
using (IndexReader reader = IndexReader.Open(this.luceneIndexDirectory, readOnly: true))
{
for (int index = 0; index < reader.MaxDoc; index++)
{
if (reader.IsDeleted(index))
{
continue;
}
Document doc = reader.Document(index);
paths.Add(doc.Get("Path"));
}
}
return paths;
}
public List<Entry> Search(string phrase)
{
var parser = new QueryParser(Version, "All", this.analyzer);
Query query = parser.Parse(phrase);
TopDocs result = this.searcher.Search(query, n: 1000);
return ToEntries(result);
}
private List<Entry> ToEntries(TopDocs docs)
{
var entries = new List<Entry>(docs.ScoreDocs.Count());
int index = 1;
foreach (var doc in docs.ScoreDocs)
{
entries.Add(new Entry(this.searcher.Doc(doc.Doc), doc.Score, index++));
}
return entries;
}
public void Delete(string path)
{
this.writer.DeleteDocuments(FindTerm(path));
this.writer.Flush(triggerMerge: false, flushDocStores: true, flushDeletes: true);
}
private Term FindTerm(string path)
{
return new Term("Path", path.ToLower());
}
public void Close()
{
this.analyzer.Close();
this.searcher.Dispose();
this.writer.Dispose();
this.luceneIndexDirectory.Dispose();
}
}
}