Mercurial Hosting > luan
view src/luan/modules/lucene/SupplementingConfig.java @ 1537:f7649ad6e3e7
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 07 Aug 2020 13:38:25 -0600 |
parents | e6d808f40bbc |
children | b89212fd04b5 |
line wrap: on
line source
package luan.modules.lucene; import java.util.Map; import java.util.LinkedHashMap; import java.util.List; import java.util.Collections; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.SnapshotDeletionPolicy; import org.apache.lucene.util.Version; import goodjava.lucene.queryparser.MultiFieldParser; import goodjava.lucene.api.MultiFieldParserConfig; import goodjava.lucene.api.MoreFieldInfo; import luan.Luan; import luan.LuanFunction; import luan.LuanTable; import luan.LuanCloner; import luan.LuanException; import luan.LuanRuntimeException; final class SupplementingConfig extends MultiFieldParserConfig { private final LuanFunction supplementer; SupplementingConfig(Version luceneVersion,MultiFieldParser mfp,LuanFunction supplementer) { super(luceneVersion,mfp); LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE); this.supplementer = (LuanFunction)cloner.get(supplementer); } public IndexWriterConfig newLuceneConfig() { IndexWriterConfig luceneConfig = super.newLuceneConfig(); SnapshotDeletionPolicy snapshotDeletionPolicy = new SnapshotDeletionPolicy(luceneConfig.getIndexDeletionPolicy()); luceneConfig.setIndexDeletionPolicy(snapshotDeletionPolicy); return luceneConfig; } public MoreFieldInfo getMoreFieldInfo(Map<String,Object> storedFields) { if( supplementer == null ) return super.getMoreFieldInfo(storedFields); try { LuanTable tbl = toTable(supplementer.luan(),storedFields); tbl = (LuanTable)supplementer.call(tbl); if( tbl == null ) { return super.getMoreFieldInfo(storedFields); } else { return new MoreFieldInfo(toLucene(tbl),Collections.emptyMap()); } } catch(LuanException e) { throw new LuanRuntimeException(e); } } static LuanTable toTable(Luan luan,Map map) throws LuanException { LuanTable table = new LuanTable(luan); for( Object obj : map.entrySet() ) { Map.Entry entry = (Map.Entry)obj; Object value = entry.getValue(); if( value instanceof List ) value = new LuanTable(luan,(List)value); table.rawPut( entry.getKey(), value ); } return table; } static Map<String,Object> toLucene(LuanTable table) throws LuanException { Map<String,Object> map = new LinkedHashMap<String,Object>(); for( Map.Entry<Object,Object> entry : table.iterable() ) { String name = (String)entry.getKey(); Object value = entry.getValue(); if( value instanceof LuanTable ) { LuanTable list = (LuanTable)value; if( !list.isList() ) throw new LuanException("table value for '"+name+"' must be a list"); value = list.asList(); } map.put(name,value); } return map; } }