Mercurial Hosting > luan
changeset 1687:f48db13ae2d9
unlogged lucene support
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 09 Jun 2022 19:44:41 -0600 |
parents | e34b73678a4f |
children | 5eb985d1411f |
files | src/goodjava/lucene/api/LuceneIndexWriter.java src/goodjava/lucene/api/MultiFieldParserConfig.java src/luan/modules/logging/LuanLogger.java src/luan/modules/lucene/Lucene.luan src/luan/modules/lucene/LuceneIndex.java |
diffstat | 5 files changed, 55 insertions(+), 26 deletions(-) [+] |
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 {} }
--- a/src/goodjava/lucene/api/MultiFieldParserConfig.java Wed Jun 08 21:55:25 2022 -0600 +++ b/src/goodjava/lucene/api/MultiFieldParserConfig.java Thu Jun 09 19:44:41 2022 -0600 @@ -20,15 +20,15 @@ this.mfp = mfp; } - public IndexWriterConfig newLuceneConfig() { + @Override public IndexWriterConfig newLuceneConfig() { return new IndexWriterConfig(luceneVersion,new FieldAnalyzer()); } - public final boolean isIndexed(String fieldName) { + @Override public final boolean isIndexed(String fieldName) { return mfp.fields.containsKey(fieldName); } - public final Analyzer getAnalyzer(String fieldName) { + @Override public final Analyzer getAnalyzer(String fieldName) { FieldParser fp = mfp.fields.get(fieldName); if( !(fp instanceof StringFieldParser) ) return null; @@ -39,7 +39,7 @@ private static final MoreFieldInfo noMoreFieldInfo = new MoreFieldInfo(Collections.emptyMap(),Collections.emptyMap()); - public MoreFieldInfo getMoreFieldInfo(Map<String,Object> storedFields) { + @Override public MoreFieldInfo getMoreFieldInfo(Map<String,Object> storedFields) { return noMoreFieldInfo; } }
--- a/src/luan/modules/logging/LuanLogger.java Wed Jun 08 21:55:25 2022 -0600 +++ b/src/luan/modules/logging/LuanLogger.java Thu Jun 09 19:44:41 2022 -0600 @@ -38,6 +38,7 @@ private static final String KEY = "Logger.Appender"; private static volatile Appender globalAppender = GoodLoggerFactory.DEFAULT_APPENDER; + public static int level = Level.INFO; public static synchronized void initThreadLogging() { if( !(globalAppender instanceof ThreadLocalAppender) ) { @@ -90,7 +91,7 @@ } private static void configure() { - GoodLoggerFactory.setConfigurer( new SimpleConfigurer(Level.INFO,globalAppender) ); + GoodLoggerFactory.setConfigurer( new SimpleConfigurer(level,globalAppender) ); } }
--- a/src/luan/modules/lucene/Lucene.luan Wed Jun 08 21:55:25 2022 -0600 +++ b/src/luan/modules/lucene/Lucene.luan Thu Jun 09 19:44:41 2022 -0600 @@ -127,9 +127,9 @@ end end - function index.save(doc) + function index.save(doc,unstored,boosts) index.check_in_transaction() - java_index.save(doc) + java_index.save(doc,unstored,boosts) end function index.search( query, from, to, options )
--- a/src/luan/modules/lucene/LuceneIndex.java Wed Jun 08 21:55:25 2022 -0600 +++ b/src/luan/modules/lucene/LuceneIndex.java Thu Jun 09 19:44:41 2022 -0600 @@ -307,7 +307,7 @@ } } - public void save(Luan luan,LuanTable doc) + public void save( Luan luan, LuanTable doc, LuanTable unstored, Map<String,Float> boosts ) throws LuanException, IOException, SQLException { Object obj = doc.get(luan,"id"); @@ -321,7 +321,22 @@ boolean commit = !writeLock.isHeldByCurrentThread(); writeLock.lock(); try { - if( id == null ) { + if( unstored!=null || boosts!=null ) { + if( unstored == null ) + throw new LuanException("unstored required with boosts"); + if( boosts == null ) + throw new LuanException("boosts required with unstored"); + if( id != null ) + throw new LuanException("update not supported"); + if( postgresBackup != null ) + throw new LuanException("not supported with postgres backup"); + if( !(writer instanceof LuceneIndexWriter) ) + throw new LuanException("not supported with index logging"); + id = ++this.id; + doc.put(luan,"id",id); + LuceneIndexWriter liw = (LuceneIndexWriter)writer; + liw.addDocument( toLucene(doc), toLucene(unstored), boosts ); + } else if( id == null ) { id = ++this.id; doc.put(luan,"id",id); if( postgresBackup != null )