Mercurial Hosting > luan
comparison src/luan/modules/lucene/Lucene.luan @ 775:1a68fc55a80c
simplify dir structure
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 26 Aug 2016 14:36:40 -0600 |
parents | lucene/src/luan/modules/lucene/Lucene.luan@50f279d3f889 |
children | 6b8ea0a9b7c9 |
comparison
equal
deleted
inserted
replaced
774:3e30cf310e56 | 775:1a68fc55a80c |
---|---|
1 java() | |
2 local Luan = require "luan:Luan.luan" | |
3 local error = Luan.error | |
4 local ipairs = Luan.ipairs or error() | |
5 local type = Luan.type or error() | |
6 local Html = require "luan:Html.luan" | |
7 local Io = require "luan:Io.luan" | |
8 local uri = Io.uri or error() | |
9 local String = require "luan:String.luan" | |
10 local matches = String.matches or error() | |
11 local Rpc = require "luan:Rpc.luan" | |
12 local LuceneIndex = require "java:luan.modules.lucene.LuceneIndex" | |
13 local NumberFieldParser = require "java:luan.modules.lucene.queryparser.NumberFieldParser" | |
14 local StringFieldParser = require "java:luan.modules.lucene.queryparser.StringFieldParser" | |
15 local SaneQueryParser = require "java:luan.modules.lucene.queryparser.SaneQueryParser" | |
16 local Version = require "java:org.apache.lucene.util.Version" | |
17 local EnglishAnalyzer = require "java:org.apache.lucene.analysis.en.EnglishAnalyzer" | |
18 | |
19 | |
20 local M = {} | |
21 | |
22 M.instances = {} | |
23 | |
24 M.type = { | |
25 string = LuceneIndex.STRING_FIELD_PARSER; | |
26 integer = NumberFieldParser.INT; | |
27 long = NumberFieldParser.LONG; | |
28 double = NumberFieldParser.DOUBLE; | |
29 | |
30 english = StringFieldParser.new(EnglishAnalyzer.new(Version.LUCENE_CURRENT)) | |
31 } | |
32 | |
33 M.literal = SaneQueryParser.literal | |
34 | |
35 function M.index(index_dir,default_type,default_fields) | |
36 local index = {} | |
37 index.dir = index_dir | |
38 local java_index = LuceneIndex.new(index_dir,default_type,default_fields) | |
39 index.indexed_fields = java_index.indexedFieldsMeta.newTable() | |
40 | |
41 -- index.indexed_only_fields[type][field] = fn(doc) | |
42 index.indexed_only_fields = java_index.indexed_only_fields | |
43 | |
44 index.to_string = java_index.to_string | |
45 -- index.backup = java_index.backup | |
46 index.snapshot = java_index.snapshot | |
47 index.advanced_search = java_index.advanced_search | |
48 index.search_in_transaction = java_index.search_in_transaction | |
49 index.delete_all = java_index.delete_all | |
50 index.delete = java_index.delete | |
51 index.save = java_index.save | |
52 index.update_in_transaction = java_index.update_in_transaction | |
53 -- index.close = java_index.close | |
54 index.ensure_open = java_index.ensure_open | |
55 index.next_id = java_index.nextId | |
56 index.highlighter = java_index.highlighter | |
57 | |
58 M.instances[index] = true | |
59 | |
60 function index.close() | |
61 M.instances[index] = nil | |
62 java_index.close() | |
63 end | |
64 | |
65 function index.search(query, from, to, sort) | |
66 from or error "missing 'from' parameter" | |
67 to or error "missing 'to' parameter" | |
68 local results = {} | |
69 local function fn(i,doc_fn) | |
70 if i >= from then | |
71 results[#results+1] = doc_fn() | |
72 end | |
73 end | |
74 local total_hits = index.advanced_search(query,fn,to,sort) | |
75 return results, total_hits | |
76 end | |
77 | |
78 function index.get_document(query) | |
79 local doc | |
80 local function fn(_,doc_fn) | |
81 doc = doc_fn() | |
82 end | |
83 local total_hits = index.advanced_search(query,fn,1) | |
84 total_hits <= 1 or error( "found " .. total_hits .. " documents" ) | |
85 return doc | |
86 end | |
87 | |
88 function index.count(query) | |
89 return index.advanced_search(query) | |
90 end | |
91 | |
92 function index.html_highlighter(query,formatter,container_tags) | |
93 local highlighter = index.highlighter(query,formatter) | |
94 return function(html) | |
95 local list = Html.parse(html,container_tags) | |
96 local result = {} | |
97 for _, obj in ipairs(list) do | |
98 if type(obj) == "string" then | |
99 obj = highlighter(obj) | |
100 end | |
101 result[#result+1] = obj | |
102 end | |
103 return Html.to_string(result) | |
104 end | |
105 end | |
106 | |
107 function index.zip(zip_file) | |
108 index.snapshot( function(dir_path,file_names) | |
109 zip_file.delete() | |
110 local zip_path = zip_file.canonical().to_string() | |
111 local dir = uri("file:"..dir_path) | |
112 local dir_name = dir.name() | |
113 local options = {dir=dir.parent()} | |
114 for _, file_name in ipairs(file_names) do | |
115 local cmd = "zip "..zip_path.." "..dir_name.."/"..file_name | |
116 Io.uri("os:"..cmd,options).read_text() | |
117 end | |
118 end ) | |
119 end | |
120 | |
121 function index.restore(zip_file) | |
122 java_index.run_in_lock( function() | |
123 local lucene_dir = uri("file:"..index.dir) | |
124 local before_restore = lucene_dir.parent().child("before_restore.zip") | |
125 index.zip(before_restore) | |
126 java_index.close() | |
127 lucene_dir.delete() | |
128 Io.uri("os:unzip "..zip_file.canonical().to_string(),{dir=lucene_dir.parent()}).read_text() | |
129 java_index.reopen() | |
130 end ) | |
131 end | |
132 | |
133 local function multi_error() | |
134 error "multiple lucene instances" | |
135 end | |
136 | |
137 if Rpc.functions.backup == nil then | |
138 | |
139 function Rpc.functions.lucene_backup(password) | |
140 Io.password == password or error "wrong password" | |
141 local zip_file = uri("file:"..index.dir).parent().child("backup.zip") | |
142 index.zip(zip_file) | |
143 return zip_file | |
144 end | |
145 | |
146 function Rpc.functions.lucene_restore(password,zip_file) | |
147 Io.password == password or error "wrong password" | |
148 local backup_zip = uri("file:"..index.dir).parent().child("backup.zip") | |
149 backup_zip.write(zip_file) | |
150 index.restore(backup_zip) | |
151 end | |
152 | |
153 else | |
154 Rpc.functions.lucene_backup = multi_error | |
155 Rpc.functions.lucene_restore = multi_error | |
156 end | |
157 | |
158 return index | |
159 end | |
160 | |
161 return M |