comparison 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
comparison
equal deleted inserted replaced
774:3e30cf310e56 775:1a68fc55a80c
1 local Luan = require "luan:Luan.luan"
2 local error = Luan.error
3 local pairs = Luan.pairs or error()
4 local ipairs = Luan.ipairs or error()
5 local range = Luan.range or error()
6 local to_string = Luan.to_string or error()
7 local Io = require "luan:Io.luan"
8 local repr = Io.repr or error()
9 local Http = require "luan:http/Http.luan"
10 local String = require "luan:String.luan"
11 local string_to_number = String.to_number or error()
12 local Html = require "luan:Html.luan"
13
14
15 local M = {}
16
17 local function style() %>
18 body {
19 font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
20 margin: 2em 5%;
21 }
22 h2 {
23 margin-bottom: .5em;
24 }
25 label {
26 text-align: right;
27 min-width: 6em;
28 display: inline-block;
29 margin-right: .5em;
30 }
31 <%
32 end
33
34 local function form() %>
35 <html>
36 <head>
37 <title>Lucene Query</title>
38 <style>
39 <% style() %>
40 input {
41 margin-top: 1em;
42 }
43 input[type="text"] {
44 font: inherit;
45 padding: .5em .8em;
46 border-radius: 8px;
47 border-style: groove;
48 }
49 input[type="text"]:focus {
50 border-color: #66afe9;
51 outline: none;
52 }
53 span[tip] {
54 color: #888;
55 font-size: smaller;
56 margin-left: .5em;
57 }
58 input[type="submit"] {
59 color: white;
60 background: #337ab7;
61 border-color: #337ab7;
62 font: inherit;
63 padding: .5em;
64 border-radius: 4px;
65 }
66 input[type="submit"]:hover {
67 background: #236aa7 !important;
68 }
69 </style>
70 </head>
71 <body>
72 <h2>Lucene Query</h2>
73 <form horizontal name="form0" method="post">
74 <div>
75 <label>Query:</label>
76 <input type=text name="query" size="80" autofocus />
77 </div>
78 <div>
79 <label></label>
80 <span tip>Query examples: <i>type:user</i> or <i>+type:user +name:Joe"</i></span>
81 </div>
82 <div>
83 <label>Max Rows:</label>
84 <input type=text name="rows" value="100" size="3" maxlength="5" />
85 </div>
86 <div>
87 <label>Sort:</label>
88 <input type=text name="sort" size="60" />
89 </div>
90 <div>
91 <label></label>
92 <span tip>Sort examples: <i>name, id</i></span>
93 </div>
94 <div>
95 <label></label>
96 <input type="submit" />
97 </div>
98 </form>
99 </body>
100 </html>
101 <% end
102
103
104 local function result(query,sort,headers,table) %>
105 <html>
106 <head>
107 <title>Lucene Query</title>
108 <style>
109 <% style() %>
110 table {
111 border-collapse: collapse;
112 width: 100%;
113 font-size: smaller;
114 }
115 th, td {
116 text-align: left;
117 padding: .5em;
118 border: solid 1px #ddd;
119 }
120 </style>
121 </head>
122 <body>
123 <h2>Lucene Query Results</h2>
124 <p><label>Query:</label> <b><%=Html.encode(to_string(query))%></b></p>
125 <p><label>Sort:</label> <b><%=Html.encode(to_string(sort))%></b></p>
126 <table>
127 <tr>
128 <th></th>
129 <% for _, header in ipairs(headers) do %>
130 <th><%=header%></th>
131 <% end %>
132 </tr>
133 <% for i, row in ipairs(table) do %>
134 <tr>
135 <td><%=i%></td>
136 <%
137 for col in range(1, #headers) do
138 local val = row[col]
139 %><td><%= val and repr(val) or "" %></td><%
140 end
141 %>
142 </tr>
143 <% end %>
144 </table>
145 </body>
146 </html>
147 <% end
148
149
150 local function index_of(tbl,val)
151 for i, v in ipairs(tbl) do
152 if v == val then
153 return i
154 end
155 end
156 local n = #tbl + 1
157 tbl[n] = val
158 return n
159 end
160
161
162 function M.of(index)
163 index or error "index is nil"
164
165 return function()
166 Io.stdout = Http.response.text_writer()
167 local query = Http.request.parameter.query
168 if query == nil then
169 form()
170 return
171 end
172 local rows = string_to_number(Http.request.parameter.rows)
173 local sort = Http.request.parameter.sort
174 local results = index.search(query,1,rows,sort)
175 local headers = {}
176 local table = {}
177 for _, doc in ipairs(results) do
178 local row = {}
179 for field, value in pairs(doc) do
180 row[index_of(headers,field)] = value
181 end
182 table[#table+1] = row
183 end
184 result(query,sort,headers,table)
185 end
186
187 end
188
189 return M