solr学习之solrj

这篇博客介绍了如何在Java环境中使用SolrJ客户端进行Solr服务的索引、搜索、删除和查询操作。详细讲解了SolrJ的Maven配置,以及添加数据、删除数据、查询数据时的基本语法,包括查询条件、过滤条件、排序方式、分页处理和高亮显示。
摘要由CSDN通过智能技术生成

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);
                    }
                }
            }
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值