view lucene/src/luan/modules/lucene/Lucene.luan @ 709:96a280ca32a2

add Lucene.instances
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 18 May 2016 19:55:48 -0600
parents 1ed9e55f0be8
children 01e68da6983b
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 Html = require "luan:Html.luan"
local Io = require "luan:Io.luan"
local String = require "luan:String.luan"
local matches = String.matches or error()
local LuceneIndex = require "java:luan.modules.lucene.LuceneIndex"
local NumberFieldParser = require "java:sane.lucene.queryparser.NumberFieldParser"
local StringFieldParser = require "java:sane.lucene.queryparser.StringFieldParser"
local SaneQueryParser = require "java:sane.lucene.queryparser.SaneQueryParser"
local Version = require "java:org.apache.lucene.util.Version"
local EnglishAnalyzer = require "java:org.apache.lucene.analysis.en.EnglishAnalyzer"


local M = {}

M.instances = {}

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

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

M.literal = SaneQueryParser.literal

function M.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.indexed_fields = java_index.indexedFieldsMeta.newTable()

	-- index.indexed_only_fields[type][field] = fn(doc)
	index.indexed_only_fields = java_index.indexed_only_fields

	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

	M.instances[index] = true

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

	function index.search(query, from, to, sort)
		local results = {}
		local function fn(i,doc_fn)
			if i >= from then
				results[#results+1] = doc_fn()
			end
		end
		local total_hits = index.advanced_search(query,fn,to,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)
		matches(zip_file,[[\.zip$]]) or error("file "..zip_file.." doesn't end with '.zip'")
		index.snapshot( function(dir,file_names)
			local t = {}
			for _, file_name in ipairs(file_names) do
				t[file_name] = "file:"..dir.."/"..file_name
			end
			Io.zip(zip_file,t)
		end )
	end

	return index
end

return M