changeset 1525:f848d40b3b07

lucene.api add boosts
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 18 Jul 2020 20:41:47 -0600
parents 7902e74bfe5c
children efbc3720d3f3
files src/goodjava/lucene/api/GoodIndexWriterConfig.java src/goodjava/lucene/api/LuceneIndexWriter.java src/goodjava/lucene/api/MoreFieldInfo.java src/goodjava/lucene/api/MultiFieldParserConfig.java
diffstat 4 files changed, 33 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/src/goodjava/lucene/api/GoodIndexWriterConfig.java	Sat Jul 18 01:00:21 2020 -0600
+++ b/src/goodjava/lucene/api/GoodIndexWriterConfig.java	Sat Jul 18 20:41:47 2020 -0600
@@ -7,5 +7,5 @@
 public interface GoodIndexWriterConfig {
 	public boolean isIndexed(String fieldName);
 	public Analyzer getAnalyzer(String fieldName);
-	public Map<String,Object> getUnstoredFields(Map<String,Object> storedFields);
+	public MoreFieldInfo getMoreFieldInfo(Map<String,Object> storedFields);
 }
--- a/src/goodjava/lucene/api/LuceneIndexWriter.java	Sat Jul 18 01:00:21 2020 -0600
+++ b/src/goodjava/lucene/api/LuceneIndexWriter.java	Sat Jul 18 20:41:47 2020 -0600
@@ -85,26 +85,34 @@
 
 	private Document newDocument(Map<String,Object> storedFields) {
 		Document doc = new Document();
-		addFields(doc,storedFields,Field.Store.YES);
-		Map<String,Object> unstoredFields = goodConfig.getUnstoredFields(storedFields);
-		addFields(doc,unstoredFields,Field.Store.NO);
+		MoreFieldInfo more = goodConfig.getMoreFieldInfo(storedFields);
+		addFields(doc,storedFields,Field.Store.YES,more.boosts);
+		addFields(doc,more.unstoredFields,Field.Store.NO,more.boosts);
 		return doc;
 	}
 
-	private void addFields( Document doc, Map<String,Object> fields, Field.Store store ) {
+	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();
 			Object value = entry.getValue();
+			Float boost = boosts.get(name);
 			if( value instanceof List ) {
 				for( Object v : (List)value ) {
-					doc.add( newField(name,v,store) );
+					doc.add( newField(name,v,store,boost) );
 				}
 			} else {
-				doc.add( newField(name,value,store) );
+				doc.add( newField(name,value,store,boost) );
 			}
 		}
 	}
 
+	private Field newField( String name, Object value, Field.Store store, Float boost ) {
+		Field field = newField(name,value,store);
+		if( boost != null )
+			field.setBoost(boost);
+		return field;
+	}
+
 	private Field newField( String name, Object value, Field.Store store ) {
 		boolean isIndexed = isIndexed(name);
 		if( store==Field.Store.NO && !isIndexed )
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/lucene/api/MoreFieldInfo.java	Sat Jul 18 20:41:47 2020 -0600
@@ -0,0 +1,14 @@
+package goodjava.lucene.api;
+
+import java.util.Map;
+
+
+public final class MoreFieldInfo {
+	public final Map<String,Object> unstoredFields;
+	public final Map<String,Float> boosts;
+
+	public MoreFieldInfo(Map<String,Object> unstoredFields,Map<String,Float> boosts) {
+		this.unstoredFields = unstoredFields;
+		this.boosts = boosts;
+	}
+}
--- a/src/goodjava/lucene/api/MultiFieldParserConfig.java	Sat Jul 18 01:00:21 2020 -0600
+++ b/src/goodjava/lucene/api/MultiFieldParserConfig.java	Sat Jul 18 20:41:47 2020 -0600
@@ -29,7 +29,9 @@
 		return analyzer instanceof KeywordAnalyzer ? null : analyzer;
 	}
 
-	public Map<String,Object> getUnstoredFields(Map<String,Object> storedFields) {
-		return Collections.emptyMap();
+	private static final MoreFieldInfo noMoreFieldInfo = new MoreFieldInfo(Collections.emptyMap(),Collections.emptyMap());
+
+	public MoreFieldInfo getMoreFieldInfo(Map<String,Object> storedFields) {
+		return noMoreFieldInfo;
 	}
 }