Mercurial Hosting > luan
comparison src/goodjava/lucene/api/LuceneUtils.java @ 1460:3ab0d043370f
start lucene.api
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 23 Mar 2020 00:04:42 -0600 |
parents | |
children | e5d48b85351c |
comparison
equal
deleted
inserted
replaced
1459:b04b8fc5f4f4 | 1460:3ab0d043370f |
---|---|
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); | |
62 } else | |
63 throw new RuntimeException("invalid value type "+value.getClass()+"' for term '"+name+"'"); | |
64 } | |
65 | |
66 } |