diff src/goodjava/lucene/api/FieldAnalyzer.java @ 1460:3ab0d043370f

start lucene.api
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 23 Mar 2020 00:04:42 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/lucene/api/FieldAnalyzer.java	Mon Mar 23 00:04:42 2020 -0600
@@ -0,0 +1,31 @@
+package goodjava.lucene.api;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.analysis.AnalyzerWrapper;
+import org.apache.lucene.analysis.core.KeywordAnalyzer;
+
+
+public final class FieldAnalyzer extends AnalyzerWrapper {
+	private static final Analyzer defaultAnalyzer = new KeywordAnalyzer();
+	private final Map<String,Analyzer> fieldAnalyzers = new ConcurrentHashMap<String,Analyzer>();
+
+	public void add(String fieldName,Analyzer analyzer) {
+		fieldAnalyzers.put(fieldName,analyzer);
+	}
+
+	public boolean isAdded(String fieldName) {
+		return fieldAnalyzers.containsKey(fieldName);
+	}
+
+	protected Analyzer getWrappedAnalyzer(String fieldName) {
+		Analyzer analyzer = fieldAnalyzers.get(fieldName);
+/*
+		if( analyzer == null )
+			throw new RuntimeException("no analyzer for field: "+fieldName);
+		return analyzer;
+*/
+		return analyzer!=null ? analyzer : defaultAnalyzer;
+	}
+}