目录
1. 准备工作
1.1 solrj依赖
<dependencies>
<!--solr的javaAPI依赖-->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>7.7.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>compile</scope>
</dependency>
</dependencies>
1.2 工具类
import org.apache.solr.client.solrj.impl.HttpSolrClient;
/**
* 建立solr连接工具类
*/
public class HttpSolrClientUtils {
//声明一个连接solr的地址 ip:port/solr/库名
private static final String SOLR_URL="http://192.168.236.128:8983//solr/";
private static final String DATA_BASE="db2-core";
private static HttpSolrClient httpSolrClient;
/**
* 获取solr连接对象
* @return
*/
public static HttpSolrClient getHttpSolrClient(){
//声明一个连接solr的对象
httpSolrClient = new HttpSolrClient.Builder(SOLR_URL+DATA_BASE).build();
return httpSolrClient;
}
public static void commitAndClose() throws Exception {
httpSolrClient.commit();
httpSolrClient.close();
}
public static void main(String[] args) {
System.out.println(HttpSolrClientUtils.getHttpSolrClient());
}
}
红色异常,可以引入slf4j的实现包,如slf4j-log4j12.jar,引入后再启动项目就可以看到不在报错了。另外使用log4j2的话,可以引入log4j-slf4j-impl-2.10.0.jar包,这是log4j对slf4j相关方法的实现。
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
2. 添加数据
2.1 代码:
/**
* 添加数据
*/
public class Test02AddData {
/**
* 添加单条数据
* @throws Exception
*/
@Test
public void addOneData() throws Exception {
//获取solr连接对象
HttpSolrClient httpSolrClient = HttpSolrClientUtils.getHttpSolrClient();
//定义文档对象
SolrInputDocument doc = new SolrInputDocument();
//设置字段属性
doc.addField("id",1);
doc.addField("goods_name","尚品宅配 定制衣柜 欧式移门推拉门整体大衣柜 卧室柜衣帽间 全屋家具定制 订金(非实际价格)");
doc.addField("goods_remark","该商品购买1-100件时享受单件价¥608.00,超出数量以结算价为准,仅限购买一次");
httpSolrClient.add(doc);
HttpSolrClientUtils.commitAndClose();
}
/**
* 添加集合数据
* @throws Exception
*/
@Test
public void addListData() throws Exception{
HttpSolrClient httpSolrClient = HttpSolrClientUtils.getHttpSolrClient();
List<SolrInputDocument> docs=new ArrayList<>();
for (int i = 2; i <=5; i++) {
SolrInputDocument dox=new SolrInputDocument();
dox.addField("id",i);
dox.addField("goods_name","锤子手机"+i);
dox.addField("goods_remark","坚果Pro3---"+i);
docs.add(dox);
}
httpSolrClient.add(docs);
// httpSolrClient.add("db1_core",docs); //向某一个库添加数据
HttpSolrClientUtils.commitAndClose();
System.out.println("操作成功");
}
}
2.2 结果:
3. 删除数据
3.1 代码:
/**
* 删除数据
*/
public class Test03DeleteData {
/**
* 根据ID删除
* @throws Exception
*/
@Test
public void deleteById() throws Exception{
HttpSolrClient httpSolrClient = HttpSolrClientUtils.getHttpSolrClient();
httpSolrClient.deleteById("1");
// httpSolrClient.deleteById("db1-core","1"); //根据ID删除指定库的数据
HttpSolrClientUtils.commitAndClose();
System.out.println("操作成功!");
}
/**
* 根据IDS删除
* @throws Exception
*/
@Test
public void deleteByIds() throws Exception{
HttpSolrClient httpSolrClient = HttpSolrClientUtils.getHttpSolrClient();
List<String> ids = Arrays.asList("2", "3");
httpSolrClient.deleteById(ids);
HttpSolrClientUtils.commitAndClose();
System.out.println("操作成功!");
}
/**
* 全部删除
* @throws Exception
*/
@Test
public void deleteAll() throws Exception{
HttpSolrClient httpSolrClient = HttpSolrClientUtils.getHttpSolrClient();
httpSolrClient.deleteByQuery("*:*");
HttpSolrClientUtils.commitAndClose();
System.out.println("操作成功!");
}
}
3.2 结果:
4. 添加自定义字段,并添加数据
4.1 修改managed-schema
<!--自定义Person的字段-->
<field name="name" type="text_cn" indexed="true" required="true" stored="true" multiValued="false"/>
<field name="age" type="pint" indexed="true" stored="true" multiValued="false"/>
<field name="sex" type="text_cn" indexed="false" stored="true" multiValued="false"/>
<field name="salary" type="pdouble" indexed="true" stored="true" multiValued="false"/>
<field name="remark" type="text_cn" indexed="true" stored="true" multiValued="false"/>
4.2 重启solr,并查看结果
4.3 添加数据
/**
* 修改managed-schema之后添加数据
*/
public class Test04AddListData {
@Test
public void addListData() throws Exception{
HttpSolrClient httpSolrClient = HttpSolrClientUtils.getHttpSolrClient();
//一个集合一个集合的添加
List<SolrInputDocument> persons = new ArrayList<>(5) ;
SolrInputDocument lvbu = new SolrInputDocument();
lvbu.addField("id","1");
lvbu.addField("name","吕布");
lvbu.addField("age",33);
lvbu.addField("sex","男");
lvbu.addField("salary","8888.88");
lvbu.addField("remark","人中吕布,马中赤兔");
SolrInputDocument zhaoyun = new SolrInputDocument();
zhaoyun.addField("id","2");
zhaoyun.addField("name","赵云");
zhaoyun.addField("age",28);
zhaoyun.addField("sex","男");
zhaoyun.addField("salary","8888.88");
zhaoyun.addField("remark","七进七出");
SolrInputDocument guanyu = new SolrInputDocument();
guanyu.addField("id","3");
guanyu.addField("name","关羽");
guanyu.addField("age",44);
guanyu.addField("sex","男");
guanyu.addField("salary","9999.88");
guanyu.addField("remark","忠肝义胆");
SolrInputDocument zhangfei = new SolrInputDocument();
zhangfei.addField("id","4");
zhangfei.addField("name","张飞");
zhangfei.addField("age",41);
zhangfei.addField("sex","男");
zhangfei.addField("salary","8888.88");
zhangfei.addField("remark","莽夫一个");
SolrInputDocument liubei = new SolrInputDocument();
liubei.addField("id","5");
liubei.addField("name","刘备");
liubei.addField("age",48);
liubei.addField("sex","男");
liubei.addField("salary","99999.88");
liubei.addField("remark","心机婊");
persons.add(lvbu) ;
persons.add(zhaoyun) ;
persons.add(guanyu) ;
persons.add(zhangfei) ;
persons.add(liubei) ;
httpSolrClient.add(persons);
HttpSolrClientUtils.commitAndClose();
System.out.println("操作成功");
}
}
4.4 查看结果
5. 使用注解的方式添加
5.1 添加注解实体类
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.solr.client.solrj.beans.Field;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
@Field("id")
private Integer id;
@Field("name")
private String name;
@Field("age")
private Integer age;
@Field("sex")
private String sex;
@Field("salary")
private Double salary;
@Field("remark")
private String remark;
}
5.2 测试代码
public class Test06AddCustDataAnnotation {
@Test
public void testAddCustDataAnnotation() throws Exception{
HttpSolrClient httpSolrClient = HttpSolrClientUtils.getHttpSolrClient();
//一个集合一个集合的添加
List<Person> persons = new ArrayList<>(5) ;
persons.add(new Person(1,"吕布",33,"男",8888.88,"人中吕布"));
persons.add(new Person(2,"赵云",28,"男",9888.88,"七进七出"));
persons.add(new Person(3,"关羽",44,"男",10888.88,"忠肝义胆"));
persons.add(new Person(4,"张飞",41,"男",11888.88,"莽夫一个"));
persons.add(new Person(5,"刘备",48,"男",12888.88,"心机婊"));
// httpSolrClient.addBean(new Person())
httpSolrClient.addBeans(persons);
HttpSolrClientUtils.commitAndClose();
System.out.println("操作成功");
}
}
5.3 查看结果