zoukankan      html  css  js  c++  java
  • solr 学习之solrJ

    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条
            
            //开启高亮
            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);
                        }
                    }
                }
            }
        }
  • 相关阅读:
    ZRender实现粒子网格动画实战
    线段树专题—ZOJ1610 Count the Colors
    LeetCode Recover Binary Search Tree
    Android跨进程訪问(AIDL服务)
    刘下记录:ImageView.scaleType 属性全解析(含视频)
    myeclipse解决JSP文件里script背景颜色的调整
    hdu 5381 The sum of gcd(线段树+gcd)
    newlisp HTTP Basic Authentication
    codeforces 132C Logo Turtle--- dp dfs
    python代码风格-PEP8
  • 原文地址:https://www.cnblogs.com/jalja/p/6639114.html
Copyright © 2011-2022 走看看