zoukankan      html  css  js  c++  java
  • 使用solrJ操作solr常用方法 【注释非常详细,非常好】

    转:

    使用solrJ操作solr常用方法

    版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37334135/article/details/76862508

    既然学的是java那么肯定需要用java代码来进行对solr的操作,如果知道在solr后台管理界面进行增删改查等操作,那么用solrJ操作solr会更好理解。

    solrJ介绍
    solrJ是一个用来访问solr的java客户端,提供了索引和搜索的方法(将一些常用的命令封装进去了),通过solrJ提供的API 接口来操作solr服务。
    这里写图片描述

    准备工作
    创建个maven工程(普通的java工程都可以不过需要自己导包),添加依赖如下:

    <dependencies>
        <dependency>
            <groupId>org.apache.solr</groupId>
            <artifactId>solr-solrj</artifactId>
            <version>4.10.2</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.2</version>
        </dependency>
      </dependencies>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    注:有的大神介绍说添加第一个solrj的依赖就行,但是我这里测试的时候发现还是需要commons-logging的依赖的,另外,如果你本地仓库没有zookeeper-3.4.6.jar跟slf4j-api-1.7.6.jar也会直接报错,自己去网上下一个吧,最终包结构我也截了个图。
    这里写图片描述

    然后启动solr服务器,接下来进行操作。
    1、添加文档

        /*
         * 测试向索引库中添加文档
         */
        @Test
        public void testSave() throws Exception{
            //1.创建连接对象
            SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
            //2.创建一个文档对象
            SolrInputDocument inputDocument = new SolrInputDocument();
            //向文档中添加域以及对应的值,注意:所有的域必须在schema.xml中定义过,前面已经给出过我定义的域。
            inputDocument.addField("id", "1");
            inputDocument.addField("item_title", "sansung爆炸牌手机");
            inputDocument.addField("item_price", 666);
            inputDocument.addField("item_image", "www.boom.png");
            //3.将文档写入索引库中
            solrServer.add(inputDocument);
            //提交
            solrServer.commit();
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    后台管理界面查询id为1的商品看看
    这里写图片描述
    2、修改文档

        /*
         * 测试修改索引库中已存在的文档
         */
        @Test
        public void testUpdate() throws Exception{
            //1.创建连接对象
            SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
            //2.创建一个文档对象
            SolrInputDocument inputDocument = new SolrInputDocument();
            inputDocument.addField("id", "1");
            //修改id为1的商品的信息(如果该商品不存在其实就是添加了)
            inputDocument.addField("item_title", "vivo手机hahaha");
            inputDocument.addField("item_price", 6666);
            inputDocument.addField("item_image", "www.123.png");
            //3.将文档写入索引库中
            solrServer.add(inputDocument);
            //提交
            solrServer.commit();
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    再去后台管理页面查询id为1的商品看看
    这里写图片描述
    3、删除文档

        /*
         * 测试删除文档:根据id删除文档 *
         */
        @Test
        public void testDeleteById() throws Exception{
            SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
            //删除文档
            solrServer.deleteById("1");
            //提交
            solrServer.commit();
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
        /*
         * 测试删除文档:根据查询结果删除文档(重新添加id为1的文档)
         */
        @Test
        public void testDeleteByQ() throws Exception{
            SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
            //根据查询结果删除文档,注意:用item_image的查询结果来进行删除是不行的
            //因为制定业务域的时候indexed=false,即不被索引,此时是不能根据图片来查询的。
            solrServer.deleteByQuery("item_title:vivo手机hahaha");
            solrServer.commit();
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    不管是根据id删除还是根据查询删除都能达到一样的效果。
    4、查询索引

    1、简单根据id查询索引

        /*
         * 简单查询:查询单个商品信息
         */
        @Test
        public void testSimpleQ() throws Exception{
            //1.创建连接
            SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
            //2.创建查询语句
            SolrQuery query = new SolrQuery();
            //3.设置查询条件
            query.set("q", "id:1");
            //4.执行查询
            QueryResponse queryResponse = solrServer.query(query);
            //5.取文档列表public class SolrDocumentList extends ArrayList<SolrDocument>
            SolrDocumentList documentList = queryResponse.getResults();
            for (SolrDocument solrDocument : documentList) {
                //取各个文档信息
                System.out.println("商品id:"+solrDocument.get("id")+" ");
                System.out.println("商品标题:"+solrDocument.get("item_title")+" ");
                System.out.println("商品价格:"+solrDocument.get("item_price")+" ");
                System.out.println("商品图片:"+solrDocument.get("item_image")+" ");
            }
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    输出结果:
    商品id:1 
    商品标题:vivo手机hahaha 
    商品价格:6666 
    商品图片:www.123.png 
    • 1
    • 2
    • 3
    • 4
    • 5

    2、设置条件进行查询

    @Test
        public void testSimpleQ2 () throws Exception{
            //1.创建连接
            SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
            //2.创建查询语句
            SolrQuery query = new SolrQuery();
            //3.设置查询条件
            query.set("q", "夏普");//设置查询关键字
            query.setSort("id", ORDER.desc);//按照id降序排列
            query.setStart(1);
            query.setRows(3);//分页条件
            query.set("df", "item_title");//默认在商品标题域进行查询
            //4、执行查询
            QueryResponse queryResponse = solrServer.query(query);
            //5.获取文档列表
            SolrDocumentList documentList = queryResponse.getResults();
            //获取总记录数
            long numFound = documentList.getNumFound();
            System.out.println("总记录数:" + numFound);
            for (SolrDocument solrDocument : documentList) {
                //取各个文档信息
                System.out.print("商品id:"+solrDocument.get("id")+" ");
                System.out.print("商品标题:"+solrDocument.get("item_title")+" ");
                System.out.print("商品价格:"+solrDocument.get("item_price")+" ");
                System.out.println();
            }
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    注:如果设置了默认查询域的话,自己又不想在这个域内查那么在查询关键字前要注明要在哪个域查,比如:”item_price:600”

    输出结果:
    总记录数:19
    商品id:936920 商品标题:夏普(SHARP)LCD-52DS70A 52英寸 日本原装液晶面板 3D Android操作系统智能液晶电视 商品价格:699900 
    商品id:816753 商品标题:夏普(SHARP)LCD-46DS40A 46英寸 日本原装液晶面板 智能全高清液晶电视 商品价格:379900 
    商品id:1356054 商品标题:夏普(SHARP)LCD-50DS72A 50英寸 无线网络 安卓智能 4K超高清液晶电视 商品价格:549900 
    • 1
    • 2
    • 3
    • 4
    • 5

    3、带高亮的复杂查询

        /*
         * 测试复杂查询:取高亮
         */
        @Test
        public void testHighLighting() throws Exception{
            //1、创建连接
            SolrServer solrServer = new HttpSolrServer("http://192.168.25.128:8080/solr/collection1");
            //2、创建查询语句
            SolrQuery query = new SolrQuery();
            //3、设置查询条件
            query.set("q", "夏普");//设置查询关键字
            query.setSort("id", ORDER.desc);//按照id降序排列
            query.setStart(1);
            query.setRows(5);//分页条件
            query.set("df", "item_title");
    
            //开启高亮显示
            query.setHighlight(true);
            query.setHighlightSimplePre("<em>");
            query.setHighlightSimplePost("</em>");
    
            //4、执行查询
            QueryResponse queryResponse = solrServer.query(query);
            //5、取高亮
            Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
            //6、获取文档列表
            SolrDocumentList documentList = queryResponse.getResults();
            //7、遍历查询结果
            for (SolrDocument solrDocument : documentList) {
                System.out.print(solrDocument.get("id")+" ");
                List<String> titleList = highlighting.get(solrDocument.get("id")).get("item_title");
                if (titleList !=null && titleList.size()>0) {
                    //能取到高亮,输出高亮
                    System.out.print(titleList.get(0));
                }
                System.out.println();
            }
    
        }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    输出结果:
    936920 <em>夏普</em>(SHARP)LCD-52DS70A 52英寸 日本原装液晶面板 3D Android操作系统智能液晶电视
    816753 <em>夏普</em>(SHARP)LCD-46DS40A 46英寸 日本原装液晶面板 智能全高清液晶电视
    1356054 <em>夏普</em>(SHARP)LCD-50DS72A 50英寸 无线网络 安卓智能 4K超高清液晶电视
    1322968 <em>夏普</em>(SHARP)LCD-40DS13A 40英寸液晶电视
    1322963 <em>夏普</em>(SHARP)LCD-32DS13A 32英寸液晶电视
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    如果看了关于在solr后台管理页面进行的相关操作后,再看这个solrJ应该就瞬间会了。

  • 相关阅读:
    JSpider(1):配置与运行
    JSpider(4):Tasks,Events&Visitors
    WebUI Case(1): www.swtdesigner.com 首页 (续)
    java web开发_购物车功能实现
    java web开发_多拿网/淘宝网购物车选择操作
    java开发_快速搜索本地文件_小应用程序
    lucene(全文搜索)_恢复/更新索引操作
    lucene(全文搜索)_根据内容建立索引_源码下载
    程序员,都去写一写前端代码吧
    javascript_时间自动刷新
  • 原文地址:https://www.cnblogs.com/libin6505/p/10467394.html
Copyright © 2011-2022 走看看