版权声明:本文为博主原创文章,未经博主允许不得转载
导入所需坐标,注意与低版本HTTPClient冲突
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.3.1</version>
</dependency>
//连接服务器
HttpSolrClient solrClient = new HttpSolrClient.Builder()
//设置solr的地址,http://127.0.0.1:8080/solr为项目路径,car为核的名字
.withBaseSolrUrl(“http://127.0.0.1:8080/solr/car”)
.build();
简单Demo
1.Car.java
package test;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.solr.client.solrj.beans.Field;
/**
*
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Car {
@Field("id")
private String id;
@Field("car_name")
private String carName;
@Field("car_money")
private Double carMoney;
@Field("car_desc")
private String carDesc;
}
2.SolrTest.java
package test;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrInputDocument;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
*
*/
public class SolrTest {
HttpSolrClient solrClient=null;
@Before
public void before(){
//连接服务器
solrClient = new HttpSolrClient.Builder()
//设置solr的地址,http://127.0.0.1:8080/solr为项目路径,car为核的名字
.withBaseSolrUrl("http://127.0.0.1:8080/solr/solr")
.build();
}
@After
public void after() throws IOException, SolrServerException {
//commit提交事务
solrClient.commit();
//关闭连接
solrClient.close();
}
//新增
@Test
public void test1() throws IOException, SolrServerException {
//创建SolrInputDocument对象
SolrInputDocument document=new SolrInputDocument();
//添加字段赋值
document.addField("id","7");
document.addField("car_name","奥迪A5L");
document.addField("car_money",33);
document.addField("car_desc","666");
//调用add方法增加
solrClient.add(document);
}
//修改
@Test
public void test2() throws IOException, SolrServerException {
SolrInputDocument document=new SolrInputDocument();
//添加字段赋值
document.addField("id","7");
document.addField("car_name","大奔");
document.addField("car_money",33);
document.addField("car_desc","666");
//调用add方法增加
solrClient.add(document);
}
//删除
@Test
public void test3() throws IOException, SolrServerException {
solrClient.deleteById("6");
}
//通过实体类新增
@Test
public void test4() throws IOException, SolrServerException {
//创建实体类
Car car=new Car();
car.setId("6");
car.setCarName("劳斯莱斯");
car.setCarMoney(1000.0);
car.setCarDesc("商务车");
//调用addBean方法增加
solrClient.addBean(car);
}
//查询
@Test
public void test5() throws IOException, SolrServerException {
//查询条件对象
SolrQuery query=new SolrQuery();
query.setQuery("*:*");//查询所有
// query.setQuery("car_name:劳斯莱斯");//条件查询,名字是劳斯莱斯的
//query.setFilterQueries("car_money:[700.0 TO *]");//(先查询一下所有)过滤条件,价格大于700
// query.setStart(0).setRows(3);//(先查询一下所有)分页查询
// query.setSort("car_money", SolrQuery.ORDER.desc);//(先查询一下所有)根据价格降序
query.setFields("id,car_name");//(先查询一下所有)查询指定字段
QueryResponse response=solrClient.query(query);
List<Car> list= response.getBeans(Car.class);
System.err.println(list);
}
//高亮查询
@Test
public void test6() throws IOException, SolrServerException {
//查询条件对象
SolrQuery query=new SolrQuery();
query.setQuery("car_name:劳斯莱斯");//查询劳斯莱斯
query.setHighlight(true);//设置为高亮查询
query.setHighlightSimplePre("<font color='blue'>");//高亮词前面添加的内容
query.setHighlightSimplePost("</font>");//高亮词后面添加的内容
query.addHighlightField("car_name");//高亮的字段
QueryResponse response=solrClient.query(query);
List<Car> list= response.getBeans(Car.class);
// System.err.println(list);
//高亮查询
Map<String,Map<String,List<String>>> highLighting=response.getHighlighting();
//将高亮字段给普通数据的字段赋值
//遍历查询的数据
if(list!=null){
for (int i = 0; i <list.size(); i++) {
//得到遍历数据的id
String id=list.get(i).getId();
//根据id拿到相应的高亮内容
String car_name = highLighting.get(id).get("car_name").get(0);
//将高亮内容赋值给普通数据
list.get(i).setCarName(car_name);
}
}
//输出转化为高亮后的结果
System.err.println(list);
}
}