view src/luan/modules/lucene/Lucene.luan @ 1343:7d9a1f8894b0

lucene change indexed_only_field() to indexed_only_fields()
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 22 Feb 2019 10:12:05 -0700
parents 60599adc27b8
children dc2af9d5463b
line wrap: on
line source

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 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 Rpc = require "luan:Rpc.luan"
local LuceneIndex = require "java:luan.modules.lucene.LuceneIndex"
local NumberFieldParser = require "java:luan.modules.lucene.queryparser.NumberFieldParser"
local StringFieldParser = require "java:luan.modules.lucene.queryparser.StringFieldParser"
local SaneQueryParser = require "java:luan.modules.lucene.queryparser.SaneQueryParser"
local Version = require "java:org.apache.lucene.util.Version"
local EnglishAnalyzer = require "java:org.apache.lucene.analysis.en.EnglishAnalyzer"


local Lucene = {}

Lucene.instances = {}

Lucene.type = {
	string = LuceneIndex.STRING_FIELD_PARSER;
	integer = NumberFieldParser.INT;
	long = NumberFieldParser.LONG;
	double = NumberFieldParser.DOUBLE;

	english = StringFieldParser.new(EnglishAnalyzer.new(Version.LUCENE_CURRENT))
}

Lucene.literal = SaneQueryParser.literal

function Lucene.index(index_dir,default_type,default_fields)
	local index = {}
	index.dir = index_dir
	local java_index = LuceneIndex.new(index_dir,default_type,default_fields)
	index.java = java_index
--	index.indexed_fields = java_index.indexedFieldsMeta.newTable()

	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.backup = java_index.backup
	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.close = java_index.close
	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

	Lucene.instances[index] = true

	function index.close()
		Lucene.instances[index] = nil
		java_index.close()
	end

	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.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

	function index.restore(zip_file)
		java_index.run_in_lock( function()
			local lucene_dir = uri("file:"..index.dir)
			local before_restore = lucene_dir.parent().child("before_restore.zip")
			index.zip(before_restore)
			java_index.close()
			lucene_dir.delete()
			Io.uri("os:unzip "..zip_file.canonical().to_string(),{dir=lucene_dir.parent()}).read_text()
			java_index.reopen()
		end )
	end

	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 = uri("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 = uri("file:"..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

return Lucene