Java Code Examples for org.apache.solr.client.solrj.response.QueryResponse#getHighlighting()

The following examples show how to use org.apache.solr.client.solrj.response.QueryResponse#getHighlighting() . 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: SearchDaoImpl.java    From learning-taotaoMall with MIT License 5 votes vote down vote up
@Override
public SearchResult search(SolrQuery query) throws Exception {
	//返回值对象
	SearchResult result = new SearchResult();
	//根据查询条件查询索引库
	QueryResponse queryResponse = solrServer.query(query);
	//取查询结果
	SolrDocumentList solrDocumentList = queryResponse.getResults();
	//取查询结果总数量
	result.setRecordCount(solrDocumentList.getNumFound());
	//商品列表
	List<Item> itemList = new ArrayList<>();
	//取高亮显示
	Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
	//取商品列表
	for (SolrDocument solrDocument : solrDocumentList) {
		//创建一商品对象
		Item item = new Item();
		item.setId((String) solrDocument.get("id"));
		//取高亮显示的结果
		List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
		String title;
		if (list != null && list.size() > 0) {
			title = list.get(0);
		} else {
			title = (String) solrDocument.get("item_title");
		}
		item.setTitle(title);
		item.setImage((String) solrDocument.get("item_image"));
		item.setPrice((long) solrDocument.get("item_price"));
		item.setSell_point((String) solrDocument.get("item_sell_point"));
		item.setCategory_name((String) solrDocument.get("item_category_name"));
		//添加的商品列表
		itemList.add(item);
	}
	result.setItemList(itemList);
	return result;
}
 
Example 3
Source File: SolrSearchQuery.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Iterable<? extends DocumentScore> query(Resource resource) throws IOException {
	QueryResponse response;
	if (query.getHighlight()) {
		query.addField("*");
	} else {
		query.addField(SearchFields.URI_FIELD_NAME);
	}
	query.addField("score");
	try {
		if (resource != null) {
			response = index.search(resource, query);
		} else {
			response = index.search(query);
		}
	} catch (SolrServerException e) {
		throw new IOException(e);
	}
	SolrDocumentList results = response.getResults();
	final Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
	return Iterables.transform(results, (SolrDocument document) -> {
		SolrSearchDocument doc = new SolrSearchDocument(document);
		Map<String, List<String>> docHighlighting = (highlighting != null) ? highlighting.get(doc.getId())
				: null;
		return new SolrDocumentScore(doc, docHighlighting);
	});
}
 
Example 4
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 5
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);
    }