Mercurial Hosting > luan
view src/luan/modules/lucene/Lucene.luan @ 1375:5c3702f60200
cleanup
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 11 Jul 2019 23:19:11 -0600 |
parents | 709f7498a363 |
children | 4c9691d6288f |
line wrap: on
line source
require "java" local Luan = require "luan:Luan.luan" local error = Luan.error local ipairs = Luan.ipairs or error() local type = Luan.type or error() local set_metatable = Luan.set_metatable or error() local Boot = require "luan:Boot.luan" local Html = require "luan:Html.luan" local Io = require "luan:Io.luan" local uri = Io.uri or error() local String = require "luan:String.luan" local matches = String.matches or error() local Thread = require "luan:Thread.luan" local schedule = Thread.schedule or error() local run_for_backup = Thread.run_for_backup or error() local Time = require "luan:Time.luan" local Rpc = require "luan:Rpc.luan" local LuceneIndex = require "java:luan.modules.lucene.LuceneIndex" local NumberFieldParser = require "java:luan.lib.queryparser.NumberFieldParser" local SaneQueryParser = require "java:luan.lib.queryparser.SaneQueryParser" local Logging = require "luan:logging/Logging.luan" local logger = Logging.logger "Lucene" local Lucene = {} Lucene.type = { english = LuceneIndex.ENGLISH_FIELD_PARSER string = LuceneIndex.STRING_FIELD_PARSER integer = NumberFieldParser.INT long = NumberFieldParser.LONG double = NumberFieldParser.DOUBLE } Lucene.literal = SaneQueryParser.literal function Lucene.index(index_dir,default_type,default_fields) type(index_dir)=="table" or error "index_dir must be table" index_dir.to_uri_string and matches(index_dir.to_uri_string(),"^file:") or error "must be file" local index = {} index.dir = index_dir local java_index, closer = LuceneIndex.getLuceneIndex(index_dir.java.file,default_type,default_fields) index.java = java_index index.closer = closer or error() index.indexed_fields = {} local mt = {} set_metatable(index.indexed_fields,mt) function mt.__index(_,key) return java_index.getIndexedFieldParser(key) end function mt.__new_index(_,key,value) return java_index.setIndexedFieldParser(key,value) end index.to_string = java_index.to_string index.snapshot = java_index.snapshot index.advanced_search = java_index.advanced_search index.search_in_transaction = java_index.search_in_transaction index.delete_all = java_index.delete_all index.delete = java_index.delete index.save = java_index.save index.update_in_transaction = java_index.update_in_transaction index.ensure_open = java_index.ensure_open index.next_id = java_index.nextId index.highlighter = java_index.highlighter index.indexed_only_fields = java_index.indexed_only_fields index.count_tokens = java_index.count_tokens index.close = closer.close function index.search( query, from, to, options ) from or error "missing 'from' parameter" to or error "missing 'to' parameter" options = options or {} local explain_fld = options.explain local score_fld = options.score local results = {} local function fn(i,doc_fn,score) if i >= from then local doc if explain_fld == nil then doc = doc_fn() else local explanation doc, explanation = doc_fn("explain") doc[explain_fld] = explanation.toString() end if score_fld ~= nil then doc[score_fld] = score end results[#results+1] = doc end end local total_hits = index.advanced_search(query,fn,to,options.sort) return results, total_hits end function index.get_document(query) local doc local function fn(_,doc_fn,_) doc = doc_fn() end local total_hits = index.advanced_search(query,fn,1) total_hits <= 1 or error( "found " .. total_hits .. " documents" ) return doc end function index.count(query) return index.advanced_search(query) end function index.html_highlighter(query,formatter,container_tags) local highlighter = index.highlighter(query,formatter) return function(html) local list = Html.parse(html,container_tags) local result = {} for _, obj in ipairs(list) do if type(obj) == "string" then obj = highlighter(obj) end result[#result+1] = obj end return Html.to_string(result) end end function index.backup_to(backup_dir) backup_dir.delete() backup_dir.mkdir() index.snapshot( function(dir_path,file_names) local lucene_dir = uri("file:"..dir_path) for _, file_name in ipairs(file_names) do local lucene_file = lucene_dir.child(file_name) local backup_file = backup_dir.child(file_name) backup_file.link_to(lucene_file) end end ) end function index.schedule_backups_to(backup_dir,repeating_delay) local lucene_dir = index.dir local function backup() if backup_dir.last_modified() < lucene_dir.last_modified() then run_for_backup(function() index.backup_to(backup_dir) logger.info "backup" end) end end schedule( backup, { repeating_delay = repeating_delay or Time.period{minutes=10} run_on_close = true daemon = true } ) end function index.zip(zip_file) index.snapshot( function(dir_path,file_names) zip_file.delete() local zip_path = zip_file.canonical().to_string() local dir = uri("file:"..dir_path) local dir_name = dir.name() local options = {dir=dir.parent()} for _, file_name in ipairs(file_names) do local cmd = "zip "..zip_path.." "..dir_name.."/"..file_name Io.uri("os:"..cmd,options).read_text() end end ) end index.zip = Boot.no_security(index.zip) function index.restore(zip_file) java_index.run_in_lock( function() local lucene_dir = index.dir local before_restore = lucene_dir.parent().child("before_restore.zip") index.zip(before_restore) java_index.doClose() lucene_dir.delete() Io.uri("os:unzip "..zip_file.canonical().to_string(),{dir=lucene_dir.parent()}).read_text() java_index.reopen() end ) end index.restore = Boot.no_security(index.restore) local function multi_error() error "multiple lucene instances" end if Rpc.functions.lucene_backup == nil then function Rpc.functions.lucene_backup(password) Io.password == password or error "wrong password" local zip_file = index.dir.parent().child("backup.zip") index.zip(zip_file) return zip_file end function Rpc.functions.lucene_restore(password,zip_file) Io.password == password or error "wrong password" local backup_zip = index.dir.parent().child("backup.zip") backup_zip.write(zip_file) index.restore(backup_zip) end else Rpc.functions.lucene_backup = multi_error Rpc.functions.lucene_restore = multi_error end return index end function Lucene.recover(index_dir,restore_dir) if not index_dir.exists() and restore_dir.exists() then index_dir.mkdir() for _, child in ipairs(restore_dir.children()) do index_dir.child(child.name()).link_to(child) end logger.error "recovered" end end return Lucene