Mercurial Hosting > luan
comparison lucene/src/luan/modules/lucene/Lucene.luan @ 544:c5a93767cc5c
lucene overhaul, untested
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 12 Jun 2015 19:11:44 -0600 |
parents | 7170a0c414d5 |
children | ddcd4296107a |
comparison
equal
deleted
inserted
replaced
543:9767da72545b | 544:c5a93767cc5c |
---|---|
1 java() | 1 java() |
2 local Luan = require "luan:Luan" | 2 local Luan = require "luan:Luan" |
3 local pairs = Luan.pairs | |
4 local ipairs = Luan.ipairs | |
5 local type = Luan.type | |
6 local number_type = Luan.number_type | |
7 local error = Luan.error | 3 local error = Luan.error |
8 local Number = require "luan:Number" | |
9 local number_type = Number.number_type | |
10 local Table = require "luan:Table" | |
11 local unpack = Table.unpack | |
12 local LuceneIndex = require "java:luan.modules.lucene.LuceneIndex" | 4 local LuceneIndex = require "java:luan.modules.lucene.LuceneIndex" |
13 local Term = require "java:org.apache.lucene.index.Term" | 5 local NumberFieldParser = require "java:sane.lucene.queryparser.NumberFieldParser" |
14 local TermQuery = require "java:org.apache.lucene.search.TermQuery" | 6 |
15 local TermRangeQuery = require "java:org.apache.lucene.search.TermRangeQuery" | |
16 local NumericRangeQuery = require "java:org.apache.lucene.search.NumericRangeQuery" | |
17 local MatchAllDocsQuery = require "java:org.apache.lucene.search.MatchAllDocsQuery" | |
18 local BooleanQuery = require "java:org.apache.lucene.search.BooleanQuery" | |
19 local BooleanClause = require "java:org.apache.lucene.search.BooleanClause" | |
20 local BytesRef = require "java:org.apache.lucene.util.BytesRef" | |
21 local NumericUtils = require "java:org.apache.lucene.util.NumericUtils" | |
22 local Sort = require "java:org.apache.lucene.search.Sort" | |
23 local SortField = require "java:org.apache.lucene.search.SortField" | |
24 | 7 |
25 local M = {} | 8 local M = {} |
9 | |
10 M.type = { | |
11 string = LuceneIndex.STRING_FIELD_PARSER; | |
12 integer = NumberFieldParser.INT; | |
13 long = NumberFieldParser.LONG; | |
14 double = NumberFieldParser.DOUBLE; | |
15 } | |
26 | 16 |
27 function M.index(indexDir) | 17 function M.index(indexDir) |
28 local index = {} | 18 local index = {} |
29 local java_index = LuceneIndex.new(indexDir) | 19 local java_index = LuceneIndex.new(indexDir) |
30 index.fields = java_index.fields.newTable() | 20 index.indexed_fields = java_index.indexedFieldsMeta.newTable() |
31 index.to_string = java_index.to_string | 21 index.to_string = java_index.to_string |
32 index.backup = java_index.backup | 22 index.backup = java_index.backup |
33 index.Writer = java_index.Writer | 23 index.Writer = java_index.Writer |
34 index.Searcher = java_index.Searcher | 24 index.Searcher = java_index.Searcher |
35 index.delete_all = java_index.delete_all | 25 index.delete_all = java_index.delete_all |
36 index.map_field_name = java_index.map_field_name | |
37 index.close = java_index.close | 26 index.close = java_index.close |
38 | 27 |
39 function index.save_document(doc) | 28 function index.save_document(doc) |
40 index.Writer( function(writer) | 29 index.Writer( function(writer) |
41 writer.save_document(doc) | 30 writer.save_document(doc) |
68 local _, _, total_hits = searcher.search(query,0) | 57 local _, _, total_hits = searcher.search(query,0) |
69 return total_hits | 58 return total_hits |
70 end ) | 59 end ) |
71 end | 60 end |
72 | 61 |
73 | |
74 | |
75 local queryTbl = {} | |
76 index.query = queryTbl | |
77 | |
78 queryTbl.parse = java_index.parse | |
79 | |
80 queryTbl.all_docs = MatchAllDocsQuery.new() | |
81 | |
82 function queryTbl.term(field,value) | |
83 if value==nil and type(field)=="table" then | |
84 local iter = pairs(field) | |
85 field, value = iter() | |
86 iter()==nil or error "only one term allowed" | |
87 end | |
88 field and value or error "missing term" | |
89 field = index.map_field_name(field) | |
90 local tp = type(value) | |
91 if tp == "string" then | |
92 return TermQuery.new(Term.new(field,value)) | |
93 end | |
94 if tp == "number" then | |
95 local br = BytesRef.new() | |
96 tp = number_type(value) | |
97 if tp == "integer" then | |
98 NumericUtils.intToPrefixCoded(value,0,br) | |
99 elseif tp == "long" then | |
100 NumericUtils.longToPrefixCoded(value,0,br) | |
101 elseif tp == "double" then | |
102 value = NumericUtils.doubleToSortableLong(value) | |
103 NumericUtils.longToPrefixCoded(value,0,br) | |
104 else | |
105 error("invalid term value type: "..tp) | |
106 end | |
107 return TermQuery.new(Term.new(field,br)) | |
108 end | |
109 error("invalid term value type: "..tp) | |
110 end | |
111 | |
112 function queryTbl.range(field,min,max,include_min,include_max) | |
113 if include_min == nil then include_min = true end | |
114 if include_max == nil then include_max = true end | |
115 field = index.map_field_name(field) | |
116 local tp = type(min) | |
117 tp == type(max) or error "min and max types don't match" | |
118 if tp == "string" then | |
119 return TermRangeQuery.newStringRange(field,min,max,include_min,include_max) | |
120 end | |
121 if tp == "number" then | |
122 tp = number_type(min) | |
123 tp == number_type(max) or error "min and max number types don't match" | |
124 if tp == "integer" then | |
125 return NumericRangeQuery.newIntRange(field,min,max,include_min,include_max) | |
126 end | |
127 if tp == "long" then | |
128 return NumericRangeQuery.newLongRange(field,min,max,include_min,include_max) | |
129 end | |
130 if tp == "double" then | |
131 return NumericRangeQuery.newDoubleRange(field,min,max,include_min,include_max) | |
132 end | |
133 error("invalid term value type: "..tp) | |
134 end | |
135 error("invalid term value type: "..tp) | |
136 end | |
137 | |
138 function queryTbl.boolean(t) | |
139 local boolean_query = BooleanQuery.new() | |
140 for query, occur_string in pairs(t) do | |
141 local occur = BooleanClause.Occur.valueOf( occur_string.upper() ) | |
142 boolean_query.add( query, occur ) | |
143 end | |
144 return boolean_query | |
145 end | |
146 | |
147 -- and list | |
148 function queryTbl.all(t) | |
149 local bt = {} | |
150 for key, query in pairs(t) do | |
151 if type(key)=="string" then | |
152 query = queryTbl.term{[key]=query} | |
153 end | |
154 bt[query] = "MUST" | |
155 end | |
156 return queryTbl.boolean(bt) | |
157 end | |
158 | |
159 -- or list | |
160 function queryTbl.any(t) | |
161 local bt = {} | |
162 for key, query in pairs(t) do | |
163 if type(key)=="string" then | |
164 query = queryTbl.term{[key]=query} | |
165 end | |
166 bt[query] = "SHOULD" | |
167 end | |
168 return queryTbl.boolean(bt) | |
169 end | |
170 | |
171 | |
172 function queryTbl.sort(fields) | |
173 #fields > 0 or error "list of sort fields expected" | |
174 local a = {} | |
175 for _, f in ipairs(fields) do | |
176 f.field or error "missing sort field" | |
177 f.type or error "missing sort type" | |
178 local field = index.map_field_name(f.field) | |
179 local type = SortField.Type.valueOf( f.type.upper() ) | |
180 local reverse = f.reverse or false | |
181 a[#a+1] = SortField.new(field,type,reverse) | |
182 end | |
183 return Sort.new(unpack(a)) | |
184 end | |
185 | |
186 | |
187 return index | 62 return index |
188 end | 63 end |
189 | 64 |
190 return M | 65 return M |