diff src/luan/modules/lucene/Web_search.luan @ 775:1a68fc55a80c

simplify dir structure
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 26 Aug 2016 14:36:40 -0600
parents lucene/src/luan/modules/lucene/Web_search.luan@ca169567ce07
children bae2d0c2576c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan/modules/lucene/Web_search.luan	Fri Aug 26 14:36:40 2016 -0600
@@ -0,0 +1,189 @@
+ local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local pairs = Luan.pairs or error()
+local ipairs = Luan.ipairs or error()
+local range = Luan.range or error()
+local to_string = Luan.to_string or error()
+local Io = require "luan:Io.luan"
+local repr = Io.repr or error()
+local Http = require "luan:http/Http.luan"
+local String = require "luan:String.luan"
+local string_to_number = String.to_number or error()
+local Html = require "luan:Html.luan"
+
+
+local M = {}
+
+local function style() %>
+			body {
+				font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
+				margin: 2em 5%;
+			}
+			h2 {
+				margin-bottom: .5em;
+			}
+			label {
+				text-align: right;
+				min-width: 6em;
+				display: inline-block;
+				margin-right: .5em;
+			}
+<%
+end
+
+local function form() %>
+<html>
+	<head>
+		<title>Lucene Query</title>
+		<style>
+			<% style() %>
+			input {
+				margin-top: 1em;
+			}
+			input[type="text"] {
+				font: inherit;
+				padding: .5em .8em;
+				border-radius: 8px;
+				border-style: groove;
+			}
+			input[type="text"]:focus {
+				border-color: #66afe9;
+				outline: none;
+			}
+			span[tip] {
+				color: #888;
+				font-size: smaller;
+				margin-left: .5em;
+			}
+			input[type="submit"] {
+				color: white;
+				background: #337ab7;
+				border-color: #337ab7;
+				font: inherit;
+				padding: .5em;
+				border-radius: 4px;
+			}
+			input[type="submit"]:hover {
+				background: #236aa7 !important;
+			}
+		</style>
+	</head>
+	<body>
+		<h2>Lucene Query</h2>
+		<form horizontal name="form0" method="post">
+			<div>
+				<label>Query:</label>
+				<input type=text name="query" size="80" autofocus />
+			</div>
+			<div>
+				<label></label>
+				<span tip>Query examples: <i>type:user</i> or <i>+type:user +name:Joe"</i></span>
+			</div>
+			<div>
+				<label>Max Rows:</label>
+				<input type=text name="rows" value="100" size="3" maxlength="5" />
+			</div>
+			<div>
+				<label>Sort:</label>
+				<input type=text name="sort" size="60" />
+			</div>
+			<div>
+				<label></label>
+				<span tip>Sort examples: <i>name, id</i></span>
+			</div>
+			<div>
+				<label></label>
+				<input type="submit" />
+			</div>
+		</form>
+	</body>
+</html>
+<% end
+
+
+local function result(query,sort,headers,table) %>
+<html>
+	<head>
+		<title>Lucene Query</title>
+		<style>
+			<% style() %>
+			table {
+				border-collapse: collapse;
+				width: 100%;
+				font-size: smaller;
+			}
+			th, td {
+				text-align: left;
+				padding: .5em;
+				border: solid 1px #ddd;
+			}
+		</style>
+	</head>
+	<body>
+		<h2>Lucene Query Results</h2>
+		<p><label>Query:</label> <b><%=Html.encode(to_string(query))%></b></p>
+		<p><label>Sort:</label> <b><%=Html.encode(to_string(sort))%></b></p>
+		<table>
+			<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
+						local val = row[col]
+						%><td><%= val and repr(val) 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 M.of(index)
+	index or error "index is nil"
+
+	return function()
+		Io.stdout = Http.response.text_writer()
+		local query = Http.request.parameter.query
+		if query == nil then
+			form()
+			return
+		end
+		local rows = string_to_number(Http.request.parameter.rows)
+		local sort = Http.request.parameter.sort
+		local results = index.search(query,1,rows,sort)
+		local headers = {}
+		local table = {}
+		for _, doc in ipairs(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
+
+return M