Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
598:e930f92d0f61 | 599:50540f0813e2 |
---|---|
18 import org.apache.lucene.analysis.core.KeywordAnalyzer; | 18 import org.apache.lucene.analysis.core.KeywordAnalyzer; |
19 import org.apache.lucene.document.Document; | 19 import org.apache.lucene.document.Document; |
20 import org.apache.lucene.document.Field; | 20 import org.apache.lucene.document.Field; |
21 import org.apache.lucene.document.StoredField; | 21 import org.apache.lucene.document.StoredField; |
22 import org.apache.lucene.document.StringField; | 22 import org.apache.lucene.document.StringField; |
23 import org.apache.lucene.document.TextField; | |
23 import org.apache.lucene.document.IntField; | 24 import org.apache.lucene.document.IntField; |
24 import org.apache.lucene.document.LongField; | 25 import org.apache.lucene.document.LongField; |
25 import org.apache.lucene.document.DoubleField; | 26 import org.apache.lucene.document.DoubleField; |
26 import org.apache.lucene.index.IndexableField; | 27 import org.apache.lucene.index.IndexableField; |
27 import org.apache.lucene.index.IndexWriter; | 28 import org.apache.lucene.index.IndexWriter; |
67 | 68 |
68 public final class LuceneIndex implements Closeable { | 69 public final class LuceneIndex implements Closeable { |
69 private static final Logger logger = LoggerFactory.getLogger(LuceneIndex.class); | 70 private static final Logger logger = LoggerFactory.getLogger(LuceneIndex.class); |
70 | 71 |
71 private static final String FLD_NEXT_ID = "nextId"; | 72 private static final String FLD_NEXT_ID = "nextId"; |
72 private static final Analyzer analyzer = new KeywordAnalyzer(); | 73 public static final StringFieldParser STRING_FIELD_PARSER = new StringFieldParser(new KeywordAnalyzer()); |
73 public static final FieldParser STRING_FIELD_PARSER = new StringFieldParser(analyzer); | |
74 | 74 |
75 private final ReentrantLock writeLock = new ReentrantLock(); | 75 private final ReentrantLock writeLock = new ReentrantLock(); |
76 private final File indexDir; | 76 private final File indexDir; |
77 final SnapshotDeletionPolicy snapshotDeletionPolicy; | 77 final SnapshotDeletionPolicy snapshotDeletionPolicy; |
78 private final IndexWriter writer; | 78 private final IndexWriter writer; |
79 private DirectoryReader reader; | 79 private DirectoryReader reader; |
80 private IndexSearcher searcher; | 80 private IndexSearcher searcher; |
81 private final ThreadLocal<IndexSearcher> threadLocalSearcher = new ThreadLocal<IndexSearcher>(); | 81 private final ThreadLocal<IndexSearcher> threadLocalSearcher = new ThreadLocal<IndexSearcher>(); |
82 private boolean isClosed = false; | 82 private boolean isClosed = false; |
83 private final MultiFieldParser mfp = new MultiFieldParser(); | 83 private final MultiFieldParser mfp; |
84 | 84 |
85 public LuceneIndex(LuanState luan,String indexDirStr) throws LuanException, IOException { | 85 public LuceneIndex(LuanState luan,String indexDirStr,FieldParser defaultFieldParser,String[] defaultFields) throws LuanException, IOException { |
86 mfp = defaultFieldParser==null ? new MultiFieldParser() : new MultiFieldParser(defaultFieldParser,defaultFields); | |
86 mfp.fields.put( "type", STRING_FIELD_PARSER ); | 87 mfp.fields.put( "type", STRING_FIELD_PARSER ); |
87 mfp.fields.put( "id", NumberFieldParser.LONG ); | 88 mfp.fields.put( "id", NumberFieldParser.LONG ); |
88 File indexDir = new File(indexDirStr); | 89 File indexDir = new File(indexDirStr); |
89 this.indexDir = indexDir; | 90 this.indexDir = indexDir; |
90 Directory dir = FSDirectory.open(indexDir); | 91 Directory dir = FSDirectory.open(indexDir); |
91 Version version = Version.LUCENE_4_9; | 92 Version version = Version.LUCENE_4_9; |
93 Analyzer analyzer = STRING_FIELD_PARSER.analyzer; | |
94 if( defaultFieldParser instanceof StringFieldParser ) { | |
95 StringFieldParser sfp = (StringFieldParser)defaultFieldParser; | |
96 analyzer = sfp.analyzer; | |
97 } | |
92 IndexWriterConfig conf = new IndexWriterConfig(version,analyzer); | 98 IndexWriterConfig conf = new IndexWriterConfig(version,analyzer); |
93 snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy()); | 99 snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy()); |
94 conf.setIndexDeletionPolicy(snapshotDeletionPolicy); | 100 conf.setIndexDeletionPolicy(snapshotDeletionPolicy); |
95 writer = new IndexWriter(dir,conf); | 101 writer = new IndexWriter(dir,conf); |
96 writer.commit(); // commit index creation | 102 writer.commit(); // commit index creation |
413 throw new LuanException(luan,"key must be string"); | 419 throw new LuanException(luan,"key must be string"); |
414 String name = (String)key; | 420 String name = (String)key; |
415 Object value = entry.getValue(); | 421 Object value = entry.getValue(); |
416 if( value instanceof String ) { | 422 if( value instanceof String ) { |
417 String s = (String)value; | 423 String s = (String)value; |
418 if( indexed.contains(name) ) { | 424 FieldParser fp = mfp.fields.get(name); |
419 doc.add(new StringField(name, s, Field.Store.YES)); | 425 if( fp != null ) { |
426 if( fp instanceof StringFieldParser && fp != STRING_FIELD_PARSER ) { | |
427 doc.add(new TextField(name, s, Field.Store.YES)); | |
428 } else { | |
429 doc.add(new StringField(name, s, Field.Store.YES)); | |
430 } | |
420 } else { | 431 } else { |
421 doc.add(new StoredField(name, s)); | 432 doc.add(new StoredField(name, s)); |
422 } | 433 } |
423 } else if( value instanceof Integer ) { | 434 } else if( value instanceof Integer ) { |
424 int i = (Integer)value; | 435 int i = (Integer)value; |