package zr.com.util; import java.io.IOException; import java.util.List; import java.util.Map; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrQuery.ORDER; import org.apache.solr.client.solrj.SolrRequest; 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.Group; import org.apache.solr.client.solrj.response.GroupCommand; import org.apache.solr.client.solrj.response.GroupResponse; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.UpdateResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.apache.solr.common.util.NamedList; import org.junit.Test; /** * * @author LF * */ public class BaseOperation { /** * 删除索引 * @throws IOException * @throws SolrServerException */ @Test public void deleteTest() throws SolrServerException, IOException{ // 建立请求 SolrServer server = new HttpSolrServer("http://192.168.10.105:18080"); // 删除 UpdateResponse ur = server.deleteById("448457__ef004ca974cc42b9a7128cb3a2eced3a"); System.out.println(ur); // 提交 server.commit(); } /** * 添加索引 * @throws IOException * @throws SolrServerException */ @Test public void addTest() throws SolrServerException, IOException{ // 建立请求 SolrServer server = new HttpSolrServer("http://192.168.10.105:18080"); // 创建SolrInputDocument对象 SolrInputDocument document = new SolrInputDocument(); // 添加相应的域(字段) document.addField("id", "15201596724"); document.addField("courseName", "算法导论"); document.addField("schoolName", "计算机技术大学"); server.add(document); // 提交 server.commit(); } /** * 更新 * @throws IOException * @throws SolrServerException */ @Test public void updateTest() throws SolrServerException, IOException{ // 建立请求 SolrServer server = new HttpSolrServer("http://192.168.10.105:18080"); // 创建SolrInputDocument对象 SolrInputDocument document = new SolrInputDocument(); // 添加相应的域(如果id的值不存在,则添加;否则,会先删除原来的数据,再添加) document.addField("id", "15201596724"); document.addField("courseName", "算法导论之算法之美"); document.addField("schoolName", "计算机技术大学"); document.addField("schoolShortName", "计算机技术软件工程学院"); server.add(document); // 提交 server.commit(); } /** * 查询 * @throws SolrServerException */ @Test public void queryTest() throws SolrServerException{ // 建立请求 SolrServer server = new HttpSolrServer("http://192.168.10.105:18080"); // 创建查询对象 SolrQuery query = new SolrQuery(); // 查询语句的处理 // String queryStr = "id:15201596724"; // String queryStr = "courseName:算法导论"; // String queryStr = "courseName:算法导论之算法之美 AND schoolShortName:学院"; String queryStr = "courseName:算法导论之算法之美 OR schoolShortName:学院"; query.setQuery(queryStr); // 分页(起始位置) query.setStart(10); // 分页(每一页显示的条数) query.setRows(5); // 排序(降序) query.setSort("courseName", ORDER.desc); //执行查询 QueryResponse response = server.query(query); // 获取响应后的结果集 SolrDocumentList list = response.getResults(); for (SolrDocument solrDocument : list) { System.out.println(solrDocument); } } /** * 模糊查询(高亮) * @throws SolrServerException */ @Test public void dimQueryTest() throws SolrServerException{ // 建立请求 SolrServer server = new HttpSolrServer("http://192.168.10.105:18080"); // 创建查询对象 SolrQuery query = new SolrQuery(); // 查询语句的处理(模糊查询,添加双引号即可) String queryStr = "courseName:"算法导论" "; // 设置高亮显示 // 是否设置高亮 query.setHighlight(true); // 前缀 query.setHighlightSimplePre("<em>"); // 后缀 query.setHighlightSimplePost("</em>"); // 设置高亮文本的长度 query.setHighlightFragsize(8); query.setHighlightRequireFieldMatch(true); // 设置高亮的字段 query.addHighlightField("courseName"); query.setQuery(queryStr); // // 分页(起始位置) // query.setStart(10); // // 分页(每一页显示的条数) // query.setRows(5); // // 排序(降序) // query.setSort("courseName", ORDER.desc); //执行查询 QueryResponse response = server.query(query); // 获取高亮的信息 Map<String, Map<String, List<String>>> map = response.getHighlighting(); System.out.println(map); //执行查询 // QueryResponse response = server.query(query); // 获取响应后的结果集 SolrDocumentList list = response.getResults(); for (SolrDocument solrDocument : list) { // 获取ID String id = solrDocument.getFieldValue("id").toString(); String courseName = solrDocument.getFieldValue("courseName").toString(); String schoolShortName = solrDocument.getFieldValue("schoolShortName").toString(); // 高亮替换 Map<String, List<String>> hlisMap = map.get(id); if (hlisMap!=null) { // System.out.println(hlisMap); Object cName = hlisMap.get("courseName"); if (cName!=null) { // 替换 courseName = cName.toString(); } } System.out.println("id:"+id+",courseName:"+courseName+",schoolShortName:"+schoolShortName); } } /** * 分组查找 * @throws SolrServerException */ @Test public void groupQuery() throws SolrServerException{ // 建立请求 SolrServer server = new HttpSolrServer("http://192.168.10.105:18080"); // 创建查询对象 SolrQuery query = new SolrQuery(); // 设置查询条件 query.setQuery("courseName:园林工程"); // 是否分组 query.setParam("group", true); // 根据某个域进行分组 query.setParam("group.field", "areaName"); // 进行过滤(在某个区间内) // query.setParam("group.query", "TimeScore:[0 TO 5]"); // 每组显示的个数 query.setParam("group.limit", "10"); // 是否计算所得分组的个数 query.setParam("group.ngroups", true); // query.setStart(5); // query.setRows(100); // 获取响应后的结果集 QueryResponse response = server.query(query); // 获取分组后的结果集 GroupResponse gResponse = response.getGroupResponse(); List<GroupCommand> commands = gResponse.getValues(); if (commands != null) { for (GroupCommand groupCommand : commands) { System.out.println("总分组的个数:"+groupCommand.getNGroups().longValue());; for (Group group : groupCommand.getValues()) { SolrDocumentList list = group.getResult(); // System.out.println(list); for (SolrDocument solrDocument : list) { String id = solrDocument.get("id").toString(); String courseID = solrDocument.get("courseID").toString(); String schoolName = solrDocument.get("schoolName").toString(); String courseName = solrDocument.get("courseName").toString(); String areaName = solrDocument.get("areaName").toString(); System.out.println("areaName"+areaName+",id:"+id+",courseID:"+courseID+",schoolName:"+schoolName+",courseName"+courseName); } } } } } }