前言:
本文对solr的操作依赖于上篇《solr本地部署启动》https://blog.csdn.net/qq_15076569/article/details/83032151
一:solr依赖的jar
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.2</version>
</dependency>
<!--日志的包, solrj执行需要一个日志包-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging-api</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
二:查询索引案例
public class SolrExample {
/**
* solr写入索引
* @throws IOException
* @throws SolrServerException
*/
@Test
public void createIndexToSolr() throws IOException, SolrServerException {
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/newDB");
SolrInputDocument solrInputFields = new SolrInputDocument();
solrInputFields.addField("id", UUID.randomUUID().toString());
solrInputFields.addField("name","指数");
solrInputFields.addField("title","中山平安指数");
solrInputFields.addField("content","平安指数的算法是根据各个区镇报警数消防出警数除以各个区镇的总人口数乘以1000得来的平安指数");
solrServer.add(solrInputFields);
solrServer.commit();
}
/**
* 添加多条索引
* @throws IOException
* @throws SolrServerException
*/
@Test
public void createIndexManyToSolr() throws IOException, SolrServerException {
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/newDB");
List<SolrInputDocument> list = new ArrayList<SolrInputDocument>();
for(int i=1;i<101;i++){
SolrInputDocument solrInputFields = new SolrInputDocument();
solrInputFields.addField("id", UUID.randomUUID().toString());
solrInputFields.addField("createDate",new Date());
if(i%2==0){
solrInputFields.addField("name",i+"棒棒");
solrInputFields.addField("title",i+"最后的棒棒");
solrInputFields.addField("content",i+"劳动人名劳动光荣");
list.add(solrInputFields);
}else{
solrInputFields.addField("name",i+"指数");
solrInputFields.addField("title",i+"中山平安指数");
solrInputFields.addField("content",i+"平安指数的算法是根据各个区镇报警数消防出警数除以各个区镇的总人口数乘以1000得来的平安指数");
list.add(solrInputFields);
}
}
solrServer.add(list);
solrServer.commit();
}
/**
* 创建索引根据javaBean方式
*/
@Test
public void createIndexByBeanToSolr() throws IOException, SolrServerException {
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/newDB");
Pazs p = new Pazs();
p.setId(UUID.randomUUID().toString());
p.setName("平安指数Bean方式");
p.setTitle("中山平安指数Bean方式");
p.setCreateDate(new Date());
p.setContent("Bean方式内容");
solrServer.addBean(p);
solrServer.commit();
}
/**
* 删除索引
*/
@Test
public void deleteIndexToSolr() throws IOException, SolrServerException {
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/newDB");
// solrServer.deleteById("968b7a61-8b5f-42c7-aeca-de6a28bd4982");
//删除格式:字段名:字段值;*:所有
// solrServer.deleteByQuery("title:平安");
solrServer.deleteByQuery("*:*");
solrServer.commit();
}
/**
* 布尔查询及
*/
@Test
public void baseQueryToSolr() throws IOException, SolrServerException {
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/newDB");
SolrQuery solrQuery = new SolrQuery("title:指数 OR content:光荣");
QueryResponse queryResponse = solrServer.query(solrQuery);
List<Pazs> beans = queryResponse.getBeans(Pazs.class);
SolrDocumentList results = queryResponse.getResults();
for (Pazs bean : beans) {
System.out.println(bean);
}
for (SolrDocument solrDocument : results) {
Object id = solrDocument.get("id");
Object name = solrDocument.get("name");
Object title = solrDocument.get("title");
System.out.println(id.toString()+name.toString()+title.toString());
}
solrServer.commit();
}
/**
* 子表达式查询
* @throws SolrServerException
*/
@Test
public void expressionQueryToSolr() throws SolrServerException {
//表达式查询: (条件1 [or and not] 条件2 ) [and or not] (条件1 [or and not] 条件2)
SolrQuery solrQuery = new SolrQuery("(content:劳动 NOT name:棒棒) OR name:指数");
solrQuery.setRows(20);//显示多少条数据
baseQuery(solrQuery);
}
/**
* 相识度查询
* @throws SolrServerException
*/
@Test
public void fuzzQueryToSolr() throws SolrServerException {
//相识度查询: ~
// 格式: 字段名称: 字段值~ 表示最大编辑2次
// 字段名称: 字段值~1 表示最大编辑1次
SolrQuery solrQuery = new SolrQuery("title:'最后~'~2");
solrQuery.setRows(20);//显示多少条数据
baseQuery(solrQuery);
}
/**
* 范围查询
* @throws SolrServerException
*/
@Test
public void rangeQueryToSolr() throws SolrServerException {
//范围查询: 支持数值 日期 文本
// 格式 [ 开始 TO 结束 ] 包含边界值
SolrQuery solrQuery = new SolrQuery("name:[0 TO 2]");
solrQuery.setRows(20);//显示多少条数据
baseQuery(solrQuery);
}
/**
* 排序查询
* @throws SolrServerException
*/
@Test
public void sortQueryToSolr() throws SolrServerException {
SolrQuery solrQuery = new SolrQuery("*:*");
//设置排序:
// 参数1 指定排序的字段 参数2: 排序方式 asc 和 desc
SolrQuery.SortClause sortClause = new SolrQuery.SortClause("createDate", "asc");
solrQuery.setSort(sortClause);
solrQuery.setRows(20);//显示多少条数据
baseQuery(solrQuery);
}
/**
* 搜索高亮显示及分页显示
* @throws SolrServerException
*/
@Test
public void highlighterQueryToSolr() throws SolrServerException {
//1. 创建solrServer对象
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/newDB");
//定义两个参数
int page = 1;//当前页
int pageSize = 3; //每页的条数
SolrQuery solrQuery = new SolrQuery("content:算法人口");
//设置高亮------开始
solrQuery.setHighlight(true);
solrQuery.addHighlightField("content");
solrQuery.setHighlightSimplePre("<font color='red'>");
solrQuery.setHighlightSimplePost("</font>");
solrQuery.setHighlightSnippets(10); //设置最大的分片数,默认为1, 主要针对如果是多字段的情况
//设置高亮------结束
//分页设置
solrQuery.setStart((page-1)*pageSize);
solrQuery.setRows(pageSize);//显示多少条数据
QueryResponse response = solrServer.query(solrQuery);
//获取高亮的集合
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
for (String docID : highlighting.keySet()) {
//获取对应文档的高亮集合
Map<String, List<String>> map = highlighting.get(docID);
List<String> list = map.get("content");
System.out.println("list的长度"+list.size());
System.out.println("高亮内容"+list.get(0));
}
}
public void baseQuery(SolrQuery query) throws SolrServerException {
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/newDB");
QueryResponse queryResponse = solrServer.query(query);
List<Pazs> pazs = queryResponse.getBeans(Pazs.class);
for (Pazs paz : pazs) {
System.out.println(paz);
}
}
}