comparison 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
comparison
equal deleted inserted replaced
1394:8fe777ba5045 1395:9dfff82dfc59
3 local pairs = Luan.pairs or error() 3 local pairs = Luan.pairs or error()
4 local ipairs = Luan.ipairs or error() 4 local ipairs = Luan.ipairs or error()
5 local range = Luan.range or error() 5 local range = Luan.range or error()
6 local to_string = Luan.to_string or error() 6 local to_string = Luan.to_string or error()
7 local stringify = Luan.stringify or error() 7 local stringify = Luan.stringify or error()
8 local eval = Luan.eval or error()
8 local Io = require "luan:Io.luan" 9 local Io = require "luan:Io.luan"
9 local Http = require "luan:http/Http.luan" 10 local Http = require "luan:http/Http.luan"
10 local String = require "luan:String.luan" 11 local String = require "luan:String.luan"
11 local string_to_number = String.to_number or error() 12 local string_to_number = String.to_number or error()
12 local Html = require "luan:Html.luan" 13 local Html = require "luan:Html.luan"
14 local html_encode = Html.encode or error()
15 local Number = require "luan:Number.luan"
13 16
14 17
15 local Web_search = {} 18 local Web_search = {}
16 19
17 local function style() %> 20 local function style()
21 %>
18 body { 22 body {
19 font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 23 font-family: sans-serif;
20 margin: 2em 5%; 24 margin: 2em 5%;
21 } 25 }
22 h2 { 26 h2 {
23 margin-bottom: .5em; 27 margin-bottom: .5em;
24 } 28 }
26 text-align: right; 30 text-align: right;
27 min-width: 6em; 31 min-width: 6em;
28 display: inline-block; 32 display: inline-block;
29 margin-right: .5em; 33 margin-right: .5em;
30 } 34 }
31 <% 35 input, textarea {
32 end
33
34 local function form() %>
35 <!doctype html>
36 <html>
37 <head>
38 <title>Lucene Query</title>
39 <style>
40 <% style() %>
41 input {
42 margin-top: 1em; 36 margin-top: 1em;
43 }
44 input[type="text"] {
45 font: inherit; 37 font: inherit;
46 padding: .5em .8em;
47 border-radius: 8px;
48 border-style: groove;
49 }
50 input[type="text"]:focus {
51 border-color: #66afe9;
52 outline: none;
53 } 38 }
54 span[tip] { 39 span[tip] {
55 color: #888; 40 color: #888;
56 font-size: smaller; 41 font-size: smaller;
57 margin-left: .5em;
58 } 42 }
59 input[type="submit"] { 43 input[type="submit"] {
60 color: white; 44 cursor: pointer;
61 background: #337ab7;
62 border-color: #337ab7;
63 font: inherit;
64 padding: .5em; 45 padding: .5em;
65 border-radius: 4px; 46 border-radius: 4px;
66 } 47 }
67 input[type="submit"]:hover { 48 <%
68 background: #236aa7 !important; 49 end
69 } 50
51 local function form()
52 %>
53 <!doctype html>
54 <html>
55 <head>
56 <title>Lucene</title>
57 <style>
58 <% style() %>
70 </style> 59 </style>
71 </head> 60 </head>
72 <body> 61 <body>
73 <h2>Lucene Query</h2> 62 <h2>Lucene Query</h2>
74 <form horizontal method="post"> 63 <form>
75 <div> 64 <div>
76 <label>Query:</label> 65 <label>Query:</label>
77 <input type=text name="query" size="80" autofocus /> 66 <input type=text name="query" size="80" autofocus>
78 </div> 67 </div>
79 <div> 68 <div>
80 <label></label> 69 <label></label>
81 <span tip>Query examples: <i>type:user</i> or <i>+type:user +name:Joe"</i></span> 70 <span tip>Query examples: <i>type:user</i> or <i>+type:user +name:Joe"</i></span>
82 </div> 71 </div>
83 <div> 72 <div>
84 <label>Max Rows:</label> 73 <label>Max Rows:</label>
85 <input type=text name="rows" value="100" size="3" maxlength="5" /> 74 <input type=text name="rows" value="100" size="3" maxlength="5">
86 </div> 75 </div>
87 <div> 76 <div>
88 <label>Sort:</label> 77 <label>Sort:</label>
89 <input type=text name="sort" size="60" /> 78 <input type=text name="sort" size="60">
90 </div> 79 </div>
91 <div> 80 <div>
92 <label></label> 81 <label></label>
93 <span tip>Sort examples: <i>name, id</i></span> 82 <span tip>Sort examples: <i>name, id</i></span>
94 </div> 83 </div>
95 <div> 84 <div>
96 <label></label> 85 <label></label>
97 <input type="submit" /> 86 <input type="submit">
98 </div> 87 </div>
99 </form> 88 </form>
100 </body> 89 </body>
101 </html> 90 </html>
102 <% end 91 <%
103 92 end
104
105 local function result(query,sort,headers,table) %>
106 <!doctype html>
107 <html>
108 <head>
109 <title>Lucene Query</title>
110 <style>
111 <% style() %>
112 table {
113 border-collapse: collapse;
114 width: 100%;
115 font-size: smaller;
116 }
117 th, td {
118 text-align: left;
119 padding: .5em;
120 border: solid 1px #ddd;
121 }
122 </style>
123 </head>
124 <body>
125 <h2>Lucene Query Results</h2>
126 <p><label>Query:</label> <b><%=Html.encode(to_string(query))%></b></p>
127 <p><label>Sort:</label> <b><%=Html.encode(to_string(sort))%></b></p>
128 <table>
129 <tr>
130 <th></th>
131 <% for _, header in ipairs(headers) do %>
132 <th><%=header%></th>
133 <% end %>
134 </tr>
135 <% for i, row in ipairs(table) do %>
136 <tr>
137 <td><%=i%></td>
138 <%
139 for col in range(1, #headers) do
140 local val = row[col]
141 %><td><%= val and stringify(val) or "" %></td><%
142 end
143 %>
144 </tr>
145 <% end %>
146 </table>
147 </body>
148 </html>
149 <% end
150 93
151 94
152 local function index_of(tbl,val) 95 local function index_of(tbl,val)
153 for i, v in ipairs(tbl) do 96 for i, v in ipairs(tbl) do
154 if v == val then 97 if v == val then
158 local n = #tbl + 1 101 local n = #tbl + 1
159 tbl[n] = val 102 tbl[n] = val
160 return n 103 return n
161 end 104 end
162 105
106 local function result(index)
107 local query = Http.request.parameters.query
108 local rows = string_to_number(Http.request.parameters.rows)
109 local sort = Http.request.parameters.sort
110 local results = index.search(query,1,rows,{sort=sort})
111 local headers = {}
112 local table = {}
113 for _, doc in ipairs(results) do
114 local row = {}
115 for field, value in pairs(doc) do
116 row[index_of(headers,field)] = value
117 end
118 row.doc = doc
119 table[#table+1] = row
120 end
121 local can_edit = index.completer ~= nil
122 %>
123 <!doctype html>
124 <html>
125 <head>
126 <title>Lucene</title>
127 <style>
128 <% style() %>
129 table {
130 border-collapse: collapse;
131 width: 100%;
132 font-size: smaller;
133 }
134 th, td {
135 text-align: left;
136 padding: .5em;
137 border: solid 1px #ddd;
138 }
139 </style>
140 </head>
141 <body>
142 <h2>Lucene Results</h2>
143 <p><label>Query:</label> <b><%=html_encode(to_string(query))%></b></p>
144 <p><label>Sort:</label> <b><%=html_encode(to_string(sort))%></b></p>
145 <table>
146 <tr>
147 <th></th>
148 <% for _, header in ipairs(headers) do %>
149 <th><%=header%></th>
150 <% end %>
151 </tr>
152 <%
153 for i, row in ipairs(table) do
154 local id = row.doc.id
155 %>
156 <tr>
157 <td>
158 <% if can_edit and id~=nil then %>
159 <a href="?id=<%=id%>"><%=i%></a>
160 <% else %>
161 <%=i%>
162 <% end %>
163 </td>
164 <%
165 for col in range(1, #headers) do
166 local val = row[col]
167 %><td><%= val and stringify(val) or "" %></td><%
168 end
169 %>
170 </tr>
171 <% end %>
172 </table>
173 </body>
174 </html>
175 <%
176 end
177
178
179 local function edit(index)
180 local id = string_to_number(Http.request.parameters.id)
181 local doc = index.get_document("id:"..id)
182 doc = stringify(doc,{strict=true,number_types=true})
183 %>
184 <!doctype html>
185 <html>
186 <head>
187 <title>Lucene</title>
188 <style>
189 <% style() %>
190 </style>
191 </head>
192 <body>
193 <h2>Lucene Edit</h2>
194 <form action="?" method=post>
195 <div><textarea name="doc" rows="20" cols="90" autofocus><%=html_encode(doc)%></textarea></div>
196 <div><input type="submit" value="Update"></div>
197 </form>
198 </body>
199 </html>
200 <%
201 end
202
203
204 local function update(index)
205 local doc = Http.request.parameters.doc
206 local completer = index.completer or error()
207 doc = eval( doc, "lucene", Number )
208 doc = completer(doc)
209 index.save(doc)
210 %>
211 <!doctype html>
212 <html>
213 <head>
214 <title>Lucene</title>
215 <style>
216 <% style() %>
217 </style>
218 </head>
219 <body>
220 <h2>Lucene Updated</h2>
221 </body>
222 </html>
223 <%
224 end
225
163 226
164 function Web_search.of(index) 227 function Web_search.of(index)
165 index or error "index is nil" 228 index or error "index is nil"
166 229
167 return function() 230 return function()
168 Io.stdout = Http.response.text_writer() 231 Io.stdout = Http.response.text_writer()
169 local query = Http.request.parameters.query 232 local query = Http.request.parameters.query
170 if query == nil then 233 if Http.request.parameters.query ~= nil then
234 result(index)
235 elseif Http.request.parameters.id ~= nil then
236 edit(index)
237 elseif Http.request.parameters.doc ~= nil then
238 update(index)
239 else
171 form() 240 form()
172 return
173 end 241 end
174 local rows = string_to_number(Http.request.parameters.rows)
175 local sort = Http.request.parameters.sort
176 local results = index.search(query,1,rows,{sort=sort})
177 local headers = {}
178 local table = {}
179 for _, doc in ipairs(results) do
180 local row = {}
181 for field, value in pairs(doc) do
182 row[index_of(headers,field)] = value
183 end
184 table[#table+1] = row
185 end
186 result(query,sort,headers,table)
187 end 242 end
188 243
189 end 244 end
190 245
191 return Web_search 246 return Web_search