1
|
|
package net.sf.flock.lucene;
|
2
|
|
|
3
|
|
import java.io.File;
|
4
|
|
import java.io.IOException;
|
5
|
|
|
6
|
|
import org.apache.lucene.analysis.SimpleAnalyzer;
|
7
|
|
import org.apache.lucene.document.Document;
|
8
|
|
import org.apache.lucene.index.IndexReader;
|
9
|
|
import org.apache.lucene.index.IndexWriter;
|
10
|
|
import org.apache.lucene.index.Term;
|
11
|
|
import org.apache.lucene.queryParser.ParseException;
|
12
|
|
import org.apache.lucene.queryParser.QueryParser;
|
13
|
|
import org.apache.lucene.search.Hits;
|
14
|
|
import org.apache.lucene.search.IndexSearcher;
|
15
|
|
import org.apache.lucene.search.Query;
|
16
|
|
import org.apache.lucene.search.Searcher;
|
17
|
|
|
18
|
|
public class IndexService {
|
19
|
|
|
20
|
|
private final String indexPath;
|
21
|
|
|
22
|
|
private IndexWriter writer = null;
|
23
|
|
private Searcher searcher;
|
24
|
|
|
25
|
|
private boolean dirty = false;
|
26
|
|
|
27
|
0
|
public IndexService(String indexPath) throws IOException {
|
28
|
0
|
this( indexPath, !new File(indexPath).exists());
|
29
|
|
}
|
30
|
|
|
31
|
1
|
public IndexService(String indexPath, boolean create) throws IOException {
|
32
|
1
|
this.indexPath = indexPath;
|
33
|
1
|
if (create) {
|
34
|
1
|
IndexWriter cw = new IndexWriter(indexPath, null, true);
|
35
|
1
|
cw.close();
|
36
|
|
}
|
37
|
1
|
this.searcher = new IndexSearcher(indexPath);
|
38
|
|
}
|
39
|
|
|
40
|
102
|
private IndexWriter getWriter() throws IOException {
|
41
|
102
|
if (this.writer==null) {
|
42
|
3
|
this.writer = new IndexWriter(indexPath, new SimpleAnalyzer(), false);
|
43
|
3
|
this.writer.mergeFactor = 100;
|
44
|
|
//this.writer.infoStream = System.out;
|
45
|
|
}
|
46
|
102
|
return this.writer;
|
47
|
|
}
|
48
|
|
|
49
|
102
|
public synchronized void add(Document document) throws IOException {
|
50
|
102
|
try {
|
51
|
102
|
this.getWriter().addDocument(document);
|
52
|
|
} finally {
|
53
|
102
|
this.dirty=true;
|
54
|
|
}
|
55
|
|
}
|
56
|
|
|
57
|
102
|
public int delete(String field, String value) throws IOException {
|
58
|
102
|
return this.delete(new Term(field, value));
|
59
|
|
}
|
60
|
|
|
61
|
102
|
public synchronized int delete(Term term) throws IOException {
|
62
|
102
|
IndexReader reader = IndexReader.open(this.indexPath);
|
63
|
102
|
try {
|
64
|
102
|
return reader.delete(term);
|
65
|
|
} finally {
|
66
|
102
|
this.dirty=true;
|
67
|
102
|
reader.close();
|
68
|
|
// !!! does the writer need to be invalidated at this point?
|
69
|
|
}
|
70
|
|
}
|
71
|
|
|
72
|
1
|
public synchronized void update(String matchField, Document document) throws IOException {
|
73
|
1
|
this.delete(matchField, document.get(matchField));
|
74
|
1
|
this.add(document);
|
75
|
|
}
|
76
|
|
|
77
|
106
|
public Hits search(String field, String queryString) throws ParseException, IOException {
|
78
|
106
|
Query query = QueryParser.parse(queryString, field, new SimpleAnalyzer());
|
79
|
106
|
return this.search(query);
|
80
|
|
}
|
81
|
|
|
82
|
106
|
public Hits search(Query query) throws IOException {
|
83
|
106
|
synchronized (this) {
|
84
|
106
|
if (this.writer!=null) {
|
85
|
|
// commit pending writes
|
86
|
3
|
writer.optimize();
|
87
|
3
|
writer.close();
|
88
|
3
|
writer=null;
|
89
|
|
}
|
90
|
106
|
if (this.dirty) {
|
91
|
|
// recreate reader
|
92
|
|
// !!! this interrupts ongoing searches
|
93
|
5
|
this.searcher.close();
|
94
|
5
|
this.searcher = new IndexSearcher(indexPath);
|
95
|
5
|
dirty = false;
|
96
|
|
}
|
97
|
|
}
|
98
|
106
|
Hits hits = this.searcher.search(query);
|
99
|
106
|
return hits;
|
100
|
|
}
|
101
|
|
|
102
|
0
|
public synchronized void close() {
|
103
|
0
|
try {
|
104
|
0
|
if (this.writer!=null) {
|
105
|
0
|
this.writer.close();
|
106
|
|
}
|
107
|
|
} catch (IOException e) {
|
108
|
0
|
e.printStackTrace();
|
109
|
|
} finally {
|
110
|
0
|
this.writer=null;
|
111
|
|
}
|
112
|
0
|
try {
|
113
|
0
|
this.searcher.close();
|
114
|
|
} catch (IOException e) {
|
115
|
0
|
e.printStackTrace();
|
116
|
|
} finally {
|
117
|
0
|
this.searcher=null;
|
118
|
|
}
|
119
|
|
}
|
120
|
|
|
121
|
|
}
|
122
|
|
|