Newer
Older
TalkjaSolr / web / WEB-INF / jsp / search.jsp
@haya4 haya4 on 22 Apr 2022 8 KB restore
  1. <%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
  2. <%@ page session="false"%>
  3. <%@page import="org.apache.solr.client.solrj.response.QueryResponse"%>
  4. <%@page import="org.apache.solr.client.solrj.response.FacetField"%>
  5. <%@page import="org.apache.solr.client.solrj.response.FacetField.Count"%>
  6. <%@page import="org.apache.solr.common.util.NamedList"%>
  7. <%@page import="org.apache.solr.common.SolrDocument"%>
  8. <%@page import="org.apache.commons.lang.StringUtils" %>
  9. <%@page import="org.apache.commons.lang.StringEscapeUtils" %>
  10. <%@page import="java.util.List"%>
  11. <%@page import="java.util.Map"%>
  12.  
  13. <%@page import="solrbook.client.util.SearchUtil"%>
  14. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  15.  
  16. <html xmlns="http://www.w3.org/1999/xhtml">
  17. <head>
  18. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  19. <link media="screen" href="./style.css" type="text/css" rel="stylesheet" />
  20. <%
  21. QueryResponse rsp = (QueryResponse)request.getAttribute("rsp");
  22. String query = (String)request.getAttribute("query");
  23. String startStr = (String)request.getAttribute("start");
  24. String[] fq = (String[])request.getAttribute("fq");
  25. if(query == null){
  26. query ="";
  27. }
  28. long found = 0;
  29. long start = 0;
  30. int rows = 10;
  31. int qtime = 0;
  32. if(rsp != null){
  33. qtime = rsp.getQTime();
  34.  
  35. //start = rsp.getResults().getStart();
  36. if (startStr != null) {
  37. try {
  38. start = Long.parseLong(startStr);
  39. }
  40. catch (NumberFormatException nfe) {
  41. start = 0;
  42. }
  43. }
  44.  
  45. String rowsStr = (String)((NamedList)rsp.getResponseHeader().get("params")).get("rows");
  46. try {
  47. rows = Integer.parseInt(rowsStr);
  48. }
  49. catch (NumberFormatException nfe) {}
  50. found = rsp.getResults().getNumFound();
  51. }
  52.  
  53. if (!StringUtils.isEmpty(query)) {
  54. %>
  55. <title><%= StringEscapeUtils.escapeHtml(query) %>の検索結果</title>
  56. <%
  57. }
  58. else {
  59. %>
  60. <title>OSM Talk-ja 検索</title>
  61. <%
  62. }
  63. %>
  64.  
  65. </head>
  66. <body onload="document.f1.query.focus();">
  67. <div id="wrap">
  68. <div id="header">
  69. <h1><a href="#">OSM Talk-ja 検索</a></h1>
  70. <br/>
  71. <form name="f1" action="index.html">
  72. <input type="text" name="query" value="<%= StringEscapeUtils.escapeHtml(query) %>"/>
  73. <input value="検索" type="submit"/>
  74. </form>
  75. </div>
  76.  
  77. <!-- %@include file="_result_header.jsp" % -->
  78. <div id="result_header">
  79. <%
  80. if (!StringUtils.isEmpty(query)) {
  81. %>
  82. <b><%= StringEscapeUtils.escapeHtml(query) %></b> の検索結果&nbsp;&nbsp;&nbsp;
  83. <%
  84. }
  85. if (found > 0) {
  86. %>
  87. <b><%= found %></b> 件中
  88. <b><%= start+1 %></b> -
  89. <b><%= start+rows > found ? found : start+rows %></b> 件目
  90. <%
  91. }
  92. else {
  93. %>
  94. <b>0</b>
  95. <%
  96. }
  97. %>
  98. ( <%= qtime/1000 %> 秒 )
  99. </div>
  100.  
  101. <div id="content">
  102. <%
  103. if (rsp != null) {
  104. if (found == 0) {
  105. %>
  106. <b><%= StringEscapeUtils.escapeHtml(query) %></b> は見つかりませんでした。他の検索語で試してください。
  107. <%
  108. }
  109. else {
  110. %>
  111. <div id="result" class="right">
  112. <%
  113. for (SolrDocument document : rsp.getResults()) {
  114. String info = "";
  115. if(document.getFirstValue("id") != null){
  116. info += document.getFirstValue("id");
  117. }
  118. if(document.getFirstValue("date") != null){
  119. if(info.length() > 0){
  120. info += " | ";
  121. }
  122. info += document.getFirstValue("date");
  123. }
  124. if(document.getFirstValue("name") != null){
  125. if(info.length() > 0){
  126. info += " | ";
  127. }
  128. info += document.getFirstValue("name");
  129. }
  130.  
  131. String titleStr = "";
  132. if (document.getFirstValue("title") != null) {
  133. titleStr = (String)document.getFirstValue("title");
  134. }
  135. %>
  136. <h2><a href="<%= document.getFieldValue("url") %>"><img src="IconTarget.png"/><%= StringEscapeUtils.escapeHtml(titleStr) %></a></h2>
  137. <h3><%= info %></h3>
  138.  
  139. <%
  140. if(!StringUtils.isEmpty(query)){
  141. String contents = "";
  142. boolean first = true;
  143. if (document.getFieldValues("contents") != null){
  144. int i = 10;
  145. for (Object line : document.getFieldValues("contents")) {
  146. i--;
  147. if (i < 0) {
  148. break;
  149. }
  150. String str = line.toString();
  151. if ((str != null) && (str.trim().length() > 0)) {
  152. if (!first) {
  153. contents += " / ";
  154. }
  155. else {
  156. first = false;
  157. }
  158. contents += line.toString();
  159. }
  160. }
  161. }
  162. %>
  163. <div class="articles"><i><%= StringEscapeUtils.escapeHtml(contents) %></i></div>
  164. <%
  165. }
  166. %>
  167.  
  168. <%
  169. String summary = "";
  170. Map<String, List<String>> highlighting = rsp.getHighlighting().get(document.getFirstValue("url"));
  171. boolean first = true;
  172. for (String field : SearchUtil.getSummaryHighlightFields()){
  173. if (highlighting.get(field) != null){
  174. for (String text : highlighting.get(field)){
  175. if (!first) {
  176. summary += "...";
  177. }
  178. else {
  179. first = false;
  180. }
  181. summary += text;
  182. }
  183. }
  184. }
  185. %>
  186. <div class="articles"><%= summary %></div>
  187. <br/>
  188. <hr/>
  189. <%
  190. }
  191. }
  192. %>
  193. </div><!-- end of #result -->
  194.  
  195. <div id="facet" class="left">
  196. <%-- facet fields --%>
  197. <%
  198. //for (FacetField facetField : rsp.getFacetFields()) {
  199. %>
  200. <!-- %@include file="_facet_field.jsp"% -->
  201. <%
  202. //}
  203. %>
  204.  
  205. <%-- facet queries --%>
  206. <!-- % for(String queryLabelName : SearchUtil.getFacetQueries().keySet()){ % -->
  207. <!-- %@include file="_facet_queries.jsp"% -->
  208. <!-- % } % -->
  209. </div><!-- end of #facet -->
  210. <%
  211. }
  212. %>
  213.  
  214. </div><!-- end of #content -->
  215. <div style="CLEAR: both"></div>
  216.  
  217.  
  218. <div id="page">
  219. <%
  220. String p_head = "|&lt;&lt;先頭ページへ";
  221. String p_prev = "&lt;前へ";
  222. String p_next = "次へ&gt;";
  223. String p_tail = "末尾ページへ&gt;&gt;|";
  224.  
  225. int wsiz = 10;
  226. int w1 = 5;
  227. int w2 = 5;
  228.  
  229. int pcnt = (int)(found / rows + ( ( found % rows ) == 0 ? 0 : 1 ));
  230. int cpag = (int)(start / rows + 1);
  231. int wbgn = cpag - w1;
  232. int wend = cpag + w2;
  233. if( wbgn < 1 ){
  234. wbgn = 1;
  235. wend = wbgn + wsiz;
  236. if( wend > pcnt + 1 ){
  237. wend = pcnt + 1;
  238. }
  239. }
  240. if( wend > pcnt + 1 ){
  241. wend = pcnt + 1;
  242. wbgn = wend - wsiz;
  243. if( wbgn < 1 ){
  244. wbgn = 1;
  245. }
  246. }
  247. %>
  248. <%
  249. if( pcnt > 1 ) {
  250. %>
  251. <strong>Page</strong>
  252. <%
  253. if( cpag > 1 ) {
  254. %>
  255. <a href="./index.html<%=SearchUtil.getLinkStr(query,fq, null, 0)%>"><%=p_head%></a>&nbsp;&nbsp;&nbsp;
  256. <a href="./index.html<%=SearchUtil.getLinkStr(query,fq, null, ((cpag-2)*rows))%>"><%=p_prev%></a>&nbsp;&nbsp;&nbsp;
  257. <%
  258. }
  259. %>
  260. &nbsp;
  261. <%
  262. for(int i=wbgn;i<wend;i++) {
  263. %>
  264. <%
  265. if(cpag == i) {
  266. %>
  267. <%= i %>&nbsp;&nbsp;&nbsp;
  268. <%
  269. }
  270. else {
  271. %>
  272. <a href="./index.html<%=SearchUtil.getLinkStr(query,fq, null, ((i-1)*rows))%>"><%=i %></a>&nbsp;&nbsp;&nbsp;
  273. <%
  274. }
  275. }
  276. %>
  277. &nbsp;
  278. <%
  279. if ( cpag < pcnt ) {
  280. %>
  281. <a href="./index.html<%=SearchUtil.getLinkStr(query,fq, null, (cpag*rows))%>"><%=p_next%></a>&nbsp;&nbsp;&nbsp;
  282. <a href="./index.html<%=SearchUtil.getLinkStr(query,fq, null, ((pcnt-1)*rows))%>"><%=p_tail%></a>&nbsp;&nbsp;&nbsp;
  283. <%
  284. }
  285. }
  286. %>
  287. </div>
  288.  
  289. <div id="footer">powered by Apache Solr4.</div>
  290.  
  291. </div><!-- end of #wrap -->
  292. </body>
  293. </html>