Newer
Older
TalkjaSolr / web / WEB-INF / jsp / search.jsp
@haya4 haya4 on 17 Mar 2019 6 KB 1st
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page session="false"%>
<%@page import="org.apache.solr.client.solrj.response.QueryResponse"%>
<%@page import="org.apache.solr.client.solrj.response.FacetField"%>
<%@page import="org.apache.solr.client.solrj.response.FacetField.Count"%>
<%@page import="org.apache.solr.common.util.NamedList"%>
<%@page import="org.apache.solr.common.SolrDocument"%>
<%@page import="org.apache.commons.lang.StringUtils" %>
<%@page import="org.apache.commons.lang.StringEscapeUtils" %>
<%@page import="java.util.List"%>
<%@page import="java.util.Map"%>

<%@page import="solrbook.client.util.SearchUtil"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link media="screen" href="./style.css" type="text/css" rel="stylesheet" />
<%
QueryResponse rsp = (QueryResponse)request.getAttribute("rsp");
String query = (String)request.getAttribute("query");
String startStr = (String)request.getAttribute("start");
String[] fq = (String[])request.getAttribute("fq");
if(query == null){
  query ="";
}
long found = 0;
long start = 0;
int rows = 10;
int qtime = 0;
if(rsp != null){
    qtime = rsp.getQTime();

    //start = rsp.getResults().getStart();
    if (startStr != null) {
        try {
            start = Long.parseLong(startStr);
        }
        catch (NumberFormatException nfe) {
            start = 0;
        }
    }

    String rowsStr = (String)((NamedList)rsp.getResponseHeader().get("params")).get("rows");
    try {
        rows = Integer.parseInt(rowsStr);
    }
    catch (NumberFormatException nfe) {}
    found = rsp.getResults().getNumFound();
}

if (!StringUtils.isEmpty(query)) {
%>
<title><%= StringEscapeUtils.escapeHtml(query) %>の検索結果</title>
<%
}
else {
%>
<title>OSM Talk-ja 検索</title>
<%
}
%>

</head>
<body onload="document.f1.query.focus();">
<div id="wrap">
    <div id="header">
        <h1><a href="#">OSM Talk-ja 検索</a></h1>
        <br/>
        <form name="f1" action="index.html">
            <input type="text" name="query" value="<%= StringEscapeUtils.escapeHtml(query) %>"/>
            <input value="検索" type="submit"/>
        </form>
    </div>

    <!-- %@include file="_result_header.jsp" % -->
    <div id="result_header">
<%
if (!StringUtils.isEmpty(query)) {
%>
        <b><%= StringEscapeUtils.escapeHtml(query) %></b> の検索結果&nbsp;&nbsp;&nbsp;
<%
}
if (found > 0) {
%>
        <b><%= found %></b> 件中
        <b><%= start+1 %></b> -
        <b><%= start+rows > found ? found : start+rows %></b> 件目
<%
}
else {
%>
        <b>0</b> 件
<%
}
%>
        ( <%= qtime/1000 %> 秒 )
    </div>

    <div id="content">
<%
if (rsp != null) {
    if (found == 0) { 
%>
        <b><%= StringEscapeUtils.escapeHtml(query) %></b> は見つかりませんでした。他の検索語で試してください。
<%
    }
    else {
%>
        <div id="result" class="right">
<%
        for (SolrDocument document : rsp.getResults()) {
            String info = "";
            if(document.getFirstValue("id") != null){
                info += document.getFirstValue("id");
            }
            if(document.getFirstValue("date") != null){
                if(info.length() > 0){
                    info += " | ";
                }
                info += document.getFirstValue("date");
            }
            if(document.getFirstValue("name") != null){
                if(info.length() > 0){
                    info += " | ";
                }
                info += document.getFirstValue("name");
            }

            String titleStr = "";
            if (document.getFirstValue("title") != null) {
                titleStr = (String)document.getFirstValue("title");
            }
%>
            <h2><a href="<%= document.getFieldValue("url") %>"><img src="IconTarget.png"/><%= StringEscapeUtils.escapeHtml(titleStr) %></a></h2>
            <h3><%= info %></h3>

<%
            if(!StringUtils.isEmpty(query)){
                String contents = "";
                boolean first = true;
                if (document.getFieldValues("contents") != null){
                    int i = 10;
                    for (Object line : document.getFieldValues("contents")) {
                        i--;
                        if (i < 0) {
                            break;
                        }
                        String str = line.toString();
                        if ((str != null) && (str.trim().length() > 0)) {
                            if (!first) {
                                contents += " / ";
                            }
                            else {
                                first = false;
                            }
                            contents += line.toString();
                        }
                    }
                }
%>
            <div class="articles"><i><%= StringEscapeUtils.escapeHtml(contents) %></i></div>
<%
            }
%>

<%
            String summary = "";
            Map<String, List<String>> highlighting = rsp.getHighlighting().get(document.getFirstValue("url"));
            boolean first = true;
            for (String field : SearchUtil.getSummaryHighlightFields()){
                if (highlighting.get(field) != null){
                    for (String text : highlighting.get(field)){
                        if (!first) {
                            summary += "...";
                        }
                        else {
                            first = false;
                        }
                        summary += text;
                    }
                }
            }
%>
            <div class="articles"><%= summary %></div>
            <br/>
            <hr/>
<%
        }
    }
%>
        </div><!-- end of #result -->

        <div id="facet" class="left">
        <%-- facet fields --%>
<%
    //for (FacetField facetField : rsp.getFacetFields()) {
%>
        <!-- %@include file="_facet_field.jsp"% -->
<% 
    //}
%>

        <%-- facet queries --%>
        <!-- % for(String queryLabelName : SearchUtil.getFacetQueries().keySet()){ % -->
        <!-- %@include file="_facet_queries.jsp"% -->
        <!-- % } % -->
        </div><!-- end of #facet -->
<%
}
%>

        <div style="CLEAR: both"></div>

    </div><!-- end of #content -->

    <%@include file="_pagination.jsp" %>

    <div id="footer">powered by Apache Solr4.</div>
</div>
</body>
</html>