solr 学习之solrJ

转自:https://www.cnblogs.com/jalja/p/6639114.html

solrJ是访问Solr服务的JAVA客户端,提供索引和搜索的请求方法,SolrJ通常嵌入在业务系统中,通过solrJ的API接口操作Solr服务。

 

复制代码
 <!-- 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>
复制代码

一、添加数据

复制代码
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();
        }
    }
复制代码

二、删除

复制代码
//根据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();
    }
复制代码

三、查询

复制代码
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条
    </span><span style="color: #008000">//</span><span style="color: #008000">开启高亮</span>
    sq.setHighlight(<span style="color: #0000ff">true</span><span style="color: #000000">);
    sq.addHighlightField(</span>"item_title");<span style="color: #008000">//</span><span style="color: #008000">设置高亮域</span>
    sq.setHighlightSimplePre("&lt;b&gt;");<span style="color: #008000">//</span><span style="color: #008000">设置高亮样式</span>
    sq.setHighlightSimplePost("&lt;/b&gt;"<span style="color: #000000">);
    </span><span style="color: #008000">//</span><span style="color: #008000">查询</span>
    QueryResponse qr=<span style="color: #000000">solrServer.query(sq);
    </span><span style="color: #008000">//</span><span style="color: #008000">获取查询结果</span>
    SolrDocumentList sds=<span style="color: #000000">qr.getResults();
    </span><span style="color: #008000">//</span><span style="color: #008000">获取查询的记录数</span>
    <span style="color: #0000ff">long</span> total=<span style="color: #000000">sds.getNumFound();
    System.out.println(</span>"数量:"+<span style="color: #000000">total);
    </span><span style="color: #0000ff">for</span>(SolrDocument sd:sds){<span style="color: #008000">//</span><span style="color: #008000">默认取出10条记录</span>
        String id=(String) sd.getFieldValue("id"<span style="color: #000000">);
        String item_title</span>=(String) sd.getFieldValue("item_title"<span style="color: #000000">);
        String item_sell_point</span>=(String) sd.getFieldValue("item_sell_point"<span style="color: #000000">);
        </span><span style="color: #0000ff">long</span> item_price=(Long) sd.getFieldValue("item_price"<span style="color: #000000">);
        String item_desc</span>=(String) sd.getFieldValue("item_desc"<span style="color: #000000">);
        String item_image</span>=(String) sd.getFieldValue("item_image"<span style="color: #000000">);
        String item_category_name</span>=(String) sd.getFieldValue("item_category_name"<span style="color: #000000">);
        System.out.println(</span>"========================================"<span style="color: #000000">);
        System.out.println(</span>"id:"+<span style="color: #000000">id);
        System.out.println(</span>"item_title:"+<span style="color: #000000">item_title);
        System.out.println(</span>"item_sell_point:"+<span style="color: #000000">item_sell_point);
        System.out.println(</span>"item_price:"+<span style="color: #000000">item_price);
        System.out.println(</span>"item_desc:"+<span style="color: #000000">item_desc);
        System.out.println(</span>"item_image:"+<span style="color: #000000">item_image);
        System.out.println(</span>"item_category_name:"+<span style="color: #000000">item_category_name);
        </span><span style="color: #008000">//</span><span style="color: #008000">获取高亮显示的结构</span>
        Map&lt;String, Map&lt;String, List&lt;String&gt;&gt;&gt; highlighting=<span style="color: #000000">qr.getHighlighting();
        </span><span style="color: #0000ff">if</span>(highlighting!=<span style="color: #0000ff">null</span><span style="color: #000000">){
            </span><span style="color: #008000">//</span><span style="color: #008000">根据Id获得每个域的高亮内容</span>
            Map&lt;String, List&lt;String&gt;&gt; map=<span style="color: #000000">highlighting.get(id);
            </span><span style="color: #008000">//</span><span style="color: #008000">根据具体的域获取高亮内容</span>
            List&lt;String&gt; list=map.get("item_title"<span style="color: #000000">);
            </span><span style="color: #0000ff">if</span>(list!=<span style="color: #0000ff">null</span> &amp;&amp; !<span style="color: #000000">list.isEmpty()){
                </span><span style="color: #0000ff">for</span><span style="color: #000000">(String str:list){
                    System.out.println(</span>"str:"+<span style="color: #000000">str);
                }
            }
        }
    }
}</span></pre>
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值