Java Code Examples for org.apache.solr.client.solrj.SolrQuery#setHighlightSimplePost()

The following examples show how to use org.apache.solr.client.solrj.SolrQuery#setHighlightSimplePost() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ItemSearchServiceLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenSearchingWithHitHighlighting_thenKeywordsShouldBeHighlighted() throws Exception {
    itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f);
    itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f);
    itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "Home Appliances", 200f);
    itemSearchService.index("hm0004", "Brand2 Dishwasher", "Washing equipments", 250f);

    SolrQuery query = new SolrQuery();
    query.setQuery("Appliances");
    query.setHighlight(true);
    query.addHighlightField("category");
    query.setHighlightSimplePre("<strong>");
    query.setHighlightSimplePost("</strong>");
    QueryResponse response = solrClient.query(query);

    Map<String, Map<String, List<String>>> hitHighlightedMap = response.getHighlighting();
    Map<String, List<String>> highlightedFieldMap = hitHighlightedMap.get("hm0001");
    List<String> highlightedList = highlightedFieldMap.get("category");
    String highLightedText = highlightedList.get(0);

    assertEquals("Home <strong>Appliances</strong>", highLightedText);

}
 
Example 2
Source File: SearchServiceImpl.java    From learning-taotaoMall with MIT License 5 votes vote down vote up
@Override
public SearchResult search(String queryString, int page, int rows) throws Exception {
	//创建查询对象
	SolrQuery query = new SolrQuery();
	//设置查询条件
	query.setQuery(queryString);
	//设置分页
	query.setStart((page - 1) * rows);
	query.setRows(rows);
	//设置默认搜素域
	query.set("df", "item_keywords");
	//设置高亮显示
	query.setHighlight(true);
	query.addHighlightField("item_title");
	query.setHighlightSimplePre("<em style=\"color:red\">");
	query.setHighlightSimplePost("</em>");
	//执行查询
	SearchResult searchResult = searchDao.search(query);
	//计算查询结果总页数
	long recordCount = searchResult.getRecordCount();
	long pageCount = recordCount / rows;
	if (recordCount % rows > 0) {
		pageCount++;
	}
	searchResult.setPageCount(pageCount);
	searchResult.setCurPage(page);

	return searchResult;
}
 
Example 3
Source File: SolrController.java    From Spring-Boot-Book with Apache License 2.0 4 votes vote down vote up
@RequestMapping("/queryAll")
    public Object queryAll() throws IOException, SolrServerException {


        //第二种方式
        SolrQuery solrQuery = new SolrQuery();
        // 设置默认搜索域
        solrQuery.setQuery("*:*");
//        solrQuery.addField("*");
        solrQuery.set("q", "知然");
        solrQuery.add("q", "name:然");
        // 设置返回结果的排序规则
        solrQuery.setSort("id", SolrQuery.ORDER.asc);
        //设置查询的条数
        solrQuery.setRows(50);
        //设置查询的开始
        solrQuery.setStart(0);
        // 设置分页参数
        solrQuery.setStart(0);
        solrQuery.setRows(20);
        //设置高亮
        solrQuery.setHighlight(true);
        //设置高亮的字段
        solrQuery.addHighlightField("name");
        //设置高亮的样式
        solrQuery.setHighlightSimplePre("<font color='red'>");
        solrQuery.setHighlightSimplePost("</font>");
        System.out.println(solrQuery);
        QueryResponse response = solrClient.query(solrQuery);
        //返回高亮显示结果
        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
        //response.getResults();查询返回的结果

        SolrDocumentList documentList = response.getResults();
        long numFound = documentList.getNumFound();
        System.out.println("总共查询到的文档数量: " + numFound);
        for (SolrDocument solrDocument : documentList) {
            System.out.println("solrDocument==============" + solrDocument);
            System.out.println("solrDocument==============" + solrDocument.get("name"));
        }
        return highlighting;
    }
 
Example 4
Source File: SolrControllerTest.java    From Spring-Boot-Book with Apache License 2.0 4 votes vote down vote up
@Test
    public void queryAll() throws IOException, SolrServerException {


        //第二种方式
        SolrQuery solrQuery = new SolrQuery();
        // 设置默认搜索域
        solrQuery.setQuery("*:*");
//        solrQuery.addField("*");
        solrQuery.set("q", "知然");
        solrQuery.add("q", "name:然");
        // 设置返回结果的排序规则
        solrQuery.setSort("id", SolrQuery.ORDER.asc);
        //设置查询的条数
        solrQuery.setRows(50);
        //设置查询的开始
        solrQuery.setStart(0);
        // 设置分页参数
        solrQuery.setStart(0);
        solrQuery.setRows(20);
        //设置高亮
        solrQuery.setHighlight(true);
        //设置高亮的字段
        solrQuery.addHighlightField("name");
        //设置高亮的样式
        solrQuery.setHighlightSimplePre("<font color='red'>");
        solrQuery.setHighlightSimplePost("</font>");
        System.out.println(solrQuery);
        QueryResponse response = solrClient.query(solrQuery);
        //返回高亮显示结果
        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
        //response.getResults();查询返回的结果

        SolrDocumentList documentList = response.getResults();
        long numFound = documentList.getNumFound();
        System.out.println("总共查询到的文档数量: " + numFound);
        for (SolrDocument solrDocument : documentList) {
            System.out.println(solrDocument);
            System.out.println(solrDocument.get("name"));
        }
        System.out.println(highlighting);
    }