solr4.1 高亮查询

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String url = "http://localhost:9080/solr"; 
	request.setCharacterEncoding("UTF-8");	
	String sword = request.getParameter("searchWord");
	 

		List<TestPojo> list = new ArrayList<TestPojo>();
	
		SolrServer server = new HttpSolrServer(url); 
		SolrQuery query = new SolrQuery("username:"+sword);
		query.setHighlight(true);
		query.setParam("hl.fl", "username");
		query.setHighlightSimplePre("<font color=\"red\">");

		query.setHighlightSimplePost("</font>");
		
		try {
			QueryResponse qResponse = server.query(query);
			
			
			
			 SolrDocumentList docs = qResponse.getResults();
			 System.out.println("文档个数:" + docs.getNumFound()); 
			 System.out.println("查询时间:" + qResponse.getQTime());
		for (SolrDocument doc : docs){ 
			String id = doc.getFieldValue("id").toString();
			
				TestPojo pj = new TestPojo();
				pj.setId(id);
				pj.setContext(qResponse.getHighlighting().get(id).get("username").toString());
		//	 System.out.println("id: " + doc.getFieldValue("id")); 
		//	 System.out.println("username: " + doc.getFieldValue("username")); 
		//	 System.out.println(); 
			 list.add(pj);
			}
		} catch (SolrServerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		request.setAttribute("pojoList", list);

		request.getRequestDispatcher("result.jsp").forward(request, response);
		
		
		
	}

你可能感兴趣的:(Solr)