1528
|
1 package luan.modules.lucene;
|
|
2
|
|
3 import java.util.Map;
|
1529
|
4 import java.util.LinkedHashMap;
|
|
5 import java.util.List;
|
1528
|
6 import java.util.Collections;
|
|
7 import org.apache.lucene.index.IndexWriterConfig;
|
|
8 import org.apache.lucene.index.SnapshotDeletionPolicy;
|
|
9 import org.apache.lucene.util.Version;
|
|
10 import goodjava.lucene.queryparser.MultiFieldParser;
|
|
11 import goodjava.lucene.api.MultiFieldParserConfig;
|
|
12 import goodjava.lucene.api.MoreFieldInfo;
|
1529
|
13 import luan.Luan;
|
1528
|
14 import luan.LuanFunction;
|
|
15 import luan.LuanTable;
|
|
16 import luan.LuanCloner;
|
|
17 import luan.LuanException;
|
|
18 import luan.LuanRuntimeException;
|
|
19
|
|
20
|
|
21 final class SupplementingConfig extends MultiFieldParserConfig {
|
|
22 private final LuanFunction supplementer;
|
|
23
|
|
24 SupplementingConfig(Version luceneVersion,MultiFieldParser mfp,LuanFunction supplementer) {
|
|
25 super(luceneVersion,mfp);
|
|
26 LuanCloner cloner = new LuanCloner(LuanCloner.Type.COMPLETE);
|
|
27 this.supplementer = (LuanFunction)cloner.get(supplementer);
|
|
28 }
|
|
29
|
|
30 public IndexWriterConfig newLuceneConfig() {
|
|
31 IndexWriterConfig luceneConfig = super.newLuceneConfig();
|
|
32 SnapshotDeletionPolicy snapshotDeletionPolicy = new SnapshotDeletionPolicy(luceneConfig.getIndexDeletionPolicy());
|
|
33 luceneConfig.setIndexDeletionPolicy(snapshotDeletionPolicy);
|
|
34 return luceneConfig;
|
|
35 }
|
|
36
|
|
37 public MoreFieldInfo getMoreFieldInfo(Map<String,Object> storedFields) {
|
|
38 if( supplementer == null )
|
|
39 return super.getMoreFieldInfo(storedFields);
|
|
40 try {
|
1529
|
41 LuanTable tbl = toTable(supplementer.luan(),storedFields);
|
1528
|
42 tbl = (LuanTable)supplementer.call(tbl);
|
|
43 if( tbl == null ) {
|
|
44 return super.getMoreFieldInfo(storedFields);
|
|
45 } else {
|
1529
|
46 return new MoreFieldInfo(toLucene(tbl),Collections.emptyMap());
|
1528
|
47 }
|
|
48 } catch(LuanException e) {
|
|
49 throw new LuanRuntimeException(e);
|
|
50 }
|
|
51 }
|
1529
|
52
|
|
53 static LuanTable toTable(Luan luan,Map map) throws LuanException {
|
|
54 LuanTable table = new LuanTable(luan);
|
|
55 for( Object obj : map.entrySet() ) {
|
|
56 Map.Entry entry = (Map.Entry)obj;
|
|
57 Object value = entry.getValue();
|
|
58 if( value instanceof List )
|
|
59 value = new LuanTable(luan,(List)value);
|
|
60 table.rawPut( entry.getKey(), value );
|
|
61 }
|
|
62 return table;
|
|
63 }
|
|
64
|
|
65 static Map<String,Object> toLucene(LuanTable table) throws LuanException {
|
|
66 Map<String,Object> map = new LinkedHashMap<String,Object>();
|
|
67 for( Map.Entry<Object,Object> entry : table.iterable() ) {
|
|
68 String name = (String)entry.getKey();
|
|
69 Object value = entry.getValue();
|
|
70 if( value instanceof LuanTable ) {
|
|
71 LuanTable list = (LuanTable)value;
|
|
72 if( !list.isList() )
|
|
73 throw new LuanException("table value for '"+name+"' must be a list");
|
|
74 value = list.asList();
|
|
75 }
|
|
76 map.put(name,value);
|
|
77 }
|
|
78 return map;
|
|
79 }
|
|
80
|
1528
|
81 }
|