solrJ是访问Solr服务的JAVA客户端,提供索引和搜索的请求方法,SolrJ通常嵌入在业务系统中,通过solrJ的API接口操作Solr服务。
一 .maven的环境jar包配置
<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj --><dependency><groupId>org.apache.solr</groupId><artifactId>solr-solrj</artifactId><version>4.10.4</version></dependency><!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency>
二.solrj向solr缓存服务器中添加数据
public static void addDocument() throws Exception{//创建Solr的客户端链接对象HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");for(int i=3;i<100;i++){//创建一个文档对象SolrInputDocument sd=new SolrInputDocument();//添加域sd.addField("id", UUID.randomUUID());sd.addField("item_title", "商品"+i);sd.addField("item_sell_point", "好看"+i);sd.addField("item_price", 100L);sd.addField("item_desc", "商品"+i+"这个东西很不错啊");sd.addField("item_image", "2"+i+".jpg");sd.addField("item_category_name", "分类"+i);solrServer.add(sd);solrServer.commit();}}
三.solrj进行删除操作
//根据document的Id直接删除public static void deleteDocument() throws Exception{//创建Solr的客户端链接对象HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");solrServer.deleteById("8ceee0a5-52ee-43b6-88ba-249b02c8279c");solrServer.commit();}//根据条件查询删除public static void deleteQueryDocument() throws Exception{//创建Solr的客户端链接对象HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");//查询删除solrServer.deleteByQuery("item_title:商品1");solrServer.commit();}
四.solorj进行查询操作
public static void queryDocument() throws Exception{//创建Solr的客户端链接对象HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");//创建solr的查询对象SolrQuery sq=new SolrQuery();//设置查询条件sq.set("q","item_title:3" );//查询QueryResponse qr=solrServer.query(sq);//获取查询结果SolrDocumentList sds=qr.getResults();//获取查询的记录数long total=sds.getNumFound();System.out.println("数量:"+total);for(SolrDocument sd:sds){//默认取出10条记录String id=(String) sd.getFieldValue("id");String item_title=(String) sd.getFieldValue("item_title");String item_sell_point=(String) sd.getFieldValue("item_sell_point");long item_price=(Long) sd.getFieldValue("item_price");String item_desc=(String) sd.getFieldValue("item_desc");String item_image=(String) sd.getFieldValue("item_image");String item_category_name=(String) sd.getFieldValue("item_category_name");System.out.println("========================================");System.out.println("id:"+id);System.out.println("item_title:"+item_title);System.out.println("item_sell_point:"+item_sell_point);System.out.println("item_price:"+item_price);System.out.println("item_desc:"+item_desc);System.out.println("item_image:"+item_image);System.out.println("item_category_name:"+item_category_name);}}
查询时用到的一些基本语法
1.设置查询条件
//设置查询条件
sq.set("q","item_title:3 AND item_desc:东西 OR item_sell_point:好看" );
2.设置过滤条件
//设置过滤条件sq.set("fq", "item_price:[1 TO 20]");
3.设置排序方式
//设置排序sq.addSort("item_title", ORDER.desc);
4.查询数据分页处理
//设置分页sq.setStart(0);//开始位置sq.setRows(3);//每页3条
5.查询结果高亮展示
public static void queryDocument() throws Exception{//创建Solr的客户端链接对象HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");//创建solr的查询对象SolrQuery sq=new SolrQuery();//设置查询条件sq.set("q","item_title:商品" );//设置过滤条件// sq.set("fq", "item_price:[1 TO 20]");//设置排序sq.addSort("item_title", ORDER.desc);//设置分页sq.setStart(0);//开始位置sq.setRows(3);//每页3条//开启高亮sq.setHighlight(true);sq.addHighlightField("item_title");//设置高亮域sq.setHighlightSimplePre("<b>");//设置高亮样式sq.setHighlightSimplePost("</b>");//查询QueryResponse qr=solrServer.query(sq);//获取查询结果SolrDocumentList sds=qr.getResults();//获取查询的记录数long total=sds.getNumFound();System.out.println("数量:"+total);for(SolrDocument sd:sds){//默认取出10条记录String id=(String) sd.getFieldValue("id");String item_title=(String) sd.getFieldValue("item_title");String item_sell_point=(String) sd.getFieldValue("item_sell_point");long item_price=(Long) sd.getFieldValue("item_price");String item_desc=(String) sd.getFieldValue("item_desc");String item_image=(String) sd.getFieldValue("item_image");String item_category_name=(String) sd.getFieldValue("item_category_name");System.out.println("========================================");System.out.println("id:"+id);System.out.println("item_title:"+item_title);System.out.println("item_sell_point:"+item_sell_point);System.out.println("item_price:"+item_price);System.out.println("item_desc:"+item_desc);System.out.println("item_image:"+item_image);System.out.println("item_category_name:"+item_category_name);//获取高亮显示的结构Map<String, Map<String, List<String>>> highlighting=qr.getHighlighting();if(highlighting!=null){//根据Id获得每个域的高亮内容Map<String, List<String>> map=highlighting.get(id);//根据具体的域获取高亮内容List<String> list=map.get("item_title");if(list!=null && !list.isEmpty()){for(String str:list){System.out.println("str:"+str);}}}}}