maven依赖
<dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>7.7.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency>
Person类
package com.zhi.test; import java.io.Serializable; import org.apache.solr.client.solrj.beans.Field; /** * 人员信息 * * @author zhi * @time 2016年12月22日09:55:42 * */ @SuppressWarnings("serial") public class Person implements Serializable { /** * 主键 */ @Field private String id; /** * 人员名称 */ @Field private String name; /** * 地址 */ @Field private String addr; /** * 年龄 */ @Field private Integer age; public Person() { super(); } public Person(String id, String name, String addr, Integer age) { super(); this.id = id; this.name = name; this.addr = addr; this.age = age; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
Datas类
package com.zhi.test; import java.util.ArrayList; import java.util.List; public class Datas { public static List<Person> getPersonData() throws Exception { List<Person> list = new ArrayList<Person>(); list.add(new Person("01", "张三丰", "湖北省浠水县天虹大厦3楼", 100)); list.add(new Person("02", "张无忌", "湖北省武汉市中南路300号", 24)); list.add(new Person("03", "郭靖", "中华人民共和国", 36)); list.add(new Person("04", "黄蓉", "中国浙江", 17)); list.add(new Person("05", "杨过", "中国浙江杭州", 25)); list.add(new Person("06", "周伯通", "中国浙江杭州", 66)); list.add(new Person("07", "黄老邪", "中国浙江杭州", 54)); list.add(new Person("08", "欧阳锋", "湖北省黄石市杭州路900号", 52)); list.add(new Person("09", "木婉清", "湖北省武汉市陆军医院8楼", 19)); list.add(new Person("10", "萧峰", "湖北省武汉汉口解决大道33号", 31)); list.add(new Person("11", "扫地僧", "湖北省武汉市江夏大道99号", 99)); list.add(new Person("12", "丁春秋", "张家界", 43)); return list; } }
SolrTest类
package com.zhi.test; import java.util.List; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.solr.client.solrj.SolrClient; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.client.solrj.response.UpdateResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; import org.apache.solr.common.SolrInputDocument; import org.junit.After; import org.junit.Before; import org.junit.Test; /** * Solr客户端测试,需提前建好core * * @author zhi * @since 2019年9月29日09:02:17 * */ public class SolrTest { private Logger logger = LogManager.getLogger(this.getClass()); private SolrClient solrClient; @Before public void init() { solrClient = new HttpSolrClient.Builder("http://192.168.114.141:8983/solr/").build(); } @After public void destroy() { try { solrClient.close(); } catch (Exception e) { logger.error("关闭SolrClient出错", e); } } /** * 添加一个Document,core中如果定义field,会自动创建对应的field */ @Test public void addDoc() { try { SolrInputDocument document = new SolrInputDocument(); document.addField("id", "1"); document.addField("name", "研发中心"); document.addField("remark", "苦逼集中营"); UpdateResponse response = solrClient.add("dept", document); solrClient.commit("dept"); // 提交,让保存生效 logger.info("保存成功,返回的信息:{}", response.toString()); } catch (Exception e) { logger.error("报错出错", e); } } /** * 查询Document列表 */ @Test public void queryDoc() { try { SolrQuery query = new SolrQuery(); query.set("q", "name:研发"); QueryResponse response = solrClient.query("dept", query); SolrDocumentList list = response.getResults(); logger.info("查询到的数据长度:{}", list.size()); for (SolrDocument doc : list) { logger.info("id={},name={},remark={}", doc.get("id"), doc.get("name"), doc.get("remark")); } } catch (Exception e) { logger.error("查询出错", e); } } /** * 根据ID删除Document */ @Test public void delDocById() { try { UpdateResponse response = solrClient.deleteById("dept", "1"); solrClient.commit("dept"); // 提交,让删除生效 logger.info("删除成功,返回信息:{}", response.toString()); } catch (Exception e) { logger.error("删除出错", e); } } /** * 添加对象列表,Bean对象至少有一个字段加上@Field注解。 * <li>有注解的会自动匹配core中的field(没有会自动生成field) * <li>没注解的不会生成field,也不会保存(即使core中定义了该名称的field) */ @Test public void addBeans() { try { UpdateResponse response = solrClient.addBeans("person", Datas.getPersonData()); solrClient.commit("person"); // 提交,让保存生效 logger.info("保存成功,返回消息:{}", response.toString()); } catch (Exception e) { logger.error("保存出错", e); } } /** * 查询Bean列表,分页查询,按id排序且只返回id、code、name3个字段 */ @Test public void queryBeans1() { try { SolrQuery query = new SolrQuery(); query.set("q", "*:*"); // 设置分页参数 query.setStart(0); query.setRows(5); query.setFields("id", "name", "addr"); // 设置返回字段 query.setSort("id", SolrQuery.ORDER.asc); // 排序 QueryResponse response = solrClient.query("person", query); List<Person> list = response.getBeans(Person.class); logger.info("查询到的数据长度:{}", list.size()); for (Person person : list) { logger.info("id={},name={},addr={}", person.getId(), person.getName(), person.getAddr()); } } catch (Exception e) { logger.error("查询出错", e); } } /** * 查询Bean列表,addr包含 武汉,age范围15到25 */ @Test public void queryBeans2() { try { SolrQuery query = new SolrQuery(); query.set("q", "*:*"); query.addFilterQuery("age:[15 TO 25]"); // 年龄范围过滤 query.addFilterQuery("addr:武汉"); // 地址相似匹配 QueryResponse response = solrClient.query("person", query); List<Person> list = response.getBeans(Person.class); logger.info("查询到的数据长度:{}", list.size()); for (Person person : list) { logger.info("id={},name={},addr={},age={}", person.getId(), person.getName(), person.getAddr(), person.getAge()); } } catch (Exception e) { logger.error("查询出错", e); } } /** * 查询Bean列表,关键字AND、OR必须大写。与queryBeans2结果一致,只是查询时机不一样 */ @Test public void queryBeans3() { try { SolrQuery query = new SolrQuery(); query.set("q", "age:[15 TO 25] AND addr:武汉"); QueryResponse response = solrClient.query("person", query); List<Person> list = response.getBeans(Person.class); logger.info("查询到的数据长度:{}", list.size()); for (Person person : list) { logger.info("id={},name={},addr={}", person.getId(), person.getName(), person.getAddr()); } } catch (Exception e) { logger.error("查询出错", e); } } }