一.solo是什么
企业级应用搜索服务器.
二.solo的使用
1.导包
junit
junit
4.11
org.apache.solr
solr-solrj
4.10.2
org.slf4j
slf4j-log4j12
1.7.22
commons-logging
commons-logging
1.2
org.apache.maven.plugins
maven-compiler-plugin
3.2
1.8
UTF-8
2.添加索引库数据
/**
* 添加索引库数据
*/
@Test
public void testName() throws Exception {
//第一步 连接solr服务器
HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
//第二步 创建文档对象
SolrInputDocument solrInputDocument = new SolrInputDocument();
solrInputDocument.addField("id", "6");
solrInputDocument.addField("title", "oppo手机,充电两分钟,通话两小时,居家旅行必备之选");
//第三步 写数据
solrServer.add(solrInputDocument);
solrServer.commit();
}
3.添加多个索引库数据
/**
* 添加多个索引库数据
*/
@Test
public void testName001() throws Exception {
//第一步 连接solr服务器
HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
//第二步 创建文档对象
List list =new ArrayList();
SolrInputDocument solrInputDocument = new SolrInputDocument();
solrInputDocument.addField("id", "1");
solrInputDocument.addField("title", "小米手机,发烧友");
SolrInputDocument solrInputDocument2 = new SolrInputDocument();
solrInputDocument2.addField("id", "2");
solrInputDocument2.addField("title", "锤子手机,老罗");
SolrInputDocument solrInputDocument3 = new SolrInputDocument();
solrInputDocument3.addField("id", "3");
solrInputDocument3.addField("title", "苹果手机,装逼圣器");
SolrInputDocument solrInputDocument4 = new SolrInputDocument();
solrInputDocument4.addField("id", "4");
solrInputDocument4.addField("title", "三星手机,小心爆炸");
list.add(solrInputDocument4);
list.add(solrInputDocument2);
list.add(solrInputDocument3);
list.add(solrInputDocument);
//第三步 写数据
solrServer.add(list);
solrServer.commit();
}
4.添加javabean对象到索引库数据中
4.1设置Javabean对象:注意要添加注解@Field 不然会报错
不添加注解报错的信息 org.apache.solr.client.solrj.beans.BindingException: class: class
public class Item {
@Field
private String id;
@Field
private String title;
@Field
private String text;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
@Override
public String toString() {
return "Item [id=" + id + ", title=" + title + ", text=" + text + "]";
}
}
4.2测试案例
/**
* 使用javabean导入
*
*/
@Test
public void testName2() throws Exception {
//第一步 获取连接器
HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
//第二步 创建需要导入的对象
Item item = new Item();
item.setId("7");
item.setTitle("金立手机,成功人士的标配");
//第三步 导入
solrServer.addBean(item);
solrServer.commit();
}
5.按照id删除索引数据库
/**
* 按照id删除索引数据库
*/
@Test
public void testName3() throws Exception {
HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
solrServer.deleteById("6");
solrServer.commit();
}
6.按照索引库标签删除
/**
* 按照索引库删除
*/
@Test
public void testName4() throws Exception {
HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
String query="title:旅行";
solrServer.deleteByQuery(query);
solrServer.commit();
}
7.查询所有数据
/**
* 查询所有数据
*/
@Test
public void testName5() throws Exception {
//第一步 获取连接
HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
//第二步 创建查询条件对象
SolrQuery solrQuery = new SolrQuery("*:*");
//第三步,执行查询,获得查询结果
QueryResponse response = solrServer.query(solrQuery);
SolrDocumentList documentList = response.getResults();
System.out.println("一共获得的"+documentList.size()+"条数据");
for (SolrDocument solrDocument : documentList) {
Object fieldValue = solrDocument.getFieldValue("id");
System.out.println("id============>"+fieldValue);
Object fieldValue2 = solrDocument.getFieldValue("title");
System.out.println("title==============>"+fieldValue2);
}
}
8.带条件查询所有数据
/**
* 带条件进行查询
*/
@Test
public void testName6() throws Exception {
//第一步 获取连接器
HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
//第二步 设置查询条件
SolrQuery solrQuery = new SolrQuery("title:成功");
//第三步 查询 得到相应数据
QueryResponse response = solrServer.query(solrQuery);
//第四步 转换成结果集
SolrDocumentList documentList = response.getResults();
System.out.println("设置查询条件后一共有"+documentList.size()+"条数据");
for (SolrDocument solrDocument : documentList) {
System.out.println("id==========>"+solrDocument.getFieldValue("id"));
System.out.println("title=======>"+solrDocument.getFieldValue("title"));
System.out.println("title=======>"+solrDocument.getFieldValue("name"));
System.out.println("title=======>"+solrDocument.getFieldValue("text"));
}
}
9.查询的数据以javabean的方式返回
/**
* 查询的数据以javabean的方式返回
*/
@Test
public void testName7() throws Exception {
//第一步.连接器
HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
//第二步,查询条件
SolrQuery solrQuery = new SolrQuery("*:*");
//第三的.得到响应数据
QueryResponse response = solrServer.query(solrQuery);
//第四步,解析为结果集
List- list = response.getBeans(Item.class);
System.out.println("返回的数据个数:"+list.size());
for (Item item : list) {
System.out.println("Id:"+item.getId());
System.out.println("Title:"+item.getTitle());
}
}
10.查询的结果、SolrQuery实现排序
/**
* 查询的结果、SolrQuery实现排序
*/
@Test
public void testName8() throws Exception {
//1 连接器
HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
//2 查询条件
SolrQuery solrQuery = new SolrQuery("*:*");
solrQuery.setSort("id", ORDER.asc);
//3 查询得到响应数据
QueryResponse response = solrServer.query(solrQuery);
//4 解析得到结果集
List- beans = response.getBeans(Item.class);
System.out.println("返回的数据个数:"+beans.size());
for (Item item : beans) {
System.out.println("Id:"+item.getId());
System.out.println("Title:"+item.getTitle());
}
}
11.查询结果分页
/**
* 查询结果分页
*/
@Test
public void testName9() throws Exception {
// 准备分页参数
int pageNum = 2; //页码
int pageSize = 3; //每页条数
//1 连接
HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
//2 查询条件
SolrQuery query = new SolrQuery("title:*");
query.setSort("id", ORDER.asc);
query.setStart((pageNum-1)*pageSize);
query.setRows(pageSize);
//3 响应数据
QueryResponse response = solrServer.query(query);
//4 解析成为结果集
List- beans = response.getBeans(Item.class);
System.out.println("返回的数据个数:"+beans.size());
for (Item item : beans) {
System.out.println("Id:"+item.getId());
System.out.println("Title:"+item.getTitle());
}
}
12.查询结果高亮显示
/**
* 查询到的结果 SolrQuery实现高亮显示
*/
@Test
public void testName10() throws Exception {
//1 连接
HttpSolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr/core2");
//2 查询条件
SolrQuery query = new SolrQuery("title:oppo");
query.setHighlightSimplePre("");
query.setHighlightSimplePost("");
query.addHighlightField("title");
//3 查询得到响应数据
QueryResponse response = solrServer.query(query);
//4 解析为结果集
List- items = response.getBeans(Item.class);
//5 解析高亮字段
Map
>> highlighting = response.getHighlighting();
System.out.println("返回的数据个数:"+items.size());
for (Item item : items) {
System.out.println("Id:"+item.getId());
System.out.println("Title:"+highlighting.get(item.getId()).get("title").get(0) );
}
}