Mercurial Hosting > luan
comparison lucene/src/luan/modules/lucene/Lucene.luan @ 287:4d53e9fc1bd9
change lucene search to take lucene objects
git-svn-id: https://luan-java.googlecode.com/svn/trunk@288 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Thu, 04 Dec 2014 09:27:20 +0000 |
parents | eb27e765affb |
children | 899253043270 |
comparison
equal
deleted
inserted
replaced
286:91be4027b2a8 | 287:4d53e9fc1bd9 |
---|---|
1 import "luan:Table" | |
1 import "luan:Java" | 2 import "luan:Java" |
2 import "java:luan.modules.lucene.LuceneIndex" | 3 import "java:luan.modules.lucene.LuceneIndex" |
4 import "java:org.apache.lucene.index.Term" | |
5 import "java:org.apache.lucene.search.TermQuery" | |
6 import "java:org.apache.lucene.search.MatchAllDocsQuery" | |
7 import "java:org.apache.lucene.search.BooleanQuery" | |
8 import "java:org.apache.lucene.search.BooleanClause" | |
9 import "java:org.apache.lucene.search.Sort" | |
10 import "java:org.apache.lucene.search.SortField" | |
3 | 11 |
4 | 12 |
5 function Index(indexDir) | 13 function Index(indexDir) |
6 local index = LuceneIndex.new(indexDir).table() | 14 local index = LuceneIndex.new(indexDir).table() |
7 | 15 |
37 local _, _, total_hits = searcher.search(query,0) | 45 local _, _, total_hits = searcher.search(query,0) |
38 return total_hits | 46 return total_hits |
39 end ) | 47 end ) |
40 end | 48 end |
41 | 49 |
50 | |
51 | |
52 local Query = {} | |
53 index.Query = Query | |
54 | |
55 Query.all_docs = MatchAllDocsQuery.new() | |
56 | |
57 function Query.term(t) | |
58 local iter = pairs(t) | |
59 local field, value = iter() | |
60 field and value or error "missing term" | |
61 iter()==nil or error "only one term allowed" | |
62 field = index.map_field_name(field) | |
63 return TermQuery.new(Term.new(field,value)) | |
64 end | |
65 | |
66 function Query.boolean(t) | |
67 local boolean_query = BooleanQuery.new() | |
68 for query, occur_string in pairs(t) do | |
69 local occur = BooleanClause.Occur.valueOf( occur_string.upper() ) | |
70 boolean_query.add( query, occur ) | |
71 end | |
72 return boolean_query | |
73 end | |
74 | |
75 -- and list | |
76 function Query.all(t) | |
77 local bt = {} | |
78 for key, query in pairs(t) do | |
79 if type(key)=="string" then | |
80 query = Query.term{[key]=query} | |
81 end | |
82 bt[query] = "MUST" | |
83 end | |
84 return Query.boolean(bt) | |
85 end | |
86 | |
87 -- or list | |
88 function Query.any(t) | |
89 local bt = {} | |
90 for key, query in pairs(t) do | |
91 if type(key)=="string" then | |
92 query = Query.term{[key]=query} | |
93 end | |
94 bt[query] = "SHOULD" | |
95 end | |
96 return Query.boolean(bt) | |
97 end | |
98 | |
99 | |
100 function Query.sort(fields) | |
101 #fields > 0 or error "list of sort fields expected" | |
102 local a = {} | |
103 for _, f in ipairs(fields) do | |
104 f.field or error "missing sort field" | |
105 f.type or error "missing sort type" | |
106 local type = SortField.Type.valueOf( f.type.upper() ) | |
107 local reverse = f.reverse or false | |
108 a[#a+1] = SortField.new(f.field,type,reverse) | |
109 end | |
110 return Sort.new(Table.unpack(a)) | |
111 end | |
112 | |
113 | |
42 return index | 114 return index |
43 end | 115 end |