view lucene/src/luan/modules/lucene/Web_search.luan @ 321:7f7708e8fdd4

remove import statement git-svn-id: https://luan-java.googlecode.com/svn/trunk@322 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 08 Feb 2015 07:26:20 +0000
parents d6cce1cc8948
children 78a6a71afbfd
line wrap: on
line source

local Luan = require "luan:Luan"
local load = Luan.load
local to_number = Luan.to_number
local pairs = Luan.pairs
local ipairs = Luan.ipairs
local range = Luan.range
local Io = require "luan:Io"
local Http = require "luan:web/Http"
local String = require "luan:String"


local function basic_style() %>
	body {font-family:'Arial',sans-serif;font-size:16px;padding:1em 2em}
	input {padding:.5em;border-radius:10px;border:1px solid #ccc;font-size:16px}
	input.btn {background:#3B619D;color:#FFF;padding:.5em 2em;font-size:20px}
	h1 {font-weight:bold;font-size: 20px}
	p {margin:1em 0 .2em}
	span.label {min-width:100px;display:inline-block;text-align:right}
	div.tip{color:#888;font-size:80%}
	table.results {margin-top:2em;border-collapse:collapse;font-size:90%}
	table.results th {background:#eee}
	table.results th,table.results td {border-left:1px solid #bbb;padding:.4em}
	table.results tr:nth-child(odd) td {background:#f8f8f8}
<% end

local function form() %>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>Lucene Query</title>
		<style><% basic_style() %></style>
	</head>
	<body>
		<h1>Lucene Query</h1>
		<form name="form0" method="post">
			<p>
				<span class="label">Query:</span> <input name="query" size="60" value="Query.all_docs" />
				<div class="tip"><span class="label"></span> Query examples: <i>Query.term{ type = 'user' }</i> or <i>"type:user AND name:Joe"</i></div>
			</p>
			<p><span class="label">Max Rows:</span> <input name="rows" value="100" maxlength="5" onkeypress="return event.charCode >= 48 && event.charCode <= 57" style="width:3em"/></p>
			<p>
				<span class="label">Sort:</span> <input name="sort" size="60" />
				<div class="tip"><span class="label"></span> Sort examples: Query.sort{{ field = 'id', type='int' }}</div>
			</p>
			<p><input type="submit" class="btn"/></p>
		</form>
		<script>document.form0.query.focus();</script>
	</body>
</html>
<% end


local function result(query,sort,headers,table) %>
<!DOCTYPE html>
<html lang="en">
	<head>
		<style><% basic_style() %></style>
	</head>
	<body>
		<h1>Lucene Query Results</h1>
			<p><span class="label">Query:</span> <b><%=query%></b></p>
			<p><span class="label">Sort:</span> <b><%=sort%></b></p>
			<table class="results">
				<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>
	</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 of(index)

	return { service = function()
		Io.stdout = Http.response.text_writer()
		local query_string = Http.request.parameters.query
		if query_string == nil then
			form()
			return
		end
		local query = load(query_string,"<query>",{Query=index.Query},true)()
		local rows = to_number(Http.request.parameters.rows)
		local sort = load(Http.request.parameters.sort,"<sort>",{Query=index.Query},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