1.solrj环境搭建
- Solr:5.3.1
- Jdk环境:1.8
- IDE环境:IDEA
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<!--solrj-->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>5.3.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jcl</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<!-- Test dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<!--IDEA编译src的java文件,而目录下的xml文件并不会一起打包,需要手动指定哪些配置文件需要读取-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
2.代码时间
solrj的所有操作,都围绕HttpSolrClient
公共代码段
在以下测试类中,都要使用这个HttpSolrClient
, 于是我们可以预先对其初始化
private HttpSolrClient httpSolrClient;
@Before
public void init(){
//solr的http请求地址/索引库名
String baseURL = "http://localhost:8080/solr/collection1";
httpSolrClient = new HttpSolrClient(baseURL);
}
2.1 增加索引
httpSolrClient.add(document);
需要提交事务
/***
* 增加索引
* @throws Exception
*/
@Test
public void testCreateIndex() throws Exception{
//Document 创建文档对象
SolrInputDocument document = new SolrInputDocument();
//添加文档域
document.addField("id","c1001");
document.addField("content_ik","我爱传智播客");
//httpSolrClient实现对Solr的操作
httpSolrClient.add(document);
//提交
httpSolrClient.commit();
}
2.2 删除索引
- 根据ID删除数据
- 根据条件删除数据
- 删除所有
@Test
public void testDeleteIndex() throws Exception{
//1、根据ID删除数据
//httpSolrClient.deleteById("c1001");
//2、根据条件删除数据
//httpSolrClient.deleteByQuery("content_ik:推荐");
//3、删除所有
httpSolrClient.deleteByQuery("*:*");
//提交
httpSolrClient.commit();
}
2.3 查询索引
简单查询
//创建Query查询,查询所有
SolrQuery solrQuery = new SolrQuery(“:“);
总是需要我们自己将查询出来的结果一个个注入到bean中是十分麻烦的,想要偷懒的可以参考 自己定义的注解和工具类封装Document
@Test
public void testSimpleSearch() throws Exception{
//创建Query查询,查询所有
SolrQuery solrQuery = new SolrQuery("*:*");
//执行查询
QueryResponse response = httpSolrClient.query(solrQuery);
//获取SolrDocumentList对象
SolrDocumentList results = response.getResults();
//结果集
for (SolrDocument result : results) {
//可以使用自定义的注解和工具类解决这个封装的问题
Object o = result.get(索引域名);
}
}
复杂查询
依照功能界面进行设置参数
@Test
public void fun01() throws IOException, SolrServerException {
/***
* 复合查询
* @throws IOException
* @throws SolrServerException
*/
//搜索商品名字里带有小黄人的商品
SolrQuery solrQuery = new SolrQuery("product_name:小黄人");
//设置默认域
solrQuery.set("df", "product_name");
//设置过滤条件
solrQuery.addFilterQuery("product_price:[8.5 TO 10]");
//价格排序
solrQuery.setSort("product_price", SolrQuery.ORDER.desc);
//分页设置
solrQuery.setStart(0);
solrQuery.setRows(5);
//高亮设置
solrQuery.setHighlight(true); //开启高亮
solrQuery.addHighlightField("product_name"); //设置高亮域
solrQuery.setHighlightSimplePre("<font style='color:red'>");
solrQuery.setHighlightSimplePost("</font>");
//执行查询
QueryResponse response = httpSolrClient.query(solrQuery);
//结果集
for (SolrDocument result : response.getResults()) {
//设置高亮,将result中对应域对象的值替换成带有高亮标签的值
setHighlight(response, result, "product_name");
//使用自定义的工具类封装数据
Product product = Doc2BeanUtil.getBean(Product.class, result);
System.out.println(product);
}
}
设置高亮的工具方法
/**
* 设置高亮
* @param response
* @param result
* @param filedName
*/
private void setHighlight(QueryResponse response, SolrDocument result, String filedName) {
//获取高亮数据
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
//判断是否有高亮数据,取出高亮数据
Map<String, List<String>> listMap = highlighting.get(result.get("id"));
if (listMap != null) {
//高亮数据
String hresult = listMap.get(filedName).get(0);
//普通数据替换成高亮数据
result.setField(filedName, hresult);
}
}