Mercurial Hosting > luan
annotate src/goodjava/lucene/api/LuceneUtils.java @ 1805:7793bcb5f34a
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 08 May 2024 11:13:16 -0600 |
parents | c62324841dfb |
children |
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; | |
1771
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1461
diff
changeset
|
11 import goodjava.lucene.queryparser.NumberFieldParser; |
1460 | 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 ) { | |
1771
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1461
diff
changeset
|
59 return NumberFieldParser.term(name,(Long)value); |
1461
e5d48b85351c
start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1460
diff
changeset
|
60 } else if( value instanceof Integer ) { |
1771
c62324841dfb
improve NumberFieldParser and add key map lucene query
Franklin Schmidt <fschmidt@gmail.com>
parents:
1461
diff
changeset
|
61 return NumberFieldParser.term(name,(Integer)value); |
1460 | 62 } else |
63 throw new RuntimeException("invalid value type "+value.getClass()+"' for term '"+name+"'"); | |
64 } | |
65 | |
66 } |