通过SolrJ获取Solr检索结果
1、通过SolrParams的方式提交查询参数
SolrClient solr = new HttpSolrClient("http://localhost:8080/solr/core"); Mapmap = new HashMap (); keyword = "".equals(keyword) ? "*" : keyword; map.put("q", "search_item:" + keyword); map.put("indent", "true"); map.put("hl", "true"); map.put("hl.fl", "title, summary"); map.put("hl.simple.pre", " "); map.put("hl.simple.post", ""); map.put("start", "0"); map.put("rows", "10"); SolrParams params = new MapSolrParams(map); try { QueryResponse resp = solr.query(params); Map >> highlight = resp.getHighlighting();//高亮集合 SolrDocumentList docsList = resp.getResults();//文档集合 int docNumFound = (int) docsList.getNumFound();//文档数 ArrayList
2、通过SolrQuery提交参数
SolrClient solr = new HttpSolrClient("http://localhost:8080/solr/core"); SolrQuery query = new SolrQuery(); //设置高亮 query.setQuery("name:datou"); query.setHighlight(true); // 开启高亮组件或用query.setParam("hl", "true"); query.addHighlightField("name");// 高亮字段 query.setHighlightSimplePre("");//标记,高亮关键字前缀 query.setHighlightSimplePost("");//后缀 // query.setHighlightSnippets(1);//结果分片数,默认为1 // query.setHighlightFragsize(1000);//每个分片的最大长度,默认为100 System.out.println(query);//用于调试程序 QueryResponse rsp = solr.query(query); NamedList list = (NamedList) rsp.getResponse().get("highlighting"); System.out.println(list);//用于显示list中的值 for (int i = 0; i < list.size(); i++){ System.out.println("id=" + list.getName(i) + "文档中高亮显示的字段:" + list.getVal(i)); }