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 public IndexService(String indexPath) throws IOException {
28 this( indexPath, !new File(indexPath).exists());
29 }
30
31 public IndexService(String indexPath, boolean create) throws IOException {
32 this.indexPath = indexPath;
33 if (create) {
34 IndexWriter cw = new IndexWriter(indexPath, null, true);
35 cw.close();
36 }
37 this.searcher = new IndexSearcher(indexPath);
38 }
39
40 private IndexWriter getWriter() throws IOException {
41 if (this.writer==null) {
42 this.writer = new IndexWriter(indexPath, new SimpleAnalyzer(), false);
43 this.writer.mergeFactor = 100;
44 //this.writer.infoStream = System.out;
45 }
46 return this.writer;
47 }
48
49 public synchronized void add(Document document) throws IOException {
50 try {
51 this.getWriter().addDocument(document);
52 } finally {
53 this.dirty=true;
54 }
55 }
56
57 public int delete(String field, String value) throws IOException {
58 return this.delete(new Term(field, value));
59 }
60
61 public synchronized int delete(Term term) throws IOException {
62 IndexReader reader = IndexReader.open(this.indexPath);
63 try {
64 return reader.delete(term);
65 } finally {
66 this.dirty=true;
67 reader.close();
68 // !!! does the writer need to be invalidated at this point?
69 }
70 }
71
72 public synchronized void update(String matchField, Document document) throws IOException {
73 this.delete(matchField, document.get(matchField));
74 this.add(document);
75 }
76
77 public Hits search(String field, String queryString) throws ParseException, IOException {
78 Query query = QueryParser.parse(queryString, field, new SimpleAnalyzer());
79 return this.search(query);
80 }
81
82 public Hits search(Query query) throws IOException {
83 synchronized (this) {
84 if (this.writer!=null) {
85 // commit pending writes
86 writer.optimize();
87 writer.close();
88 writer=null;
89 }
90 if (this.dirty) {
91 // recreate reader
92 // !!! this interrupts ongoing searches
93 this.searcher.close();
94 this.searcher = new IndexSearcher(indexPath);
95 dirty = false;
96 }
97 }
98 Hits hits = this.searcher.search(query);
99 return hits;
100 }
101
102 public synchronized void close() {
103 try {
104 if (this.writer!=null) {
105 this.writer.close();
106 }
107 } catch (IOException e) {
108 e.printStackTrace();
109 } finally {
110 this.writer=null;
111 }
112 try {
113 this.searcher.close();
114 } catch (IOException e) {
115 e.printStackTrace();
116 } finally {
117 this.searcher=null;
118 }
119 }
120
121 }
This page was automatically generated by Maven