comparison src/goodjava/lucene/api/LuceneIndexWriter.java @ 1528:3bd4d7963456

use goodjava/lucene/api
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 26 Jul 2020 23:11:53 -0600
parents f848d40b3b07
children c27dc6af87ca
comparison
equal deleted inserted replaced
1527:fa1e3adbebfb 1528:3bd4d7963456
14 import org.apache.lucene.document.LongField; 14 import org.apache.lucene.document.LongField;
15 import org.apache.lucene.document.DoubleField; 15 import org.apache.lucene.document.DoubleField;
16 import org.apache.lucene.document.FloatField; 16 import org.apache.lucene.document.FloatField;
17 import org.apache.lucene.index.IndexWriter; 17 import org.apache.lucene.index.IndexWriter;
18 import org.apache.lucene.index.IndexWriterConfig; 18 import org.apache.lucene.index.IndexWriterConfig;
19 import org.apache.lucene.index.LiveIndexWriterConfig;
19 import org.apache.lucene.index.Term; 20 import org.apache.lucene.index.Term;
20 import org.apache.lucene.index.DirectoryReader; 21 import org.apache.lucene.index.DirectoryReader;
21 import org.apache.lucene.index.IndexReader; 22 import org.apache.lucene.index.IndexReader;
22 import org.apache.lucene.index.CheckIndex; 23 import org.apache.lucene.index.CheckIndex;
23 import org.apache.lucene.search.Query; 24 import org.apache.lucene.search.Query;
24 import org.apache.lucene.search.IndexSearcher; 25 import org.apache.lucene.search.IndexSearcher;
25 import org.apache.lucene.store.Directory; 26 import org.apache.lucene.store.Directory;
26 import org.apache.lucene.util.Version;
27 import goodjava.logging.Logger; 27 import goodjava.logging.Logger;
28 import goodjava.logging.LoggerFactory; 28 import goodjava.logging.LoggerFactory;
29 29
30 30
31 public final class LuceneIndexWriter implements GoodIndexWriter { 31 public final class LuceneIndexWriter implements GoodIndexWriter {
32 private static final Logger logger = LoggerFactory.getLogger(LuceneIndexWriter.class); 32 private static final Logger logger = LoggerFactory.getLogger(LuceneIndexWriter.class);
33 private final FieldAnalyzer fieldAnalyzer = new FieldAnalyzer(); 33 private final FieldAnalyzer fieldAnalyzer;
34 public final Version luceneVersion;
35 public final IndexWriterConfig luceneConfig;
36 public final IndexWriter luceneWriter; 34 public final IndexWriter luceneWriter;
37 public final GoodIndexWriterConfig goodConfig; 35 public final GoodIndexWriterConfig goodConfig;
38 private final Map<String,Boolean> indexedMap = new HashMap<String,Boolean>(); 36 private final Map<String,Boolean> indexedMap = new HashMap<String,Boolean>();
39 37
40 public LuceneIndexWriter(Version luceneVersion,Directory dir,GoodIndexWriterConfig goodConfig) throws IOException { 38 public LuceneIndexWriter(Directory dir,GoodIndexWriterConfig goodConfig) throws IOException {
41 this.luceneVersion = luceneVersion; 39 IndexWriterConfig luceneConfig = goodConfig.newLuceneConfig();
42 this.luceneConfig = new IndexWriterConfig(luceneVersion,fieldAnalyzer); 40 Analyzer analyzer = luceneConfig.getAnalyzer();
41 if( !(analyzer instanceof FieldAnalyzer) )
42 throw new RuntimeException("analyzer must be FieldAnalyzer");
43 this.fieldAnalyzer = (FieldAnalyzer)analyzer;
43 this.luceneWriter = new IndexWriter(dir,luceneConfig); 44 this.luceneWriter = new IndexWriter(dir,luceneConfig);
44 this.goodConfig = goodConfig; 45 this.goodConfig = goodConfig;
45 luceneWriter.commit(); // commit index creation 46 luceneWriter.commit(); // commit index creation
47 }
48
49 public Directory getDirectory() {
50 return luceneWriter.getDirectory();
51 }
52
53 public LiveIndexWriterConfig getLuceneConfig() {
54 return luceneWriter.getConfig();
46 } 55 }
47 56
48 public void close() throws IOException { 57 public void close() throws IOException {
49 luceneWriter.close(); 58 luceneWriter.close();
50 } 59 }
72 81
73 public void updateDocument(String keyFieldName,Map<String,Object> storedFields) throws IOException { 82 public void updateDocument(String keyFieldName,Map<String,Object> storedFields) throws IOException {
74 if( !isIndexed(keyFieldName) ) 83 if( !isIndexed(keyFieldName) )
75 throw new RuntimeException("can't update using unindexed field "+keyFieldName); 84 throw new RuntimeException("can't update using unindexed field "+keyFieldName);
76 if( fieldAnalyzer.isAdded(keyFieldName) ) 85 if( fieldAnalyzer.isAdded(keyFieldName) )
77 throw new RuntimeException("can't update using analyzeed field "+keyFieldName); 86 throw new RuntimeException("can't update using analyzed field "+keyFieldName);
78 Document doc = newDocument(storedFields); 87 Document doc = newDocument(storedFields);
79 Object keyValue = storedFields.get(keyFieldName); 88 Object keyValue = storedFields.get(keyFieldName);
80 if( keyValue==null ) 89 if( keyValue==null )
81 throw new RuntimeException("no value for field "+keyFieldName); 90 throw new RuntimeException("no value for field "+keyFieldName);
82 Term term = LuceneUtils.term(keyFieldName,keyValue); 91 Term term = LuceneUtils.term(keyFieldName,keyValue);