package testsolrj;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.ORDER;
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;
/**
* SolrJ学习api
* @author 黄宝康
* 2018年9月17日 下午7:07:21
*/
public class TestSolrj {
@Test
public void addDocument() throws Exception, IOException {
HttpSolrServer solrServer = new HttpSolrServer("http://192.168.254.130:8080/solr/new_core");
for (int i = 3; i < 100; i++) {
// 创建一个文档对象
SolrInputDocument sd = new SolrInputDocument();
// 添加域
sd.addField("id", UUID.randomUUID());
sd.addField("title", "商品" + i);
sd.addField("author", "huanbaokang" + i);
sd.addField("content", "内容" + i);
solrServer.add(sd);
solrServer.commit();
}
}
// 根据document的Id直接删除
@Test
public void deleteDocument() throws Exception {
// 创建Solr的客户端链接对象
HttpSolrServer solrServer = new HttpSolrServer("http://192.168.254.130:8080/solr/new_core");
solrServer.deleteById("cd8b6018-7317-4a33-8c4e-a6f97e42da5b");
solrServer.commit();
}
// 根据条件查询删除
@Test
public void deleteQueryDocument() throws Exception {
// 创建Solr的客户端链接对象
HttpSolrServer solrServer = new HttpSolrServer("http://192.168.254.130:8080/solr/new_core");
// 查询删除
solrServer.deleteByQuery("title:商品1");
solrServer.commit();
}
@Test
public void queryDocument() throws Exception {
// 创建Solr的客户端链接对象
HttpSolrServer solrServer = new HttpSolrServer("http://192.168.254.130:8080/solr/new_core");
// 创建solr的查询对象
SolrQuery sq = new SolrQuery();
// 设置查询条件
sq.set("q", "title:商品11");
// 查询
QueryResponse qr = solrServer.query(sq);
// 获取查询结果
SolrDocumentList sds = qr.getResults();
// 获取查询的记录数
long total = sds.getNumFound();
System.out.println("数量:" + total);
for (SolrDocument sd : sds) {// 默认取出10条记录
String id = (String) sd.getFieldValue("id");
String title = (String) sd.getFieldValue("title");
String author = (String) sd.getFieldValue("author");
String content = (String) sd.getFieldValue("content");
System.out.println("========================================");
System.out.println("id:" + id);
System.out.println("title:" + title);
System.out.println("author:" + author);
System.out.println("content:" + content);
}
}
@Test
public void queryDocument2() throws Exception {
// 创建Solr的客户端链接对象
HttpSolrServer solrServer = new HttpSolrServer("http://192.168.254.130:8080/solr/new_core");
// 创建solr的查询对象
SolrQuery sq = new SolrQuery();
// 设置查询条件
sq.set("q", "title:商品11");
// 设置过滤条件
// sq.set("fq", "item_price:[1 TO 20]");
// 设置排序
sq.addSort("title", ORDER.desc);
// 设置分页
sq.setStart(0);// 开始位置
sq.setRows(3);// 每页3条
// 开启高亮
sq.setHighlight(true);
sq.addHighlightField("title");// 设置高亮域
sq.setHighlightSimplePre("<b>");// 设置高亮样式
sq.setHighlightSimplePost("</b>");
// 查询
QueryResponse qr = solrServer.query(sq);
// 获取查询结果
SolrDocumentList sds = qr.getResults();
// 获取查询的记录数
long total = sds.getNumFound();
System.out.println("数量:" + total);
for (SolrDocument sd : sds) {// 默认取出10条记录
String id = (String) sd.getFieldValue("id");
String title = (String) sd.getFieldValue("title");
String author = (String) sd.getFieldValue("author");
String content = (String) sd.getFieldValue("content");
System.out.println("========================================");
System.out.println("id:" + id);
System.out.println("title:" + title);
System.out.println("author:" + author);
System.out.println("content:" + content);
// 获取高亮显示的结构
Map<String, Map<String, List<String>>> highlighting = qr.getHighlighting();
if (highlighting != null) {
// 根据Id获得每个域的高亮内容
Map<String, List<String>> map = highlighting.get(id);
// 根据具体的域获取高亮内容
List<String> list = map.get("title");
if (list != null && !list.isEmpty()) {
for (String str : list) {
System.out.println("str:" + str);
}
}
}
}
}
}
solr 学习之solrJ
最新推荐文章于 2024-04-07 11:35:15 发布