Mercurial Hosting > luan
annotate src/goodjava/lucene/api/LuceneIndexWriter.java @ 1476:7d145095cc0b
lucene.logging check
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 19 Apr 2020 20:42:26 -0600 |
parents | 5e3870618377 |
children | f848d40b3b07 |
rev | line source |
---|---|
1460 | 1 package goodjava.lucene.api; |
2 | |
3 import java.io.IOException; | |
4 import java.util.Map; | |
5 import java.util.HashMap; | |
6 import java.util.List; | |
7 import org.apache.lucene.analysis.Analyzer; | |
8 import org.apache.lucene.document.Document; | |
9 import org.apache.lucene.document.Field; | |
10 import org.apache.lucene.document.StoredField; | |
11 import org.apache.lucene.document.StringField; | |
12 import org.apache.lucene.document.TextField; | |
13 import org.apache.lucene.document.IntField; | |
14 import org.apache.lucene.document.LongField; | |
15 import org.apache.lucene.document.DoubleField; | |
16 import org.apache.lucene.document.FloatField; | |
17 import org.apache.lucene.index.IndexWriter; | |
18 import org.apache.lucene.index.IndexWriterConfig; | |
19 import org.apache.lucene.index.Term; | |
20 import org.apache.lucene.index.DirectoryReader; | |
21 import org.apache.lucene.index.IndexReader; | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
22 import org.apache.lucene.index.CheckIndex; |
1460 | 23 import org.apache.lucene.search.Query; |
24 import org.apache.lucene.search.IndexSearcher; | |
25 import org.apache.lucene.store.Directory; | |
26 import org.apache.lucene.util.Version; | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
27 import goodjava.logging.Logger; |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
28 import goodjava.logging.LoggerFactory; |
1460 | 29 |
30 | |
31 public final class LuceneIndexWriter implements GoodIndexWriter { | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
32 private static final Logger logger = LoggerFactory.getLogger(LuceneIndexWriter.class); |
1465 | 33 private final FieldAnalyzer fieldAnalyzer = new FieldAnalyzer(); |
34 public final Version luceneVersion; | |
1460 | 35 public final IndexWriterConfig luceneConfig; |
1465 | 36 public final IndexWriter luceneWriter; |
1460 | 37 public final GoodIndexWriterConfig goodConfig; |
38 private final Map<String,Boolean> indexedMap = new HashMap<String,Boolean>(); | |
39 | |
1465 | 40 public LuceneIndexWriter(Version luceneVersion,Directory dir,GoodIndexWriterConfig goodConfig) throws IOException { |
41 this.luceneVersion = luceneVersion; | |
42 this.luceneConfig = new IndexWriterConfig(luceneVersion,fieldAnalyzer); | |
43 this.luceneWriter = new IndexWriter(dir,luceneConfig); | |
1460 | 44 this.goodConfig = goodConfig; |
1465 | 45 luceneWriter.commit(); // commit index creation |
1460 | 46 } |
47 | |
48 public void close() throws IOException { | |
49 luceneWriter.close(); | |
50 } | |
51 | |
52 public void commit() throws IOException { | |
53 luceneWriter.commit(); | |
54 } | |
55 | |
56 public void rollback() throws IOException { | |
57 luceneWriter.rollback(); | |
58 } | |
59 | |
60 public void deleteAll() throws IOException { | |
61 luceneWriter.deleteAll(); | |
62 } | |
63 | |
64 public void deleteDocuments(Query query) throws IOException { | |
65 luceneWriter.deleteDocuments(query); | |
66 } | |
67 | |
68 public void addDocument(Map<String,Object> storedFields) throws IOException { | |
69 Document doc = newDocument(storedFields); | |
70 luceneWriter.addDocument(doc); | |
71 } | |
72 | |
73 public void updateDocument(String keyFieldName,Map<String,Object> storedFields) throws IOException { | |
74 if( !isIndexed(keyFieldName) ) | |
75 throw new RuntimeException("can't update using unindexed field "+keyFieldName); | |
76 if( fieldAnalyzer.isAdded(keyFieldName) ) | |
77 throw new RuntimeException("can't update using analyzeed field "+keyFieldName); | |
78 Document doc = newDocument(storedFields); | |
79 Object keyValue = storedFields.get(keyFieldName); | |
80 if( keyValue==null ) | |
81 throw new RuntimeException("no value for field "+keyFieldName); | |
82 Term term = LuceneUtils.term(keyFieldName,keyValue); | |
83 luceneWriter.updateDocument(term,doc); | |
84 } | |
85 | |
86 private Document newDocument(Map<String,Object> storedFields) { | |
87 Document doc = new Document(); | |
88 addFields(doc,storedFields,Field.Store.YES); | |
89 Map<String,Object> unstoredFields = goodConfig.getUnstoredFields(storedFields); | |
90 addFields(doc,unstoredFields,Field.Store.NO); | |
91 return doc; | |
92 } | |
93 | |
94 private void addFields( Document doc, Map<String,Object> fields, Field.Store store ) { | |
95 for( Map.Entry<String,Object> entry : fields.entrySet() ) { | |
96 String name = entry.getKey(); | |
97 Object value = entry.getValue(); | |
98 if( value instanceof List ) { | |
99 for( Object v : (List)value ) { | |
100 doc.add( newField(name,v,store) ); | |
101 } | |
102 } else { | |
103 doc.add( newField(name,value,store) ); | |
104 } | |
105 } | |
106 } | |
107 | |
108 private Field newField( String name, Object value, Field.Store store ) { | |
109 boolean isIndexed = isIndexed(name); | |
110 if( store==Field.Store.NO && !isIndexed ) | |
111 throw new RuntimeException("field '"+name+"' is unstored and unindexed"); | |
112 if( value instanceof String ) { | |
113 String s = (String)value; | |
114 if( !isIndexed ) { | |
115 return new StoredField(name,s); | |
116 } else if( !fieldAnalyzer.isAdded(name) ) { | |
117 return new StringField(name,s,store); | |
118 } else { | |
119 return new TextField(name,s,store); | |
120 } | |
121 } else if( value instanceof Integer ) { | |
122 int i = (Integer)value; | |
123 if( !isIndexed ) { | |
124 return new StoredField(name,i); | |
125 } else { | |
126 return new IntField(name,i,store); | |
127 } | |
128 } else if( value instanceof Long ) { | |
129 long i = (Long)value; | |
130 if( !isIndexed ) { | |
131 return new StoredField(name,i); | |
132 } else { | |
133 return new LongField(name,i,store); | |
134 } | |
135 } else if( value instanceof Double ) { | |
136 double i = (Double)value; | |
137 if( !isIndexed ) { | |
138 return new StoredField(name,i); | |
139 } else { | |
140 return new DoubleField(name,i,store); | |
141 } | |
142 } else if( value instanceof Float ) { | |
143 float i = (Float)value; | |
144 if( !isIndexed ) { | |
145 return new StoredField(name,i); | |
146 } else { | |
147 return new FloatField(name,i,store); | |
148 } | |
149 } else if( value instanceof byte[] ) { | |
150 if( isIndexed ) | |
151 throw new RuntimeException("can't index byte field "+name); | |
152 byte[] b = (byte[])value; | |
153 return new StoredField(name, b); | |
154 } else | |
155 throw new RuntimeException("invalid value type "+value.getClass()+"' for field '"+name+"'"); | |
156 } | |
157 | |
158 private synchronized boolean isIndexed(String fieldName) { | |
159 Boolean b = indexedMap.get(fieldName); | |
160 if( b==null ) { | |
161 b = goodConfig.isIndexed(fieldName); | |
162 indexedMap.put(fieldName,b); | |
163 Analyzer analyzer = goodConfig.getAnalyzer(fieldName); | |
164 if( analyzer!=null ) | |
165 fieldAnalyzer.add(fieldName,analyzer); | |
166 } | |
167 return b; | |
168 } | |
169 | |
170 | |
171 public void reindexDocuments(final String keyFieldName,Query query) throws IOException { | |
1465 | 172 IndexReader reader = openReader(); |
1460 | 173 final IndexSearcher searcher = new IndexSearcher(reader); |
174 searcher.search( query, new GoodCollector(){ | |
175 public void collectDoc(int iDoc) throws IOException { | |
176 Document doc = searcher.doc(iDoc); | |
177 Map<String,Object> storedFields = LuceneUtils.toMap(doc); | |
178 updateDocument(keyFieldName,storedFields); | |
179 } | |
180 }); | |
181 reader.close(); | |
182 } | |
1465 | 183 |
184 public IndexReader openReader() throws IOException { | |
185 return DirectoryReader.open(luceneWriter.getDirectory()); | |
186 } | |
1476
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
187 |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
188 public void check() throws IOException { |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
189 CheckIndex.Status status = new CheckIndex(luceneWriter.getDirectory()).checkIndex(); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
190 if( !status.clean ) |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
191 logger.error("index not clean"); |
7d145095cc0b
lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents:
1465
diff
changeset
|
192 } |
1460 | 193 } |