zoukankan      html  css  js  c++  java
  • Solr系列4-SolrJ开发应用

    1: Solr导入

    1.1导入POMStar

    # Base Code Java org.apache.solr solr-solrj 8.4.0 # spring boot org.springframework.boot spring-boot-starter-data-solr

    # Base Code Java
    <dependency>
      <groupId>org.apache.solr</groupId>
      <artifactId>solr-solrj</artifactId>
      <version>8.4.0</version>
    </dependency>
    # spring boot
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-solr</artifactId>
    </dependency>

    2 SolrJ概述

    对Solr的所有请求均由发送SolrClient

    SolrClient 有一些具体的实现,每个实现都针对不同的使用模式或弹性模型:

    • HttpSolrClient-面向以查询为中心的工作负载,尽管它也是一个很好的通用客户端。直接与单个Solr节点通信。
    • Http2SolrClient-利用HTTP / 2的异步,非阻塞和通用客户端。此类是实验性的,因此它的API可能会在次要版本的SolrJ中更改或删除。
    • LBHttpSolrClient-在Solr节点列表之间平衡请求负载。根据节点运行状况调整“服务中”节点的列表。
    • LBHttp2SolrClient-就像LBHttpSolrClientHttp2SolrClient改用。此类是实验性的,因此它的API可能会在次要版本的SolrJ中更改或删除。
    • CloudSolrClient-旨在与SolrCloud部署进行通信。使用已经记录的ZooKeeper状态来发现请求并将其路由到健康的Solr节点。
    • ConcurrentUpdateSolrClient-面向以索引为中心的工作负载。在将较大的批次发送到Solr之前,在内部缓冲文档。
    • ConcurrentUpdateSolrClient-就像ConcurrentUpdateSolrClientHttp2SolrClient改用。此类是实验性的,因此它的API可能会在次要版本的SolrJ中更改或删除

    3: 声明网址

    大多数SolrClient实现(CloudSolrClient和除外Http2SolrClient)都要求用户指定一个或多个Solr基本URL,然后客户端将其用于将HTTP请求发送到Solr。从那时起,用户在其提供的基本URL上包括的路径会影响所创建客户端的行为。

    1. 具有指向特定核心或集合(例如http://hostname:8983/solr/core1)的路径的URL 。如果在基本URL中指定了核心或集合,则不需要对该客户端进行的后续请求来重新指定受影响的集合。但是,客户端仅限于将请求发送到该核心/集合,而不能将请求发送到任何其他请求。
    2. 指向根Solr路径的URL(例如http://hostname:8983/solr)。如果在基本URL中未指定任何核心或集合,则可以对任何核心/集合进行请求,但是必须在所有请求上指定受影响的核心/集合。

    4: 自定义SolrClient

    SolrClient实现都允许用户指定连接并读取与Solr通信的超时

    final String solrUrl = "http://localhost:8983/solr";
    return new HttpSolrClient.Builder(solrUrl)
        .withConnectionTimeout(10000)
        .withSocketTimeout(60000)
        .build();

    Solr Cloud 集群实现是 SolrJ的CloudSolrClient实现(CloudSolrClientCloudHttp2SolrClien

    5: SolrJ创建文档

    //http://localhost:8080/saveOrUpdate?id=1&name=dgw&age=24
    @GetMapping("saveOrUpdate")
    public String saveOrUpdate(Admin admin) {
        if (admin.getId() == null) {
            admin.setId(System.currentTimeMillis());
        }
        try { #使用 SolrInputDocument
            final SolrInputDocument doc = new SolrInputDocument();
            doc.setField("id", admin.getId());
            doc.setField("name", admin.getName());
            doc.setField("age", admin.getAge());
            // collectionName 文档名称
            solrClient.add(solrConfig.collectionName, doc);
            solrClient.commit(solrConfig.collectionName);
            return admin.toString();
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "error";
    }


    6Solr复杂查询

    final SolrClient client = getSolrClient();
    
    final Map<String, String> queryParamMap = new HashMap<String, String>();
    queryParamMap.put("q", "*:*");
    queryParamMap.put("fl", "id, name");
    queryParamMap.put("sort", "id asc");
    MapSolrParams queryParams = new MapSolrParams(queryParamMap);
    
    final QueryResponse response = client.query("techproducts", queryParams);
    final SolrDocumentList documents = response.getResults();
    
    print("Found " + documents.getNumFound() + " documents");
    for(SolrDocument document : documents) {
      final String id = (String) document.getFirstValue("id");
      final String name = (String) document.getFirstValue("name");
    
      print("id: " + id + "; name: " + name);
    }

    7: CRUD查询例子

    package com.dgw.springbootsolr.controller;
    
    import com.dgw.springbootsolr.config.SolrConfig;
    import com.dgw.springbootsolr.entity.Admin;
    import com.dgw.springbootsolr.entity.Goods;
    import org.apache.solr.client.solrj.SolrClient;
    import org.apache.solr.client.solrj.SolrQuery;
    import org.apache.solr.client.solrj.SolrServerException;
    import org.apache.solr.client.solrj.response.QueryResponse;
    import org.apache.solr.common.SolrDocument;
    import org.apache.solr.common.SolrDocumentList;
    import org.apache.solr.common.SolrInputDocument;
    import org.apache.solr.common.util.NamedList;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * @program: springboot-solr
     * @description:
     * @author: Mr.Dai
     * @create: 2020-02-26 21:09
     **/
    @RestController
    public class SolrController {
    
        @Autowired
        public SolrClient solrClient;
    
        @Autowired
        private SolrConfig solrConfig;
    
        //http://localhost:8080/saveOrUpdate?id=1&name=dgw&age=24
        @GetMapping("saveOrUpdate")
        public String saveOrUpdate(Admin admin) {
            if (admin.getId() == null) {
                admin.setId(System.currentTimeMillis());
            }
            try {
                final SolrInputDocument doc = new SolrInputDocument();
                doc.setField("id", admin.getId());
                doc.setField("name", admin.getName());
                doc.setField("age", admin.getAge());
                // collectionName 文档名称
                solrClient.add(solrConfig.collectionName, doc);
                solrClient.commit(solrConfig.collectionName);
                return admin.toString();
            } catch (SolrServerException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return "error";
        }
    
        /**
         * 根据id删除索引
         * //http://localhost:8080/delete?id=1
         */
        @GetMapping("delete")
        public String delete(String id) {
            try {
                solrClient.deleteById(solrConfig.collectionName, id);
                solrClient.commit(solrConfig.collectionName);
                return id;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "error";
        }
    
        /**
         * 删除所有的索引
         * http://localhost:8080/deleteAll
         */
        @GetMapping("deleteAll")
        public String deleteAll() {
            try {
                solrClient.deleteByQuery(solrConfig.collectionName, "*:*");
                solrClient.commit(solrConfig.collectionName);
                return "success";
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "error";
        }
    
        /**
         * 根据id查询索引
         * http://localhost:8080/getGoodsById?id=1
         */
        @GetMapping("getGoodsById")
        public String getGoodsById(String id) throws Exception {
            SolrDocument document = solrClient.getById(solrConfig.collectionName, id);
            return document.toString();
        }
    
        //http://localhost:8080/addBean
        @GetMapping("addBean")
        public String addBean() {
            Admin dgw2 = new Admin(1L, "dgw2", 25);
            try {
                solrClient.addBean(solrConfig.collectionName, dgw2);
                solrClient.commit(solrConfig.collectionName);
            } catch (IOException | SolrServerException e) {
                e.printStackTrace();
            }
            return "error";
        }
    
        // http://localhost:8080/search
        @GetMapping("search")
        public Map<String, Object> search() {
            final SolrQuery solrQuery = new SolrQuery("*:*");
            solrQuery.addField("id");
            solrQuery.addField("name");
            solrQuery.addField("age");
            solrQuery.setSort("id", SolrQuery.ORDER.asc);
    
    
            //设置高亮前缀
            solrQuery.setHighlightSimplePre("<span style='color:red'>");
            //设置高亮后缀
            solrQuery.setHighlightSimplePost("</span>");
    
            //指定高亮域
            solrQuery.addHighlightField("id");
            solrQuery.addHighlightField("name");
            solrQuery.setHighlightFragsize(1);
            //打开开关
            solrQuery.setHighlight(true);
    
            //返回集合
            Map<String, Object> returnMap = new HashMap(16);
            try {
                QueryResponse query = solrClient.query(solrConfig.collectionName, solrQuery);
    
                SolrDocumentList results = query.getResults();
                //返回行数
                long numFound = results.getNumFound();
                //获取高亮显示的结果, 高亮显示的结果和查询结果是分开放的
                Map<String, Map<String, List<String>>> highlight = query.getHighlighting();
                System.out.println(results);
                System.out.println(highlight);
                results.forEach(result -> {
                    Map map = highlight.get(result.get("id"));
                    result.addField("nameHH", map.get("name"));
                });
                returnMap.put("results", results);
                return returnMap;
            } catch (SolrServerException | IOException e) {
                e.printStackTrace();
            }
            return new HashMap<String, Object>() {
                {
                    put("id", "1");
                    put("name", "ggg");
                    put("age", "12");
                }
            };
        }
    
        /**
         * 综合查询: 在综合查询中, 有按条件查询, 条件过滤, 排序, 分页, 高亮显示, 获取部分域信息
         *
         * @return
         */
        @GetMapping("search2")
        public Map<String, Object> search2(String keyword) {
            //返回集合
            Map<String, Object> returnMap = new HashMap();
            try {
                SolrQuery params = new SolrQuery();
                //查询条件
                params.set("q", keyword);
                //过滤条件
                params.set("fq", "goodsPrice:[100 TO 100000]");
                //排序
                params.addSort("id", SolrQuery.ORDER.asc);
                //分页
                //从第几条记录开始
                params.setStart(0);
                //最多返回多少条记录
                params.setRows(20);
                //默认域
                params.set("df", "goodsIntroduce");
                //只查询指定域
                params.set("fl", "id,goodsName,goodsIntroduce,goodsPrice");
                //高亮
                //打开开关
                params.setHighlight(true);
                //指定高亮域
                params.addHighlightField("goodsIntroduce");
                params.addHighlightField("goodsName");
                //设置高亮前缀
                params.setHighlightSimplePre("<span style='color:red'>");
                //设置高亮后缀
                params.setHighlightSimplePost("</span>");
                QueryResponse queryResponse = solrClient.query(solrConfig.collectionName, params);
                SolrDocumentList results = queryResponse.getResults();
                //返回行数
                long numFound = results.getNumFound();
                //获取高亮显示的结果, 高亮显示的结果和查询结果是分开放的
                Map<String, Map<String, List<String>>> highlight = queryResponse.getHighlighting();
                results.forEach(result -> {
                    Map map = highlight.get(result.get("id"));
                    result.addField("goodsNameHH", map.get("goodsName"));
                    result.addField("goodsIntroduceHH", map.get("goodsIntroduce"));
                });
                returnMap.put("numFound", numFound);
                returnMap.put("results", results);
                return returnMap;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        //http://localhost:8080/test
        @GetMapping("/test")
        public String test() {
            //System.out.println(solrConfig.collectionName);
            return solrConfig.collectionName;
        }
    }
  • 相关阅读:
    复合索引的列顺序判断
    sqlserver 存储过程中使用临时表到底会不会导致重编译
    Sql Server参数化查询之where in和like实现详解
    浅析SqlServer简单参数化模式下对sql语句自动参数化处理以及执行计划重用
    SQL优化 查询语句中,用 inner join 作为过滤条件和用where作为过滤条件的区别
    通过手动创建统计信息优化sql查询性能案例
    SQL Server 并行操作优化,避免并行操作被抑制而影响SQL的执行效率
    SQL Server 执行计划利用统计信息对数据行的预估原理二(为什么复合索引列顺序会影响到执行计划对数据行的预估)
    C#实现文件数据库
    ASP.NET DropDownList1_SelectedIndexChanged使用
  • 原文地址:https://www.cnblogs.com/dgwblog/p/12369970.html
Copyright © 2011-2022 走看看