annotate src/goodjava/lucene/api/LuceneIndexWriter.java @ 1769:9d9683e76496

bug fix
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 11 Jun 2023 19:17:26 -0600
parents f48db13ae2d9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.lucene.api;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.IOException;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import java.util.Map;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.util.HashMap;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.util.List;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import org.apache.lucene.analysis.Analyzer;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import org.apache.lucene.document.Document;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import org.apache.lucene.document.Field;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import org.apache.lucene.document.StoredField;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import org.apache.lucene.document.StringField;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 import org.apache.lucene.document.TextField;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 import org.apache.lucene.document.IntField;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 import org.apache.lucene.document.LongField;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 import org.apache.lucene.document.DoubleField;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 import org.apache.lucene.document.FloatField;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 import org.apache.lucene.index.IndexWriter;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 import org.apache.lucene.index.IndexWriterConfig;
1528
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1525
diff changeset
19 import org.apache.lucene.index.LiveIndexWriterConfig;
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 import org.apache.lucene.index.Term;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 import org.apache.lucene.index.DirectoryReader;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 import org.apache.lucene.index.IndexReader;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1465
diff changeset
23 import org.apache.lucene.index.CheckIndex;
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 import org.apache.lucene.search.Query;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 import org.apache.lucene.search.IndexSearcher;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 import org.apache.lucene.store.Directory;
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
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
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);
1528
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1525
diff changeset
33 private final FieldAnalyzer fieldAnalyzer;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1460
diff changeset
34 public final IndexWriter luceneWriter;
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 public final GoodIndexWriterConfig goodConfig;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 private final Map<String,Boolean> indexedMap = new HashMap<String,Boolean>();
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37
1528
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1525
diff changeset
38 public LuceneIndexWriter(Directory dir,GoodIndexWriterConfig goodConfig) throws IOException {
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1525
diff changeset
39 IndexWriterConfig luceneConfig = goodConfig.newLuceneConfig();
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1525
diff changeset
40 Analyzer analyzer = luceneConfig.getAnalyzer();
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1525
diff changeset
41 if( !(analyzer instanceof FieldAnalyzer) )
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1525
diff changeset
42 throw new RuntimeException("analyzer must be FieldAnalyzer");
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1525
diff changeset
43 this.fieldAnalyzer = (FieldAnalyzer)analyzer;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1460
diff changeset
44 this.luceneWriter = new IndexWriter(dir,luceneConfig);
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 this.goodConfig = goodConfig;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1460
diff changeset
46 luceneWriter.commit(); // commit index creation
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
49 @Override public IndexWriter getLuceneIndexWriter() {
1539
c27dc6af87ca remove nextId
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
50 return luceneWriter;
1528
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1525
diff changeset
51 }
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1525
diff changeset
52
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
53 @Override public void close() throws IOException {
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 luceneWriter.close();
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
57 @Override public void commit() throws IOException {
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 luceneWriter.commit();
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
61 @Override public void rollback() throws IOException {
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 luceneWriter.rollback();
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
65 @Override public void deleteAll() throws IOException {
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 luceneWriter.deleteAll();
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
69 @Override public void deleteDocuments(Query query) throws IOException {
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 luceneWriter.deleteDocuments(query);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
73 @Override public void addDocument(Map<String,Object> storedFields) throws IOException {
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 Document doc = newDocument(storedFields);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 luceneWriter.addDocument(doc);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
78 @Override public void updateDocument(String keyFieldName,Map<String,Object> storedFields) throws IOException {
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 if( !isIndexed(keyFieldName) )
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 throw new RuntimeException("can't update using unindexed field "+keyFieldName);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 if( fieldAnalyzer.isAdded(keyFieldName) )
1528
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1525
diff changeset
82 throw new RuntimeException("can't update using analyzed field "+keyFieldName);
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 Document doc = newDocument(storedFields);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 Object keyValue = storedFields.get(keyFieldName);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 if( keyValue==null )
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 throw new RuntimeException("no value for field "+keyFieldName);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 Term term = LuceneUtils.term(keyFieldName,keyValue);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 luceneWriter.updateDocument(term,doc);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 private Document newDocument(Map<String,Object> storedFields) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 Document doc = new Document();
1525
f848d40b3b07 lucene.api add boosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
93 MoreFieldInfo more = goodConfig.getMoreFieldInfo(storedFields);
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
94 addFields( doc, storedFields, Field.Store.YES, more.boosts );
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
95 addFields( doc, more.unstoredFields, Field.Store.NO, more.boosts );
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 return doc;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
99 public void addDocument( Map<String,Object> storedFields, Map<String,Object> unstoredFields, Map<String,Float> boosts )
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
100 throws IOException
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
101 {
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
102 Document doc = new Document();
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
103 addFields( doc, storedFields, Field.Store.YES, boosts );
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
104 addFields( doc, unstoredFields, Field.Store.NO, boosts );
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
105 luceneWriter.addDocument(doc);
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
106 }
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
107
1525
f848d40b3b07 lucene.api add boosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
108 private void addFields( Document doc, Map<String,Object> fields, Field.Store store, Map<String,Float> boosts ) {
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109 for( Map.Entry<String,Object> entry : fields.entrySet() ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 String name = entry.getKey();
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 Object value = entry.getValue();
1525
f848d40b3b07 lucene.api add boosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
112 Float boost = boosts.get(name);
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 if( value instanceof List ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 for( Object v : (List)value ) {
1525
f848d40b3b07 lucene.api add boosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
115 doc.add( newField(name,v,store,boost) );
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 } else {
1525
f848d40b3b07 lucene.api add boosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
118 doc.add( newField(name,value,store,boost) );
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122
1525
f848d40b3b07 lucene.api add boosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
123 private Field newField( String name, Object value, Field.Store store, Float boost ) {
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
124 Field field = newField2(name,value,store,boost);
1525
f848d40b3b07 lucene.api add boosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
125 if( boost != null )
f848d40b3b07 lucene.api add boosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
126 field.setBoost(boost);
f848d40b3b07 lucene.api add boosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
127 return field;
f848d40b3b07 lucene.api add boosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
128 }
f848d40b3b07 lucene.api add boosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
129
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
130 private Field newField2( String name, Object value, Field.Store store, Float boost ) {
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 boolean isIndexed = isIndexed(name);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 if( store==Field.Store.NO && !isIndexed )
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133 throw new RuntimeException("field '"+name+"' is unstored and unindexed");
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134 if( value instanceof String ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 String s = (String)value;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 if( !isIndexed ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137 return new StoredField(name,s);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138 } else if( !fieldAnalyzer.isAdded(name) ) {
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
139 if( boost == null ) {
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
140 return new StringField(name,s,store);
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
141 } else {
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
142 return new Field( name, s, Field.Store.NO, Field.Index.NOT_ANALYZED);
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
143 }
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 } else {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 return new TextField(name,s,store);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 } else if( value instanceof Integer ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148 int i = (Integer)value;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149 if( !isIndexed ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150 return new StoredField(name,i);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151 } else {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152 return new IntField(name,i,store);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154 } else if( value instanceof Long ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155 long i = (Long)value;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156 if( !isIndexed ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157 return new StoredField(name,i);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158 } else {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159 return new LongField(name,i,store);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161 } else if( value instanceof Double ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
162 double i = (Double)value;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163 if( !isIndexed ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164 return new StoredField(name,i);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165 } else {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166 return new DoubleField(name,i,store);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
168 } else if( value instanceof Float ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
169 float i = (Float)value;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
170 if( !isIndexed ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
171 return new StoredField(name,i);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172 } else {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173 return new FloatField(name,i,store);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
175 } else if( value instanceof byte[] ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
176 if( isIndexed )
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
177 throw new RuntimeException("can't index byte field "+name);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
178 byte[] b = (byte[])value;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
179 return new StoredField(name, b);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
180 } else
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181 throw new RuntimeException("invalid value type "+value.getClass()+"' for field '"+name+"'");
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
183
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184 private synchronized boolean isIndexed(String fieldName) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
185 Boolean b = indexedMap.get(fieldName);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186 if( b==null ) {
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
187 b = goodConfig.isIndexed(fieldName);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188 indexedMap.put(fieldName,b);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
189 Analyzer analyzer = goodConfig.getAnalyzer(fieldName);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
190 if( analyzer!=null )
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
191 fieldAnalyzer.add(fieldName,analyzer);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
192 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
193 return b;
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
194 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
196
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
197 @Override public void reindexDocuments(final String keyFieldName,Query query) throws IOException {
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1460
diff changeset
198 IndexReader reader = openReader();
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
199 final IndexSearcher searcher = new IndexSearcher(reader);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
200 searcher.search( query, new GoodCollector(){
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
201 @Override public void collectDoc(int iDoc) throws IOException {
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
202 Document doc = searcher.doc(iDoc);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
203 Map<String,Object> storedFields = LuceneUtils.toMap(doc);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
204 updateDocument(keyFieldName,storedFields);
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
205 }
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
206 });
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
207 reader.close();
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
208 }
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1460
diff changeset
209
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
210 @Override public IndexReader openReader() throws IOException {
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1460
diff changeset
211 return DirectoryReader.open(luceneWriter.getDirectory());
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1460
diff changeset
212 }
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1465
diff changeset
213
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1465
diff changeset
214 public void check() throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1465
diff changeset
215 CheckIndex.Status status = new CheckIndex(luceneWriter.getDirectory()).checkIndex();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1465
diff changeset
216 if( !status.clean )
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1465
diff changeset
217 logger.error("index not clean");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1465
diff changeset
218 }
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
219
1687
f48db13ae2d9 unlogged lucene support
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
220 @Override public void tag(String tag) throws IOException {}
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
221
1460
3ab0d043370f start lucene.api
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
222 }