diff lucene/src/luan/modules/lucene/LuceneIndex.java @ 599:50540f0813e2

support default search fields in lucene; add search to blog;
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 16 Sep 2015 20:55:49 -0600
parents 790d5de23042
children d36027b41570
line wrap: on
line diff
--- a/lucene/src/luan/modules/lucene/LuceneIndex.java	Wed Sep 16 14:32:52 2015 -0600
+++ b/lucene/src/luan/modules/lucene/LuceneIndex.java	Wed Sep 16 20:55:49 2015 -0600
@@ -20,6 +20,7 @@
 import org.apache.lucene.document.Field;
 import org.apache.lucene.document.StoredField;
 import org.apache.lucene.document.StringField;
+import org.apache.lucene.document.TextField;
 import org.apache.lucene.document.IntField;
 import org.apache.lucene.document.LongField;
 import org.apache.lucene.document.DoubleField;
@@ -69,8 +70,7 @@
 	private static final Logger logger = LoggerFactory.getLogger(LuceneIndex.class);
 
 	private static final String FLD_NEXT_ID = "nextId";
-	private static final Analyzer analyzer = new KeywordAnalyzer();
-	public static final FieldParser STRING_FIELD_PARSER = new StringFieldParser(analyzer);
+	public static final StringFieldParser STRING_FIELD_PARSER = new StringFieldParser(new KeywordAnalyzer());
 
 	private final ReentrantLock writeLock = new ReentrantLock();
 	private final File indexDir;
@@ -80,15 +80,21 @@
 	private IndexSearcher searcher;
 	private final ThreadLocal<IndexSearcher> threadLocalSearcher = new ThreadLocal<IndexSearcher>();
 	private boolean isClosed = false;
-	private final MultiFieldParser mfp = new MultiFieldParser();
+	private final MultiFieldParser mfp;
 
-	public LuceneIndex(LuanState luan,String indexDirStr) throws LuanException, IOException {
+	public LuceneIndex(LuanState luan,String indexDirStr,FieldParser defaultFieldParser,String[] defaultFields) throws LuanException, IOException {
+		mfp = defaultFieldParser==null ? new MultiFieldParser() : new MultiFieldParser(defaultFieldParser,defaultFields);
 		mfp.fields.put( "type", STRING_FIELD_PARSER );
 		mfp.fields.put( "id", NumberFieldParser.LONG );
 		File indexDir = new File(indexDirStr);
 		this.indexDir = indexDir;
 		Directory dir = FSDirectory.open(indexDir);
-		Version version = Version.LUCENE_4_9;
+		Version version = Version.LUCENE_4_9;		
+		Analyzer analyzer = STRING_FIELD_PARSER.analyzer;
+		if( defaultFieldParser instanceof StringFieldParser ) {
+			StringFieldParser sfp = (StringFieldParser)defaultFieldParser;
+			analyzer = sfp.analyzer;
+		}
 		IndexWriterConfig conf = new IndexWriterConfig(version,analyzer);
 		snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy());
 		conf.setIndexDeletionPolicy(snapshotDeletionPolicy);
@@ -415,8 +421,13 @@
 			Object value = entry.getValue();
 			if( value instanceof String ) {
 				String s = (String)value;
-				if( indexed.contains(name) ) {
-					doc.add(new StringField(name, s, Field.Store.YES));
+				FieldParser fp = mfp.fields.get(name);
+				if( fp != null ) {
+					if( fp instanceof StringFieldParser && fp != STRING_FIELD_PARSER ) {
+						doc.add(new TextField(name, s, Field.Store.YES));
+					} else {
+						doc.add(new StringField(name, s, Field.Store.YES));
+					}
 				} else {
 					doc.add(new StoredField(name, s));
 				}