Mercurial Hosting > luan
comparison src/goodjava/lucene/api/LuceneIndexWriter.java @ 1687:f48db13ae2d9
unlogged lucene support
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 09 Jun 2022 19:44:41 -0600 |
parents | 35601f15ecc3 |
children |
comparison
equal
deleted
inserted
replaced
1686:e34b73678a4f | 1687:f48db13ae2d9 |
---|---|
44 this.luceneWriter = new IndexWriter(dir,luceneConfig); | 44 this.luceneWriter = new IndexWriter(dir,luceneConfig); |
45 this.goodConfig = goodConfig; | 45 this.goodConfig = goodConfig; |
46 luceneWriter.commit(); // commit index creation | 46 luceneWriter.commit(); // commit index creation |
47 } | 47 } |
48 | 48 |
49 public IndexWriter getLuceneIndexWriter() { | 49 @Override public IndexWriter getLuceneIndexWriter() { |
50 return luceneWriter; | 50 return luceneWriter; |
51 } | 51 } |
52 | 52 |
53 public void close() throws IOException { | 53 @Override public void close() throws IOException { |
54 luceneWriter.close(); | 54 luceneWriter.close(); |
55 } | 55 } |
56 | 56 |
57 public void commit() throws IOException { | 57 @Override public void commit() throws IOException { |
58 luceneWriter.commit(); | 58 luceneWriter.commit(); |
59 } | 59 } |
60 | 60 |
61 public void rollback() throws IOException { | 61 @Override public void rollback() throws IOException { |
62 luceneWriter.rollback(); | 62 luceneWriter.rollback(); |
63 } | 63 } |
64 | 64 |
65 public void deleteAll() throws IOException { | 65 @Override public void deleteAll() throws IOException { |
66 luceneWriter.deleteAll(); | 66 luceneWriter.deleteAll(); |
67 } | 67 } |
68 | 68 |
69 public void deleteDocuments(Query query) throws IOException { | 69 @Override public void deleteDocuments(Query query) throws IOException { |
70 luceneWriter.deleteDocuments(query); | 70 luceneWriter.deleteDocuments(query); |
71 } | 71 } |
72 | 72 |
73 public void addDocument(Map<String,Object> storedFields) throws IOException { | 73 @Override public void addDocument(Map<String,Object> storedFields) throws IOException { |
74 Document doc = newDocument(storedFields); | 74 Document doc = newDocument(storedFields); |
75 luceneWriter.addDocument(doc); | 75 luceneWriter.addDocument(doc); |
76 } | 76 } |
77 | 77 |
78 public void updateDocument(String keyFieldName,Map<String,Object> storedFields) throws IOException { | 78 @Override public void updateDocument(String keyFieldName,Map<String,Object> storedFields) throws IOException { |
79 if( !isIndexed(keyFieldName) ) | 79 if( !isIndexed(keyFieldName) ) |
80 throw new RuntimeException("can't update using unindexed field "+keyFieldName); | 80 throw new RuntimeException("can't update using unindexed field "+keyFieldName); |
81 if( fieldAnalyzer.isAdded(keyFieldName) ) | 81 if( fieldAnalyzer.isAdded(keyFieldName) ) |
82 throw new RuntimeException("can't update using analyzed field "+keyFieldName); | 82 throw new RuntimeException("can't update using analyzed field "+keyFieldName); |
83 Document doc = newDocument(storedFields); | 83 Document doc = newDocument(storedFields); |
89 } | 89 } |
90 | 90 |
91 private Document newDocument(Map<String,Object> storedFields) { | 91 private Document newDocument(Map<String,Object> storedFields) { |
92 Document doc = new Document(); | 92 Document doc = new Document(); |
93 MoreFieldInfo more = goodConfig.getMoreFieldInfo(storedFields); | 93 MoreFieldInfo more = goodConfig.getMoreFieldInfo(storedFields); |
94 addFields(doc,storedFields,Field.Store.YES,more.boosts); | 94 addFields( doc, storedFields, Field.Store.YES, more.boosts ); |
95 addFields(doc,more.unstoredFields,Field.Store.NO,more.boosts); | 95 addFields( doc, more.unstoredFields, Field.Store.NO, more.boosts ); |
96 return doc; | 96 return doc; |
97 } | |
98 | |
99 public void addDocument( Map<String,Object> storedFields, Map<String,Object> unstoredFields, Map<String,Float> boosts ) | |
100 throws IOException | |
101 { | |
102 Document doc = new Document(); | |
103 addFields( doc, storedFields, Field.Store.YES, boosts ); | |
104 addFields( doc, unstoredFields, Field.Store.NO, boosts ); | |
105 luceneWriter.addDocument(doc); | |
97 } | 106 } |
98 | 107 |
99 private void addFields( Document doc, Map<String,Object> fields, Field.Store store, Map<String,Float> boosts ) { | 108 private void addFields( Document doc, Map<String,Object> fields, Field.Store store, Map<String,Float> boosts ) { |
100 for( Map.Entry<String,Object> entry : fields.entrySet() ) { | 109 for( Map.Entry<String,Object> entry : fields.entrySet() ) { |
101 String name = entry.getKey(); | 110 String name = entry.getKey(); |
110 } | 119 } |
111 } | 120 } |
112 } | 121 } |
113 | 122 |
114 private Field newField( String name, Object value, Field.Store store, Float boost ) { | 123 private Field newField( String name, Object value, Field.Store store, Float boost ) { |
115 Field field = newField(name,value,store); | 124 Field field = newField2(name,value,store,boost); |
116 if( boost != null ) | 125 if( boost != null ) |
117 field.setBoost(boost); | 126 field.setBoost(boost); |
118 return field; | 127 return field; |
119 } | 128 } |
120 | 129 |
121 private Field newField( String name, Object value, Field.Store store ) { | 130 private Field newField2( String name, Object value, Field.Store store, Float boost ) { |
122 boolean isIndexed = isIndexed(name); | 131 boolean isIndexed = isIndexed(name); |
123 if( store==Field.Store.NO && !isIndexed ) | 132 if( store==Field.Store.NO && !isIndexed ) |
124 throw new RuntimeException("field '"+name+"' is unstored and unindexed"); | 133 throw new RuntimeException("field '"+name+"' is unstored and unindexed"); |
125 if( value instanceof String ) { | 134 if( value instanceof String ) { |
126 String s = (String)value; | 135 String s = (String)value; |
127 if( !isIndexed ) { | 136 if( !isIndexed ) { |
128 return new StoredField(name,s); | 137 return new StoredField(name,s); |
129 } else if( !fieldAnalyzer.isAdded(name) ) { | 138 } else if( !fieldAnalyzer.isAdded(name) ) { |
130 return new StringField(name,s,store); | 139 if( boost == null ) { |
140 return new StringField(name,s,store); | |
141 } else { | |
142 return new Field( name, s, Field.Store.NO, Field.Index.NOT_ANALYZED); | |
143 } | |
131 } else { | 144 } else { |
132 return new TextField(name,s,store); | 145 return new TextField(name,s,store); |
133 } | 146 } |
134 } else if( value instanceof Integer ) { | 147 } else if( value instanceof Integer ) { |
135 int i = (Integer)value; | 148 int i = (Integer)value; |
179 } | 192 } |
180 return b; | 193 return b; |
181 } | 194 } |
182 | 195 |
183 | 196 |
184 public void reindexDocuments(final String keyFieldName,Query query) throws IOException { | 197 @Override public void reindexDocuments(final String keyFieldName,Query query) throws IOException { |
185 IndexReader reader = openReader(); | 198 IndexReader reader = openReader(); |
186 final IndexSearcher searcher = new IndexSearcher(reader); | 199 final IndexSearcher searcher = new IndexSearcher(reader); |
187 searcher.search( query, new GoodCollector(){ | 200 searcher.search( query, new GoodCollector(){ |
188 public void collectDoc(int iDoc) throws IOException { | 201 @Override public void collectDoc(int iDoc) throws IOException { |
189 Document doc = searcher.doc(iDoc); | 202 Document doc = searcher.doc(iDoc); |
190 Map<String,Object> storedFields = LuceneUtils.toMap(doc); | 203 Map<String,Object> storedFields = LuceneUtils.toMap(doc); |
191 updateDocument(keyFieldName,storedFields); | 204 updateDocument(keyFieldName,storedFields); |
192 } | 205 } |
193 }); | 206 }); |
194 reader.close(); | 207 reader.close(); |
195 } | 208 } |
196 | 209 |
197 public IndexReader openReader() throws IOException { | 210 @Override public IndexReader openReader() throws IOException { |
198 return DirectoryReader.open(luceneWriter.getDirectory()); | 211 return DirectoryReader.open(luceneWriter.getDirectory()); |
199 } | 212 } |
200 | 213 |
201 public void check() throws IOException { | 214 public void check() throws IOException { |
202 CheckIndex.Status status = new CheckIndex(luceneWriter.getDirectory()).checkIndex(); | 215 CheckIndex.Status status = new CheckIndex(luceneWriter.getDirectory()).checkIndex(); |
203 if( !status.clean ) | 216 if( !status.clean ) |
204 logger.error("index not clean"); | 217 logger.error("index not clean"); |
205 } | 218 } |
206 | 219 |
207 public void tag(String tag) throws IOException {} | 220 @Override public void tag(String tag) throws IOException {} |
208 | 221 |
209 } | 222 } |