<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<!-- <scope>test</scope>-->
</dependency>
test注释掉才可以在src/main/java里面应用
否则只能在src/test/java里面用
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.7.3</version>
</dependency>
代码:
package com.brt.iem.admin.solr.controller;
import com.brt.common.result.Result;
import com.brt.common.result.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrInputDocument;
import org.junit.Test;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@Api(value="/solr", tags="solr管理Api操作")
@Validated
@RestController
@RequestMapping("/solr")
public class SolrController {
/**
* @description: 查询solr一般
* @author: peiyajie
* @date: 2020年6月19日 下午15:10:12
* @return Result
*/
@ApiOperation(value="查询solr的高亮", notes="查询solr的高亮测试")
@ApiImplicitParams({@ApiImplicitParam(name="menuId", value="菜单id", required=true)
})
@GetMapping("/queryHighlight/{menuId}")
@Test //Result<Object>
public void queryHighlight() {//@NotNull(message = "menuId不可为空"),如果请求和参数相同,则使用@PathVariable("menuId") Integer menuId
Result<Object> result = null;
try {
//构建solr客户端
HttpSolrClient.Builder builder=new HttpSolrClient
.Builder("http://10.217.37.28:8983/solr/peiyajieCore")
.withConnectionTimeout(10000)
.withSocketTimeout(6000);
SolrClient client=builder.build();
//封装查询对象
//q查询关键字,默认为q=*.*;前面是域,后面是查询包含该字段
SolrQuery sql=new SolrQuery();//查询所有 "*:*"
sql.setQuery("roleName:应急资源_市级");//为创建体哦阿健对象开始添加条件,页面模糊查询,并且可以分析器
sql.setSort("roleId", SolrQuery.ORDER.asc);//按照id升序
sql.setStart(0);//设置起始页
sql.setRows(3);//设置分页处理
//返回哪些字段,以逗号分隔
sql.set("fl","id,roleId,roleName");//只查询这两个areaName,id
//打开高亮开关
sql.setHighlight(true);//打开高亮开关
sql.addHighlightField("roleName");//设置高亮域
sql.setHighlightSimplePre("<em style='color:red;'>");//设置高亮前缀
sql.setHighlightSimplePost("</em>");//设置高亮后缀
//执行返回对象
try {
QueryResponse reponse=client.query(sql);
SolrDocumentList list=reponse.getResults();
System.out.println("查询符合条件的个数有:"+list.getNumFound());
//高亮容器,最外面是id:v,域名和List集合,第一个map是文档id,第二个是高亮显示的字段名称
Map<String, Map<String, List<String>>> highlighting= reponse.getHighlighting();//获取高亮的结果,也可以说是高亮的容器
for(SolrDocument solrDocument:list){
String id= (String)solrDocument.get("id");
System.out.println("id:"+id);
String name=(String)solrDocument.getFieldValue("roleName");
System.out.println("name:"+name);
Map<String, List<String>> map=highlighting.get(id);//获取id为*的map
List<String> highlist=map.get("roleName");
System.out.println("map里面商品的名字为:"+highlist.get(0));
}
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
result = Result.success();
} catch (Exception e) {
result = Result.error(ResultEnum.SERVER_EXCEPTION);
// logger.error("queryMenu --- "+e.getMessage(), e);
}
// return result;
}
/**
* @description: 查询solr结合copyfeild使用
* @author: peiyajie
* @date: 2020年6月19日 下午15:10:12
* @return Result
*/
@ApiOperation(value="查询solr的高亮", notes="查询solr的高亮测试")
@ApiImplicitParams({@ApiImplicitParam(name="menuId", value="菜单id", required=true)
})
//匹配满足所有
@GetMapping("/queryHighlight/{menuId}")
@Test //Result<Object>
public void queryHighlightKeywords() {//@NotNull(message = "menuId不可为空"),如果请求和参数相同,则使用@PathVariable("menuId") Integer menuId
Result<Object> result = null;
try {
//构建solr客户端
HttpSolrClient.Builder builder=new HttpSolrClient
.Builder("http://10.217.37.28:8983/solr/peiyajieCore")
.withConnectionTimeout(10000)
.withSocketTimeout(6000);
SolrClient client=builder.build();
//封装查询对象
//q查询关键字,默认为q=*.*;前面是域,后面是查询包含该字段
SolrQuery sql=new SolrQuery();//查询所有 "*:*"
sql.setQuery("yjzy");//为创建体哦阿健对象开始添加条件,页面模糊查询,并且可以分析器
sql.setSort("roleId", SolrQuery.ORDER.asc);//按照id升序
sql.setStart(0);//设置起始页
sql.setRows(3);//设置分页处理
//返回哪些字段,以逗号分隔
sql.set("fl","id,roleId,roleName,roleCode");//只查询这两个roleId,roleName
//默认查询域
sql.set("df","keywords");//复合的都是单
//打开高亮开关
sql.setHighlight(true);//打开高亮开关
sql.addHighlightField("roleName");//设置高亮域
sql.addHighlightField("roleCode");
sql.setHighlightSimplePre("<em style='color:red;'>");//设置高亮前缀
sql.setHighlightSimplePost("</em>");//设置高亮后缀
//执行返回对象
try {
QueryResponse reponse=client.query(sql);
SolrDocumentList list=reponse.getResults();
System.out.println("查询符合条件的个数有:"+list.getNumFound());
//高亮容器,最外面是id:v,域名和List集合,第一个map是文档id,第二个是高亮显示的字段名称
Map<String, Map<String, List<String>>> highlighting= reponse.getHighlighting();//获取高亮的结果,也可以说是高亮的容器
for(SolrDocument solrDocument:list){
String id= (String)solrDocument.get("id");
System.out.println("id:"+id);
String name=(String)solrDocument.getFieldValue("roleName");
System.out.println("name:"+name);
Map<String, List<String>> map=highlighting.get(id);//获取id为*的map
List<String> highlist=map.get("roleName");
System.out.println("map里面商品的名字为:"+highlist.get(0));
}
} catch (SolrServerException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
result = Result.success();
} catch (Exception e) {
result = Result.error(ResultEnum.SERVER_EXCEPTION);
// logger.error("queryMenu --- "+e.getMessage(), e);
}
// return result;
}
/**
* 增加文档
*/
@Test
public void addDocument() throws IOException, SolrServerException {
HttpSolrClient.Builder builder=new HttpSolrClient
.Builder("http://10.217.37.28:8983/solr/peiyajieCore")
.withConnectionTimeout(10000)
.withSocketTimeout(6000);
SolrClient client=builder.build();
//创建一个文档对象
SolrInputDocument input=new SolrInputDocument();
//向文档中添加域
input.addField("roleName","测试solr角色");
input.addField("roleCode","solr_code");
//把文档引入索引库
client.add(input);
client.commit();
client.close();
}
@Test
public void deleteDocument() throws IOException, SolrServerException {
HttpSolrClient.Builder builder = new HttpSolrClient
.Builder("http://10.217.37.28:8983/solr/peiyajieCore")
.withConnectionTimeout(10000)
.withSocketTimeout(6000);
SolrClient client = builder.build();
//client.deleteById("e1195484-4dcf-4259-b426-f99ebd0174f1");
client.deleteByQuery("id:e1195484-4dcf-4259-b426-f99ebd0174f1");
client.commit();
client.close();
}
}