comparison lucene/src/luan/modules/lucene/Web_search.luan @ 285:582e8db4cdb6

add lucene Web_search git-svn-id: https://luan-java.googlecode.com/svn/trunk@286 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 02 Dec 2014 06:17:45 +0000
parents
children 91be4027b2a8
comparison
equal deleted inserted replaced
284:8870840251ea 285:582e8db4cdb6
1 import "luan:Io"
2 import "luan:web/Http"
3 import "luan:String"
4
5
6 local function form() %>
7 <html>
8 <body>
9 <form method="post">
10 <p>Query: <input name="query" size="60" /></p>
11 <p>Rows: <input name="rows" value="20" /></p>
12 <p>Sort: <input name="sort" size="60" /></p>
13 <p><input type="submit" /></p>
14 </form>
15 </body>
16 </html>
17 <% end
18
19
20 local function result(query,sort,headers,table) %>
21 <html>
22 <body>
23 <p>Query: <b><%=repr(query)%></b></p>
24 <p>Sort: <b><%=repr(sort)%></b></p>
25 <table border="1">
26 <tr><th></th>
27 <%
28 for _, header in ipairs(headers) do
29 %><th><%=header%></th><%
30 end
31 %>
32 </tr>
33 <%
34 for i, row in ipairs(table) do
35 %>
36 <tr><td><%=i%></td>
37 <%
38 for col in range(1,#headers) do
39 %><td><%= row[col] or "" %></td><%
40 end
41 %></tr><%
42 end
43 %>
44 </body>
45 </html>
46 <% end
47
48
49 local function index_of(tbl,val)
50 for i, v in ipairs(tbl) do
51 if v == val then
52 return i
53 end
54 end
55 local n = #tbl + 1
56 tbl[n] = val
57 return n
58 end
59
60
61 function of(index)
62
63 return { service = function()
64 Io.stdout = Http.response.text_writer()
65 local query_string = Http.request.parameters.query
66 if query_string == nil then
67 form()
68 return
69 end
70 local query = load(query_string,"<query>",{},true)()
71 local rows = to_number(Http.request.parameters.rows)
72 local sort = load(Http.request.parameters.sort,"<sort>",{},true)()
73 index.Searcher( function(searcher)
74 local results, length, total_hits = searcher.search(query,rows,sort)
75 local headers = {}
76 local table = {}
77 for doc in results do
78 local row = {}
79 for field, value in pairs(doc) do
80 row[index_of(headers,field)] = value
81 end
82 table[#table+1] = row
83 end
84 result(query,sort,headers,table)
85 end )
86 end }
87
88 end