/** * */ package solrbook.client.servlet; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.RequestDispatcher; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrServer; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.params.FacetParams; import org.apache.solr.common.params.HighlightParams; import solrbook.client.util.SearchUtil; /** * @author $Author$ * */ public class SearchServlet extends HttpServlet { private SolrServer solrServer = null; /* * (非 Javadoc) * * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig) */ @Override public void init(ServletConfig config) throws ServletException { super.init(config); solrServer = new HttpSolrServer("http://172.17.0.1:8983/solr/talkja"); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doService(request, response); } @SuppressWarnings("CallToPrintStackTrace") protected void doService( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 検索条件生成 SolrQuery query = createSolrQuery(request); // 結果受け取り QueryResponse rsp = null; try { rsp = solrServer.query(query); } catch (SolrServerException sse) { sse.printStackTrace(); } request.setAttribute("rsp", rsp); RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/jsp/search.jsp"); rd.forward(request, response); } private String getQueryString(HttpServletRequest request) { String vStr = request.getParameter("query"); String queryStr = null; if (vStr != null) { try { queryStr = new String(vStr.getBytes("8859_1")); } catch (UnsupportedEncodingException ex) { Logger.getLogger(SearchServlet.class.getName()).log(Level.SEVERE, null, ex); } } request.setAttribute("query", queryStr); if (StringUtils.isEmpty(queryStr)) { queryStr = "*:*"; } return queryStr; } private String getPerFieldParameterName(String field, String paramName) { return "f." + field + "." + paramName; } private SolrQuery createSolrQuery(HttpServletRequest request) { SolrQuery query = new SolrQuery(); query.setRows(10); // fl(取得フィールドの指定) //query.setFields(SearchUtil.getFields()); // ハイライト query.setHighlight(true); query.addHighlightField(SearchUtil.getTitleHighlightField()); for (String field : SearchUtil.getSummaryHighlightFields()) { query.addHighlightField(field); } query.setHighlightSnippets(1); query.setHighlightSimplePre("<b style=\"background:aquamarine\">"); query.setHighlightSimplePost("</b>"); query.set(HighlightParams.USE_PHRASE_HIGHLIGHTER, true); query.set(getPerFieldParameterName("title", HighlightParams.ALTERNATE_FIELD), "title"); query.set(getPerFieldParameterName("contents", HighlightParams.ALTERNATE_FIELD), "contents"); query.set(getPerFieldParameterName("contents", HighlightParams.ALTERNATE_FIELD_LENGTH), 1000); // ファセット /* query.setFacet(true); query.setFacetLimit(20); query.setFacetMinCount(1); query.setFacetSort(FacetParams.FACET_SORT_COUNT); // ファセットフィールド for (String field : SearchUtil.getFacetLabels().keySet()) { query.addFacetField(field); } for (String category : SearchUtil.getFacetQueries().keySet()) { for (int i = 0; i < SearchUtil.getFacetQueries().get(category).length; i++) { query.addFacetQuery(SearchUtil.getFacetQueries().get(category)[i][0]); } } */ // 検索語の設定 String str = getQueryString(request); if ((str != null) && str.trim().length() > 0) { query.setQuery(str); } // start(取得開始位置) int start = 0; String startStr = request.getParameter("start"); if (!StringUtils.isEmpty(startStr)) { try { start = Integer.parseInt(startStr); request.setAttribute("start", startStr); } catch (NumberFormatException nfe) {} } // fq(絞り込み条件の取得) /* String[] filterQueries = request.getParameterValues("fq"); if (filterQueries != null) { query.addFilterQuery(filterQueries); } request.setAttribute("fq", filterQueries); */ return query; } }