我们使用SolrJ来管理索引库,一般习惯是先建一个单元测试类测试下增删改查方法是否好使,因为这样做可以大大减少出错概率,提升开发效率。
由于我们的taotao-search-service工程还没有添加对SolrJ的依赖,因此需要先添加对SolrJ的依赖,即在taotao-search-service工程的pom文件当中添加如下依赖(之所以不用写版本号是因为在taotao-parent工程中已经统一定义好了版本号)。
<!-- Solr客户端 --> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> </dependency>除此之外,由于要进行单元测试,所以还需在pom文件中添加对junit的依赖。
<!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency>这样,taotao-search-service工程的pom文件就变成下面这个样子了。
<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> <parent> <groupId>com.taotao</groupId> <artifactId>taotao-search</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>taotao-search-service</artifactId> <packaging>war</packaging> <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <!-- Solr客户端 --> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> </dependency> <!-- 依赖taotao-search-interface --> <dependency> <groupId>com.taotao</groupId> <artifactId>taotao-search-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <!-- 与Dubbo相关 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <!-- 排除依赖 --> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> <exclusion> <groupId>org.jboss.netty</groupId> <artifactId>netty</artifactId> </exclusion> </exclusions> </dependency> <!-- Zookeeper的客户端,你要连接Zookeeper,需要把以下两个jar包加进来 --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> </dependency> </dependencies> <build> <plugins> <!-- 配置打包时跳过测试(一般打包的时候,会使用Maven install命令,如果你写了一些测试类, 它会执行这个测试类的,但是我们不希望它们执行,所以就需要有这样一个插件) --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skipTests>true</skipTests> </configuration> </plugin> </plugins> </build> </project>下面我们新建一个SolrJTest测试类,如下图所示,咱先来测试一下添加文档的操作。注意:添加的字段必须是managed-schema文件定义好的。 为了方便大家复制,现将SolrJTest测试类的代码贴出,如下所示。
package com.taotao.search.test; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.common.SolrInputDocument; import org.junit.Test; public class SolrJTest { @Test public void testSolrJAddDocument() throws Exception { // 1. 创建SolrClient对象,建立连接,需要指定url地址 // 和单机版Solr服务器建立连接 String baseURL = "http://192.168.81.133:8080/solr/core1"; // Solr服务器的地址,特别指定核1 SolrClient solrClient = new HttpSolrClient.Builder(baseURL) .withConnectionTimeout(10000) .withSocketTimeout(60000) .build(); // 2. 创建SolrInputDocument对象 SolrInputDocument document = new SolrInputDocument(); // 3. 向文档中添加域 document.setField("id", "test001"); document.setField("item_title", "海尔空调"); document.setField("item_sell_point", "送电暖宝一个哟!"); document.setField("item_price", 10000); document.setField("item_image", "http://www.123.ipg"); document.setField("item_category_name", "电器"); document.setField("item_desc", "这是一款最新的空调,质量好,值得您信赖!!"); // 4. 将文档提交到索引库当中 solrClient.add(document); solrClient.commit(); } }执行上面的测试方法,有可能Eclipse控制台会报如下这样一个异常。 如果要想解决这个错误,那么可以参考我的《淘淘商城第46讲——使用SolrJ管理索引库时,报错:java.lang.NoSuchMethodError: org.apache.http.impl.conn.PoolingHttpClie…》这篇文章进行学习。
以上测试方法执行成功后,我们到Solr首页,点击Execute Query按钮,即可查询到我们刚才添加的文档。 大家发现了没有,在查询出来的数据当中没有item_desc字段,这是因为在managed-schema文件中指定了该字段不保存,如下图所示,indexed="true"表示分词存储且可以查询,stored="false"表示不存储内容,如果stored="false",那么该字段就不会随查询结果一起显示,只有当stored="true"时才会显示。 下面我们来测试通过id删除文档这一操作。首先我们需要在SolrJTest单元测试类中添加如下测试方法。
@Test public void testSolrJDeleteDocument() throws Exception { // 1. 创建SolrClient对象,建立连接,需要指定url地址 // 和单机版Solr服务器建立连接 String baseURL = "http://192.168.81.133:8080/solr/core1"; // Solr服务器的地址,特别指定核1 SolrClient solrClient = new HttpSolrClient.Builder(baseURL) .withConnectionTimeout(10000) .withSocketTimeout(60000) .build(); // 通过id来删除文档 solrClient.deleteById("test001"); // 提交 solrClient.commit(); }执行上面的方法,成功后,我们再查询,发现已经没有刚才我们添加的那个文档了,如下图所示。 下面我们再来测试另外一种删除方法,由于刚才我们把仅存的一条文档删除了,因此现在我们再来添加两个文档,如下图所示。 现在通过搜索对象来删除文档,故应在SolrJTest单元测试类中添加如下测试方法。
@Test public void deleteDocumentByQuery() throws Exception { // 1. 创建SolrClient对象,建立连接,需要指定url地址 // 和单机版Solr服务器建立连接 String baseURL = "http://192.168.81.133:8080/solr/core1"; // Solr服务器的地址,特别指定核1 SolrClient solrClient = new HttpSolrClient.Builder(baseURL) .withConnectionTimeout(10000) .withSocketTimeout(60000) .build(); // 通过价格来删除文档 solrClient.deleteByQuery("item_price:7000"); // 提交 solrClient.commit(); }执行上面的方法,成功后,我们再查询,发现已经没有价格为7000的那个文档记录了,如下图所示。 这里我就不再编写代码测试修改文档这一操作了,因为修改文档跟添加文档是一样的,只要id相同,那么新记录就会覆盖老记录,从而就能达到修改文档的目的了。
最后,我们来测试一下查询文档这一操作,我们需要在SolrJTest单元测试类中添加如下测试方法。
@Test public void queryDocument() throws Exception { // 1. 创建SolrClient对象,建立连接,需要指定url地址 // 和单机版Solr服务器建立连接 String baseURL = "http://192.168.81.133:8080/solr/core1"; // Solr服务器的地址,特别指定核1 SolrClient solrClient = new HttpSolrClient.Builder(baseURL) .withConnectionTimeout(10000) .withSocketTimeout(60000) .build(); SolrQuery query = new SolrQuery(); query.setQuery("id:test001"); QueryResponse response = solrClient.query(query); SolrDocumentList list = response.getResults(); for (SolrDocument document : list) { String id = document.getFieldValue("id").toString(); String title = document.getFieldValue("item_title").toString(); System.out.println(id); System.out.println(title); } }执行上面的方法,成功后,我们便可在Eclipse控制台上看到如下打印结果。