一 、业务字段配置
vim solrhome/collection1/conf/schema.xml
添加如下配置
商品信息格式
- 重启tomcat
查询测试(高亮)
二 、java通过Solrj操作solr
1. 创建maven工程 (pom.xml)
<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.jiazf.demo</groupId>
<artifactId>solrdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
2. 创建java测试类
package mytest;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
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;
public class SolrjTest {
//创建一个连接
SolrServer solrServer = new HttpSolrServer("http://192.168.153.22:8080/solr");
@Test
/**
* 增加操作
*/
public void addItem() throws IOException, SolrServerException {
//创建一个文档对象
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "test001");
document.addField("item_title", "测试title");
document.addField("item_sell_point", "测试");
document.addField("item_price", "11111");
document.addField("item_image", "111111");
document.addField("item_category_name", "111111");
//将item添加到solr的服务器中
solrServer.add(document);
solrServer.commit();
}
@Test
/**
* 删除操作
*/
public void deleteItem() throws IOException, SolrServerException {
solrServer.deleteByQuery("id:test001");
solrServer.commit();
}
@Test
/**
* 查询 "三星" 关键字
*/
public void paginatedQuery() throws Exception {
int page = 8;
int size = 5;
// 创建一个查询对象
SolrQuery query = new SolrQuery();
// 设置查询条件
query.setQuery("三星");
//设置开始条目数字
query.setStart((page - 1) * size);
//设置每页显示多少条记录
query.setRows(size);
//设置是否使用高亮
query.setHighlight(true);
//高亮的前置字符串
query.setHighlightSimplePre("<span style='color:red; font-weight:bold'>");
//高亮的后置字符串
query.setHighlightSimplePost("</span>");
//设置默认字段用于搜索
query.set("df", "item_title");
//设置添加高亮的字段
query.addHighlightField("item_title");
// 根据查询条件查询索引库
QueryResponse queryResponse = solrServer.query(query);
// 取查询结果
SolrDocumentList solrDocumentList = queryResponse.getResults();
// 取查询结果总数量
System.out.println(solrDocumentList.getNumFound());
// 取高亮显示
Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
// 取商品列表
for (SolrDocument solrDocument : solrDocumentList) {
System.out.println(solrDocument.get("id"));
// 取高亮显示的结果
List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
String title = "";
if (list != null && list.size() > 0) {
title = list.get(0);
} else {
title = (String) solrDocument.get("item_title");
}
System.out.println(title);
System.out.println(solrDocument.get("item_image"));
System.out.println(solrDocument.get("item_price"));
System.out.println(solrDocument.get("item_sell_point"));
System.out.println(solrDocument.get("item_category_name"));
System.out.println("=======================================");
}
}
}
3. 效果
-
插入效果
-
删除效果
-
查询效果