view lucene/src/luan/modules/lucene/Web_search.luan @ 540:4362eb720da9

add Number module
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 01 Jun 2015 18:04:50 -0600
parents 473e456444ff
children c5a93767cc5c
line wrap: on
line source

local Luan = require "luan:Luan"
local load = Luan.load
local pairs = Luan.pairs
local ipairs = Luan.ipairs
local range = Luan.range
local to_string = Luan.to_string
local Number = require "luan:Number"
local integer = Number.integer
local long = Number.long
local double = Number.double
local Io = require "luan:Io"
local Http = require "luan:http/Http"
local String = require "luan:String"
local string_to_number = String.string_to_number
local Html = require "luan:Html"

local M = {}

local function form() %>
<html>
	<head>
		<% Html.simply_html_head() %>
		<title>Lucene Query</title>
	</head>
	<body>
		<div container>
			<h3 margin-top="1.5em">Lucene Query</h3>
			<form horizontal name="form0" method="post" margin-top="2em">
				<div row>
					<div colspan=2 align="right">
						<label>Query:</label>
					</div>
					<div colspan=10>
						<input name="query" size="80" value="query.all_docs" autofocus />
						<div textcolor="#888">Query examples: <i>query.term{ type = 'user' }</i> or <i>"type:user AND name:Joe"</i></div>
					</div>
				</div>
				<div row margin-top="1em">
					<div colspan=2 align="right">
						<label>Max Rows:</label>
					</div>
					<div colspan=10>
						<input name="rows" value="100" size="3" maxlength="5" /></p>
					</div>
				</div>
				<div row margin-top="1em">
					<div colspan=2 align="right">
						<label>Sort:</label>
					</div>
					<div colspan=10>
						<input name="sort" size="60" />
						<div textcolor="#888">Sort examples: sort{{ field = 'id', type='int' }}</div>
					</div>
				</div>
				<div row margin-top="1em">
					<div colspan=2></div>
					<div colspan=10>
						<input type="submit" textcolor="white" bgcolor="#337ab7" large/>
					</div>
				</div>
			</form>
		</div>
		<% Html.simply_html_body_bottom() %>
	</body>
</html>
<% end


local function result(query,sort,headers,table) %>
<html>
	<head>
		<% Html.simply_html_head() %>
		<title>Lucene Query</title>
	</head>
	<body>
		<div container>
			<h3 margin-top="1.5em">Lucene Query Results</h3>
			<div row>
				<div colspan=2 align="right">
					<label>Query:</label>
				</div>
				<div colspan=10>
					<b><%=Html.encode(to_string(query))%></b></p>
				</div>
			</div>
			<div row>
				<div colspan=2 align="right">
					<label>Sort:</label>
				</div>
				<div colspan=10>
					<b><%=Html.encode(to_string(sort))%></b></p>
				</div>
			</div>
			<table border condensed margin-top="1.5em">
				<tr>
					<th></th>
					<% for _, header in ipairs(headers) do %>
						<th><%=header%></th>
					<% end %>
				</tr>
				<% for i, row in ipairs(table) do %>
					<tr>
						<td><%=i%></td>
						<% for col in range(1, #headers) do %>
							<td><%= row[col] or "" %></td>
						<% end %>
					</tr>
				<% end %>
			</table>
		</div>
		<% Html.simply_html_body_bottom() %>
	</body>
</html>
<% end


local function index_of(tbl,val)
	for i, v in ipairs(tbl) do
		if v == val then
			return i
		end
	end
	local n = #tbl + 1
	tbl[n] = val
	return n
end


function M.of(index)

	return function()
		Io.stdout = Http.response.text_writer()
		local query_string = Http.request.parameter.query
		if query_string == nil then
			form()
			return
		end
		local query_env = {
			query=index.query;
			integer=integer;
			long=long;
			double=double;
		}
		local query = load(query_string,"<query>",query_env,true)()
		local rows = string_to_number(Http.request.parameter.rows)
		local sort = load(Http.request.parameter.sort,"<sort>",{sort=index.query.sort},true)()
		index.Searcher( function(searcher)
			local results, length, total_hits = searcher.search(query,rows,sort)
			local headers = {}
			local table = {}
			for doc in results do
				local row = {}
				for field, value in pairs(doc) do
					row[index_of(headers,field)] = value
				end
				table[#table+1] = row
			end
			result(query,sort,headers,table)
		end )
	end

end

return M