diff 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
line wrap: on
line diff
--- a/src/goodjava/lucene/api/LuceneIndexWriter.java	Wed Jun 08 21:55:25 2022 -0600
+++ b/src/goodjava/lucene/api/LuceneIndexWriter.java	Thu Jun 09 19:44:41 2022 -0600
@@ -46,36 +46,36 @@
 		luceneWriter.commit();  // commit index creation
 	}
 
-	public IndexWriter getLuceneIndexWriter() {
+	@Override public IndexWriter getLuceneIndexWriter() {
 		return luceneWriter;
 	}
 
-	public void close() throws IOException {
+	@Override public void close() throws IOException {
 		luceneWriter.close();
 	}
 
-	public void commit() throws IOException {
+	@Override public void commit() throws IOException {
 		luceneWriter.commit();
 	}
 
-	public void rollback() throws IOException {
+	@Override public void rollback() throws IOException {
 		luceneWriter.rollback();
 	}
 
-	public void deleteAll() throws IOException {
+	@Override public void deleteAll() throws IOException {
 		luceneWriter.deleteAll();
 	}
 
-	public void deleteDocuments(Query query) throws IOException {
+	@Override public void deleteDocuments(Query query) throws IOException {
 		luceneWriter.deleteDocuments(query);
 	}
 
-	public void addDocument(Map<String,Object> storedFields) throws IOException {
+	@Override public void addDocument(Map<String,Object> storedFields) throws IOException {
 		Document doc = newDocument(storedFields);
 		luceneWriter.addDocument(doc);
 	}
 
-	public void updateDocument(String keyFieldName,Map<String,Object> storedFields) throws IOException {
+	@Override public void updateDocument(String keyFieldName,Map<String,Object> storedFields) throws IOException {
 		if( !isIndexed(keyFieldName) )
 			throw new RuntimeException("can't update using unindexed field "+keyFieldName);
 		if( fieldAnalyzer.isAdded(keyFieldName) )
@@ -91,11 +91,20 @@
 	private Document newDocument(Map<String,Object> storedFields) {
 		Document doc = new Document();
 		MoreFieldInfo more = goodConfig.getMoreFieldInfo(storedFields);
-		addFields(doc,storedFields,Field.Store.YES,more.boosts);
-		addFields(doc,more.unstoredFields,Field.Store.NO,more.boosts);
+		addFields( doc, storedFields, Field.Store.YES, more.boosts );
+		addFields( doc, more.unstoredFields, Field.Store.NO, more.boosts );
 		return doc;
 	}
 
+	public void addDocument( Map<String,Object> storedFields, Map<String,Object> unstoredFields, Map<String,Float> boosts )
+		throws IOException
+	{
+		Document doc = new Document();
+		addFields( doc, storedFields, Field.Store.YES, boosts );
+		addFields( doc, unstoredFields, Field.Store.NO, boosts );
+		luceneWriter.addDocument(doc);
+	}
+
 	private void addFields( Document doc, Map<String,Object> fields, Field.Store store, Map<String,Float> boosts ) {
 		for( Map.Entry<String,Object> entry : fields.entrySet() ) {
 			String name = entry.getKey();
@@ -112,13 +121,13 @@
 	}
 
 	private Field newField( String name, Object value, Field.Store store, Float boost ) {
-		Field field = newField(name,value,store);
+		Field field = newField2(name,value,store,boost);
 		if( boost != null )
 			field.setBoost(boost);
 		return field;
 	}
 
-	private Field newField( String name, Object value, Field.Store store ) {
+	private Field newField2( String name, Object value, Field.Store store, Float boost ) {
 		boolean isIndexed = isIndexed(name);
 		if( store==Field.Store.NO && !isIndexed )
 			throw new RuntimeException("field '"+name+"' is unstored and unindexed");
@@ -127,7 +136,11 @@
 			if( !isIndexed ) {
 				return new StoredField(name,s);
 			} else if( !fieldAnalyzer.isAdded(name) ) {
-				return new StringField(name,s,store);
+				if( boost == null ) {
+					return new StringField(name,s,store);
+				} else {
+					return new Field( name, s, Field.Store.NO, Field.Index.NOT_ANALYZED);
+				}
 			} else {
 				return new TextField(name,s,store);
 			}
@@ -181,11 +194,11 @@
 	}
 
 
-	public void reindexDocuments(final String keyFieldName,Query query) throws IOException {
+	@Override public void reindexDocuments(final String keyFieldName,Query query) throws IOException {
 		IndexReader reader = openReader();
 		final IndexSearcher searcher = new IndexSearcher(reader);
 		searcher.search( query, new GoodCollector(){
-			public void collectDoc(int iDoc) throws IOException {
+			@Override public void collectDoc(int iDoc) throws IOException {
 				Document doc = searcher.doc(iDoc);
 				Map<String,Object> storedFields = LuceneUtils.toMap(doc);
 				updateDocument(keyFieldName,storedFields);
@@ -194,7 +207,7 @@
 		reader.close();
 	}
 
-	public IndexReader openReader() throws IOException {
+	@Override public IndexReader openReader() throws IOException {
 		return DirectoryReader.open(luceneWriter.getDirectory());
 	}
 
@@ -204,6 +217,6 @@
 			logger.error("index not clean");
 	}
 
-	public void tag(String tag) throws IOException {}
+	@Override public void tag(String tag) throws IOException {}
 
 }