Solr里面导入了中文分词器 IKAnalyzer6.5.0.jar
新建一个Maven项目
项目结构
编写代码
(1)导入solr的pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wjx</groupId>
<artifactId>solr</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
<!--solr的依赖-->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>
(2)测试Solr类
包括新增,查询,根据Id查询,查询全部,高亮显示
package com.wjx;
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 java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* @Description: solr测试类
* @Auther: wjx
* @Date: 2019/2/13 14:28
*/
public class SolrTest {
/**
* solr的java客户端
*/
private HttpSolrClient solrClient;
/**
* 获取连接
*
* @return
*/
private HttpSolrClient getSolrClient() {
synchronized (this) {
if (solrClient == null) {
solrClient = new HttpSolrClient.Builder().withBaseSolrUrl("http://114.116.24.32:8080/solr/collection_1").build();
}
}
return solrClient;
}
/**
* 测试提交solr到服务器
*
* @throws IOException
* @throws SolrServerException
*/
@Test
public void testAddDocument() throws IOException, SolrServerException {
//获取solr的服务
getSolrClient();
for (int i = 0; i < 100; i++) {
SolrInputDocument document = new SolrInputDocument();
document.addField("id", i);
document.addField("title_ik", "这是一部手机" + i);
document.addField("content_ik", "手机描述" + i);
solrClient.add(document);
}
//提交
solrClient.commit();
//关闭
solrClient.close();
System.out.println("提交成功...");
}
/**
* 根据Id删除
*
* @throws IOException
* @throws SolrServerException
*/
@Test
public void testDeleteId() throws IOException, SolrServerException {
//获取solr的服务
getSolrClient();
solrClient.deleteById("200");
solrClient.commit();
solrClient.close();
}
/**
* 删除所有
*
* @throws IOException
* @throws SolrServerException
*/
@Test
public void testDeleteAll() throws IOException, SolrServerException {
getSolrClient();
solrClient.deleteByQuery("*:*");
solrClient.commit();
solrClient.close();
}
/**
* 全部查询
*
* @throws IOException
* @throws SolrServerException
*/
@Test
public void testAllQuery() throws IOException, SolrServerException {
getSolrClient();
//设置条件
SolrQuery solrQuery = new SolrQuery();
//查询的前100条记录
solrQuery.setRows(100);
//查询的条件
solrQuery.setQuery("*:*");
//查询返回的对象
QueryResponse response = solrClient.query(solrQuery);
//查询返回的集合
SolrDocumentList results = response.getResults();
for (SolrDocument document : results) {
Object id = document.get("id");
Object title_ik = document.get("title_ik");
Object content_ik = document.get("content_ik");
System.out.println("id:" + id + ",title_ik:" + title_ik + ",content_ik:" + content_ik);
}
solrClient.close();
}
/**
* 分页查询
*
* @throws SolrServerException
* @throws IOException
*/
@Test
public void testQueryByPage() throws SolrServerException, IOException {
getSolrClient();
// 以后参数都是通过这个对象去构造...
SolrQuery solrQuery = new SolrQuery();
//solrParams.setQuery("title_ik:手机"); 注释掉,按照条件查询
solrQuery.setQuery("*:*");
//分页
solrQuery.setStart(10);//当前查询起始页
solrQuery.setRows(10);//每页显示多少条数据
QueryResponse queryResponse = solrClient.query(solrQuery);
// 返回结果,默认只返回10条记录
SolrDocumentList documentList = queryResponse.getResults();
for (SolrDocument solrDocument : documentList) {
Object title_ik = solrDocument.get("title_ik");
System.out.println(title_ik);
}
}
/**
* 高亮显示
*
* @throws IOException
* @throws SolrServerException
*/
@Test
public void testHighlighting() throws IOException, SolrServerException {
getSolrClient();
// 以后参数都是通过这个对象去构造...
SolrQuery solrQuery = new SolrQuery();
solrQuery.setQuery("title_ik:手机");
//分页
solrQuery.setStart(0);//当前查询起始页
solrQuery.setRows(10);//每页显示多少条数据
//开启高亮
solrQuery.setHighlight(true);
//设置需要高亮显示的字段名
solrQuery.setParam("hl.fl", "title_ik");
//设置高亮前置显示
solrQuery.setHighlightSimplePre("<span color='red'>");
//设置高亮后缀显示
solrQuery.setHighlightSimplePost("</span>");
QueryResponse response = solrClient.query(solrQuery);
SolrDocumentList documentList = response.getResults();
Map<String, Map<String, List<String>>> highlightingMap = response.getHighlighting();
for (SolrDocument document : documentList) {
//id
Object id = document.get("id");
//根据Id查到高亮显示的结果
Map<String, List<String>> stringList = highlightingMap.get(id);
System.out.println(stringList);
}
}
}
高亮显示结果: