diff src/luan/modules/lucene/Web_search.luan @ 1395:9dfff82dfc59

finish postgres work
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 09 Sep 2019 01:22:23 -0600
parents b84f60ebe196
children 1979cff9aad2
line wrap: on
line diff
--- a/src/luan/modules/lucene/Web_search.luan	Sun Sep 08 22:13:08 2019 -0600
+++ b/src/luan/modules/lucene/Web_search.luan	Mon Sep 09 01:22:23 2019 -0600
@@ -5,18 +5,22 @@
 local range = Luan.range or error()
 local to_string = Luan.to_string or error()
 local stringify = Luan.stringify or error()
+local eval = Luan.eval or error()
 local Io = require "luan:Io.luan"
 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 html_encode = Html.encode or error()
+local Number = require "luan:Number.luan"
 
 
 local Web_search = {}
 
-local function style() %>
+local function style()
+%>
 			body {
-				font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
+				font-family: sans-serif;
 				margin: 2em 5%;
 			}
 			h2 {
@@ -28,53 +32,38 @@
 				display: inline-block;
 				margin-right: .5em;
 			}
-<%
-end
-
-local function form() %>
-<!doctype html>
-<html>
-	<head>
-		<title>Lucene Query</title>
-		<style>
-			<% style() %>
-			input {
+			input, textarea {
 				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;
+				cursor: pointer;
 				padding: .5em;
 				border-radius: 4px;
 			}
-			input[type="submit"]:hover {
-				background: #236aa7 !important;
-			}
+<%
+end
+
+local function form()
+%>
+<!doctype html>
+<html>
+	<head>
+		<title>Lucene</title>
+		<style>
+<%			style() %>
 		</style>
 	</head>
 	<body>
 		<h2>Lucene Query</h2>
-		<form horizontal method="post">
+		<form>
 			<div>
 				<label>Query:</label>
-				<input type=text name="query" size="80" autofocus />
+				<input type=text name="query" size="80" autofocus>
 			</div>
 			<div>
 				<label></label>
@@ -82,11 +71,11 @@
 			</div>
 			<div>
 				<label>Max Rows:</label>
-				<input type=text name="rows" value="100" size="3" maxlength="5" />
+				<input type=text name="rows" value="100" size="3" maxlength="5">
 			</div>
 			<div>
 				<label>Sort:</label>
-				<input type=text name="sort" size="60" />
+				<input type=text name="sort" size="60">
 			</div>
 			<div>
 				<label></label>
@@ -94,21 +83,49 @@
 			</div>
 			<div>
 				<label></label>
-				<input type="submit" />
+				<input type="submit">
 			</div>
 		</form>
 	</body>
 </html>
-<% end
+<%
+end
 
 
-local function result(query,sort,headers,table) %>
+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
+
+local function result(index)
+	local query = Http.request.parameters.query
+	local rows = string_to_number(Http.request.parameters.rows)
+	local sort = Http.request.parameters.sort
+	local results = index.search(query,1,rows,{sort=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
+		row.doc = doc
+		table[#table+1] = row
+	end
+	local can_edit = index.completer ~= nil
+%>
 <!doctype html>
 <html>
 	<head>
-		<title>Lucene Query</title>
+		<title>Lucene</title>
 		<style>
-			<% style() %>
+<%			style() %>
 			table {
 				border-collapse: collapse;
 				width: 100%;
@@ -122,42 +139,88 @@
 		</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>
+		<h2>Lucene 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 %>
+<%				for _, header in ipairs(headers) do %>
 					<th><%=header%></th>
-				<% end %>
+<%				end %>
 			</tr>
-			<% for i, row in ipairs(table) do %>
+<%
+			for i, row in ipairs(table) do
+				local id = row.doc.id
+%>
 				<tr>
-					<td><%=i%></td>
-					<%
+					<td>
+<%					if can_edit and id~=nil then %>
+						<a href="?id=<%=id%>"><%=i%></a>
+<%					else %>
+						<%=i%>
+<%					end %>
+					</td>
+<%
 					for col in range(1, #headers) do
 						local val = row[col]
 						%><td><%= val and stringify(val) or "" %></td><%
 					end
-					%>
+%>
 				</tr>
-			<% end %>
+<%			end %>
 		</table>
 	</body>
 </html>
-<% end
+<%
+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
+local function edit(index)
+	local id = string_to_number(Http.request.parameters.id)
+	local doc = index.get_document("id:"..id)
+	doc = stringify(doc,{strict=true,number_types=true})
+%>
+<!doctype html>
+<html>
+	<head>
+		<title>Lucene</title>
+		<style>
+<%			style() %>
+		</style>
+	</head>
+	<body>
+		<h2>Lucene Edit</h2>
+		<form action="?" method=post>
+			<div><textarea name="doc" rows="20" cols="90" autofocus><%=html_encode(doc)%></textarea></div>
+			<div><input type="submit" value="Update"></div>
+		</form>
+	</body>
+</html>
+<%
+end
+
+
+local function update(index)
+	local doc = Http.request.parameters.doc
+	local completer = index.completer or error()
+	doc = eval( doc, "lucene", Number )
+	doc = completer(doc)
+	index.save(doc)
+%>
+<!doctype html>
+<html>
+	<head>
+		<title>Lucene</title>
+		<style>
+<%			style() %>
+		</style>
+	</head>
+	<body>
+		<h2>Lucene Updated</h2>
+	</body>
+</html>
+<%
 end
 
 
@@ -167,23 +230,15 @@
 	return function()
 		Io.stdout = Http.response.text_writer()
 		local query = Http.request.parameters.query
-		if query == nil then
+		if Http.request.parameters.query ~= nil then
+			result(index)
+		elseif Http.request.parameters.id ~= nil then
+			edit(index)
+		elseif Http.request.parameters.doc ~= nil then
+			update(index)
+		else
 			form()
-			return
 		end
-		local rows = string_to_number(Http.request.parameters.rows)
-		local sort = Http.request.parameters.sort
-		local results = index.search(query,1,rows,{sort=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