一、准备工作
1.1 清空之前的测试数据
1.2 IDEA创建项目
1.3 修改pom.xml加入相依赖
<properties> <solrj.version>7.7.2</solrj.version> </properties> <dependencies> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>${solrj.version}</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> </dependencies>
1.4 创建Test01Connection测试
public class Test01Connection { //声明一个连接solr的地址 ip:port/solr/库名 public static final String SOLR_URL="http://ip:8983/solr/db2-core"; //声明一个连接solr的对象 private static HttpSolrClient httpSolrClient; static { httpSolrClient=new HttpSolrClient.Builder(SOLR_URL).build(); } public static void main(String[] args) { System.out.println(httpSolrClient); } }
二、增删查
2.1 添加数据
package com.wang.solr; import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.common.SolrInputDocument; import java.io.IOException; import java.util.ArrayList; import java.util.List; /** * @author 王一宁 * @date 2020/7/14 16:32 */ public class Test02AddData { //声明一个连接solr的地址 ip:port/solr/库名 public static final String SOLR_URL="http://47.94.39.202:8983/solr/db2-core"; //声明一个连接solr的对象 private static HttpSolrClient httpSolrClient; static { httpSolrClient=new HttpSolrClient.Builder(SOLR_URL).build(); } public static void main(String[] args) throws IOException, SolrServerException { //一个一个添加 /*SolrInputDocument doc=new SolrInputDocument(); doc.addField("id",1); doc.addField("goods_name","尚品宅配 定制衣柜 欧式移门推拉门整体大衣柜 卧室柜衣帽间 全屋家具定制 订金(非实际价格)"); doc.addField("goods_remark","该商品购买1-100件时享受单件价¥608.00,超出数量以结算价为准,仅限购买一次"); httpSolrClient.add(doc); httpSolrClient.commit(); httpSolrClient.close();*/ //一个集合一个集合的添加 List<SolrInputDocument> docs=new ArrayList<>(); for (int i = 1; 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.commit(); httpSolrClient.close(); System.out.println("操作成功"); } }
2.2 指定库添加数据
2.3 删除数据
/** * @author 王一宁 * @date 2020/7/14 16:32 */ public class Test03DeleteData { //声明一个连接solr的地址 ip:port/solr/库名 public static final String SOLR_URL="http://47.94.39.202:8983/solr/db2-core"; //声明一个连接solr的对象 private static HttpSolrClient httpSolrClient; static { httpSolrClient=new HttpSolrClient.Builder(SOLR_URL).build(); } public static void main(String[] args) throws IOException, SolrServerException { // 根据ID删除 //httpSolrClient.deleteById("1"); // 根据IDS删除 //httpSolrClient.deleteById(Arrays.asList("1","2","3")); // 根据ID删除指定库的数据 //httpSolrClient.deleteById("db1-core","1"); // 全部删除 httpSolrClient.deleteByQuery("*:*"); httpSolrClient.commit(); httpSolrClient.close(); } }
三、添加自定义字段并添加数据
3.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"/>
3.2 修改后重启动solr查看
3.3 添加数据
/** * @author 王一宁 * @date 2020/7/14 16:32 */ public class Test05AddCustData { //声明一个连接solr的地址 ip:port/solr/库名 public static final String SOLR_URL="http://47.94.39.202:8983/solr/db2-core"; //声明一个连接solr的对象 private static HttpSolrClient httpSolrClient; static { httpSolrClient=new HttpSolrClient.Builder(SOLR_URL).build(); } public static void main(String[] args) throws IOException, SolrServerException { //一个集合一个集合的添加 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); httpSolrClient.commit(); httpSolrClient.close(); System.out.println("操作成功"); } }
查看数据是否添加成功
3.4 使用注解的方式添加【不推荐】
创建Person
@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; }
测试
public class Test06AddDataAnnoation { //声明一个连接solr的地址 ip:port/solr/库名 public static final String SOLR_URL="http://47.94.39.202:8983/solr/db2-core"; //声明一个连接solr的对象 private static HttpSolrClient httpSolrClient; static { httpSolrClient=new HttpSolrClient.Builder(SOLR_URL).build(); } public static void main(String[] args) throws IOException, SolrServerException { //一个集合一个集合的添加 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.addBeans(persons); httpSolrClient.commit(); httpSolrClient.close(); System.out.println("操作成功"); } }
四、全查询
public class Test06QueryAll { //声明一个连接solr的地址 ip:port/solr/库名 public static final String SOLR_URL="http://47.94.39.202:8983/solr/db2-core"; //声明一个连接solr的对象 private static HttpSolrClient httpSolrClient; static { httpSolrClient=new HttpSolrClient.Builder(SOLR_URL).build(); } public static void main(String[] args) throws IOException, SolrServerException { String keywords = "*:*"; SolrQuery solrQuery = new SolrQuery(keywords); QueryResponse query = httpSolrClient.query(solrQuery); SolrDocumentList results = query.getResults(); System.out.println("总条数:"+results.getNumFound()); for (SolrDocument result : results){ System.out.println(result); } httpSolrClient.commit(); httpSolrClient.close(); System.out.println("操作成功"); } }
五、分页查询
public class Test06QueryForPage { //声明一个连接solr的地址 ip:port/solr/库名 public static final String SOLR_URL="http://47.94.39.202:8983/solr/db2-core"; //声明一个连接solr的对象 private static HttpSolrClient httpSolrClient; static { httpSolrClient=new HttpSolrClient.Builder(SOLR_URL).build(); } public static void main(String[] args) throws IOException, SolrServerException { String keywords = "*:*"; SolrQuery solrQuery = new SolrQuery(keywords); int page = 1; int limit = 3; //从哪里开始查询 solrQuery.setStart((page-1)*limit); //设置每页显示多少数据 solrQuery.setRows(limit); QueryResponse query = httpSolrClient.query(solrQuery); SolrDocumentList results = query.getResults(); System.out.println("总条数:"+results.getNumFound()); for (SolrDocument result : results){ System.out.println(result); } httpSolrClient.commit(); httpSolrClient.close(); System.out.println("操作成功"); } }
六、排序查询
public class Test08QuerySort { //声明一个连接solr的地址 ip:port/solr/库名 public static final String SOLR_URL="http://ips:8983/solr/db2-core"; //声明一个连接solr的对象 private static HttpSolrClient httpSolrClient; static { httpSolrClient=new HttpSolrClient.Builder(SOLR_URL).build(); } public static void main(String[] args) throws IOException, SolrServerException { String keywords="*:*"; //创建一个查询条件对象 SolrQuery solrQuery=new SolrQuery(keywords); // solrQuery.setSort("id",SolrQuery.ORDER.desc); //只能根据一个属性进行排序 solrQuery.setSort("age",SolrQuery.ORDER.asc); //查询 QueryResponse query = httpSolrClient.query(solrQuery); //取出结果 SolrDocumentList results = query.getResults(); System.out.println("总条数:"+results.getNumFound()); for (SolrDocument result : results) { System.out.println(result); } } }
七、范围查询
public class Test10QueryRange { //声明一个连接solr的地址 ip:port/solr/库名 public static final String SOLR_URL="http://ip:8983/solr/db2-core"; //声明一个连接solr的对象 private static HttpSolrClient httpSolrClient; static { httpSolrClient=new HttpSolrClient.Builder(SOLR_URL).build(); } public static void main(String[] args) throws IOException, SolrServerException { //创建条件 SolrQuery solrQuery=new SolrQuery("*:*"); //限定范围 20-40岁之间的 solrQuery.setFilterQueries("age:[20 TO 40]"); QueryResponse query = httpSolrClient.query(solrQuery); //取出结果 SolrDocumentList results = query.getResults(); System.out.println("总条数:"+results.getNumFound()); for (SolrDocument result : results) { System.out.println(result); } } }
八、过滤查询
public class Test11QueryFilter { //声明一个连接solr的地址 ip:port/solr/库名 public static final String SOLR_URL="ip:8983/solr/db2-core"; //声明一个连接solr的对象 private static HttpSolrClient httpSolrClient; static { httpSolrClient=new HttpSolrClient.Builder(SOLR_URL).build(); } public static void main(String[] args) throws IOException, SolrServerException { //创建条件 SolrQuery solrQuery=new SolrQuery("*:*"); //限定条件 年龄=33 solrQuery.setFilterQueries("age:33"); QueryResponse query = httpSolrClient.query(solrQuery); //取出结果 SolrDocumentList results = query.getResults(); System.out.println("总条数:"+results.getNumFound()); for (SolrDocument result : results) { System.out.println(result); } } }