Mercurial Hosting > luan
annotate src/goodjava/lucene/api/LuceneUtils.java @ 1708:8d257d56dc42
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 04 Jul 2022 14:06:59 -0600 |
parents | e5d48b85351c |
children | c62324841dfb |
rev | line source |
---|---|
1460 | 1 package goodjava.lucene.api; |
2 | |
3 import java.util.Map; | |
4 import java.util.HashMap; | |
5 import java.util.List; | |
6 import java.util.ArrayList; | |
7 import org.apache.lucene.document.Document; | |
8 import org.apache.lucene.index.IndexableField; | |
9 import org.apache.lucene.index.Term; | |
10 import org.apache.lucene.util.BytesRef; | |
11 import org.apache.lucene.util.NumericUtils; | |
12 | |
13 | |
14 public final class LuceneUtils { | |
15 private LuceneUtils() {} // never | |
16 | |
17 public static Object getValue(IndexableField ifld) { | |
18 BytesRef br = ifld.binaryValue(); | |
19 if( br != null ) | |
20 return br.bytes; | |
21 Number n = ifld.numericValue(); | |
22 if( n != null ) | |
23 return n; | |
24 String s = ifld.stringValue(); | |
25 if( s != null ) | |
26 return s; | |
27 throw new RuntimeException("invalid field type for "+ifld); | |
28 } | |
29 | |
30 public static Map<String,Object> toMap(Document doc) { | |
31 if( doc==null ) | |
32 return null; | |
33 Map<String,Object> map = new HashMap<String,Object>(); | |
34 for( IndexableField ifld : doc ) { | |
35 String name = ifld.name(); | |
36 Object value = getValue(ifld); | |
37 Object old = map.get(name); | |
38 if( old == null ) { | |
39 map.put(name,value); | |
40 } else { | |
41 List list; | |
42 if( old instanceof List ) { | |
43 list = (List)old; | |
44 } else { | |
45 list = new ArrayList(); | |
46 list.add(old); | |
47 map.put(name,list); | |
48 } | |
49 list.add(value); | |
50 } | |
51 } | |
52 return map; | |
53 } | |
54 | |
55 public static Term term(String name,Object value) { | |
56 if( value instanceof String ) { | |
57 return new Term(name,(String)value); | |
58 } else if( value instanceof Long ) { | |
59 BytesRef br = new BytesRef(); | |
60 NumericUtils.longToPrefixCoded((Long)value,0,br); | |
61 return new Term(name,br); | |
1461
e5d48b85351c
start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1460
diff
changeset
|
62 } else if( value instanceof Integer ) { |
e5d48b85351c
start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1460
diff
changeset
|
63 BytesRef br = new BytesRef(); |
e5d48b85351c
start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1460
diff
changeset
|
64 NumericUtils.intToPrefixCoded((Integer)value,0,br); |
e5d48b85351c
start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1460
diff
changeset
|
65 return new Term(name,br); |
1460 | 66 } else |
67 throw new RuntimeException("invalid value type "+value.getClass()+"' for term '"+name+"'"); | |
68 } | |
69 | |
70 } |