一、什么是SolrJ

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

  

二、SolrJ的基本操作

2.1 添加文档

  • 实现步骤:

  第一步:创建一个java工程

  第二步:导入相关jar包

     

  第三步:和Solr服务器建立连接。HttpSolrServer对象建立连接

  第四步:创建一个SolrInputDocument对象,然后添加域

  第五步:将SolrInputDocument添加到索引库

  第六步:提交

  • 代码实现

    1. //向索引库中添加索引
    2. @Test
    3. public void addDocument() throws Exception {
    4. //和solr服务器创建连接
    5. //参数:solr服务器的地址
    6. SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
    7. //创建一个文档对象
    8. SolrInputDocument document = new SolrInputDocument();
    9. //向文档中添加域
    10. //第一个参数:域的名称,域的名称必须是在schema.xml中定义的
    11. //第二个参数:域的值
    12. document.addField("id", "c0001");
    13. document.addField("title_ik", "使用solrJ添加的文档");
    14. document.addField("content_ik", "文档的内容");
    15. document.addField("product_name", "商品名称");
    16. //把document对象添加到索引库中
    17. solrServer.add(document);
    18. //提交修改
    19. solrServer.commit();
    20. } 

2.2 删除文档

  • 根据id删除

    1. //删除文档,根据id删除
    2. @Test
    3. public void deleteDocumentByid() throws Exception {
    4. //创建连接
    5. SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
    6. //根据id删除文档
    7. solrServer.deleteById("c0001");
    8. //提交修改
    9. solrServer.commit();
    10. } 
  • 根据查询删除

    1. //根据查询条件删除文档
    2. @Test
    3. public void deleteDocumentByQuery() throws Exception {
    4. //创建连接
    5. SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
    6. //根据查询条件删除文档
    7. solrServer.deleteByQuery("*:*");
    8. //提交修改
    9. solrServer.commit();
    10. }

2.3 修改文档

  在solrJ中修改没有对应的update方法,只有add方法,只需要添加一条新的文档,和被修改的文档id一致就,可以修改了。本质上就是先删除后添加

2.4 查询文档

  • 简单查询

    1. //查询索引
    2. @Test
    3. public void queryIndex() throws Exception {
    4. //创建连接
    5. SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
    6. //创建一个query对象
    7. SolrQuery query = new SolrQuery();
    8. //设置查询条件
    9. query.setQuery("*:*");
    10. //执行查询
    11. QueryResponse queryResponse = solrServer.query(query);
    12. //取查询结果
    13. SolrDocumentList solrDocumentList = queryResponse.getResults();
    14. //共查询到商品数量
    15. System.out.println("共查询到商品数量:" + solrDocumentList.getNumFound());
    16. //遍历查询的结果
    17. for (SolrDocument solrDocument : solrDocumentList) {
    18. System.out.println(solrDocument.get("id"));
    19. System.out.println(solrDocument.get("product_name"));
    20. System.out.println(solrDocument.get("product_price"));
    21. System.out.println(solrDocument.get("product_catalog_name"));
    22. System.out.println(solrDocument.get("product_picture"));
    23. }
    24. }
  • 复杂查询(其中包含查询、过滤、分页、排序、高亮显示等处理)
    1. //复杂查询索引
    2. @Test
    3. public void queryIndex2() throws Exception {
    4. //创建连接
    5. SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
    6. //创建一个query对象
    7. SolrQuery query = new SolrQuery();
    8. //设置查询条件
    9. query.setQuery("钻石");
    10. //过滤条件
    11. query.setFilterQueries("product_catalog_name:幽默杂货");
    12. //排序条件
    13. query.setSort("product_price", ORDER.asc);
    14. //分页处理
    15. query.setStart(0);
    16. query.setRows(10);
    17. //结果中域的列表
    18. query.setFields("id","product_name","product_price","product_catalog_name","product_picture");
    19. //设置默认搜索域
    20. query.set("df", "product_keywords");
    21. //高亮显示
    22. query.setHighlight(true);
    23. //高亮显示的域
    24. query.addHighlightField("product_name");
    25. //高亮显示的前缀
    26. query.setHighlightSimplePre("<em>");
    27. //高亮显示的后缀
    28. query.setHighlightSimplePost("</em>");
    29. //执行查询
    30. QueryResponse queryResponse = solrServer.query(query);
    31. //取查询结果
    32. SolrDocumentList solrDocumentList = queryResponse.getResults();
    33. //共查询到商品数量
    34. System.out.println("共查询到商品数量:" + solrDocumentList.getNumFound());
    35. //遍历查询的结果
    36. for (SolrDocument solrDocument : solrDocumentList) {
    37. System.out.println(solrDocument.get("id"));
    38. //取高亮显示
    39. String productName = "";
    40. Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
    41. List<String> list = highlighting.get(solrDocument.get("id")).get("product_name");
    42. //判断是否有高亮内容
    43. if (null != list) {
    44. productName = list.get(0);
    45. } else {
    46. productName = (String) solrDocument.get("product_name");
    47. }
    48.  
    49. System.out.println(productName);
    50. System.out.println(solrDocument.get("product_price"));
    51. System.out.println(solrDocument.get("product_catalog_name"));
    52. System.out.println(solrDocument.get("product_picture"));
    53.  
    54. }
    55. } 

三、使用SolrJ管理集群

3.1 添加文档

  使用步骤:

  第一步:把solrJ相关的jar包添加到工程中。

  第二步:创建一个SolrServer对象,需要使用CloudSolrServer子类。构造方法的参数是zookeeper的地址列表。  

  第三步:需要设置DefaultCollection属性

  第四步:创建一SolrInputDocument对象。

  第五步:向文档对象中添加域

  第六步:把文档对象写入索引库。

  第七步:提交。

  1.   @Test
  2. public void testSolrCloudAddDocument() throws Exception {
  3. // 第一步:把solrJ相关的jar包添加到工程中。
  4. // 第二步:创建一个SolrServer对象,需要使用CloudSolrServer子类。构造方法的参数是zookeeper的地址列表。
  5. // 参数是zookeeper的地址列表,使用逗号分隔
  6. CloudSolrServer solrServer = new CloudSolrServer("192.168.25.135:2181,192.168.25.135:2182,192.168.25.135:2183");
  7. // 第三步:需要设置DefaultCollection属性。
  8. solrServer.setDefaultCollection("collection2");
  9. // 第四步:创建一SolrInputDocument对象。
  10. SolrInputDocument document = new SolrInputDocument();
  11. // 第五步:向文档对象中添加域
  12. document.addField("id", "test01");
  13. document.addField("item_title", "测试商品");
  14. document.addField("item_price", "100");
  15. // 第六步:把文档对象写入索引库。
  16. solrServer.add(document);
  17. // 第七步:提交。
  18. solrServer.commit();
  19. }

3.2 查询文档

  创建一个CloudSolrServer对象,其他处理和单机版一致。

  1. @Test
  2.   public void testSolrCloudQueryDocument() throws Exception {
  3. CloudSolrServer cloudSolrServer = new CloudSolrServer("192.168.25.135:2181,192.168.25.135:2182,192.168.25.135:2183");
  4. cloudSolrServer.setDefaultCollection("collection2");
  5. // 创建一个query对象
  6. SolrQuery query = new SolrQuery();
  7. // 设置查询条件
  8. query.setQuery("*:*");
  9. // 执行查询
  10. QueryResponse queryResponse = cloudSolrServer.query(query);
  11. // 取查询结果
  12. SolrDocumentList solrDocumentList = queryResponse.getResults();
  13. // 共查询到商品数量
  14. System.out.println("共查询到商品数量:"+solrDocumentList.getNumFound());
  15. // 遍历查询结果
  16. for (SolrDocument solrDocument : solrDocumentList) {
  17. System.out.println(solrDocument.get("id"));
  18. System.out.println(solrDocument.get("item_title"));
  19. System.out.println(solrDocument.get("item_price"));
  20. }
  21. }

四、把搜索功能切换到集群版

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans4.2.xsd
  7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context4.2.xsd
  8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx4.2.xsd
  9. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util4.2.xsd">
  10.  
  11. <!-- 单机版solr服务配置 -->
  12. <!-- <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
  13. <constructor-arg name="baseURL" value="http://192.168.25.135:8080/solr"></constructor-arg>
  14. </bean> -->
  15. <!-- 集群版solr服务 -->
  16. <bean id="cloudSolrServer" class="org.apache.solr.client.solrj.impl.CloudSolrServer">
  17. <constructor-arg name="zkHost" value="192.168.25.135:2181,192.168.25.135:2182,192.168.25.135:2183"></constructor-arg>
  18. <property name="defaultCollection" value="collection2"></property>
  19. </bean>
  20. </beans>

Solr学习笔记(3) —— SolrJ管理索引库&集群的更多相关文章

  1. JAVAEE——Solr:安装及配置、后台管理索引库、 使用SolrJ管理索引库、仿京东的电商搜索案例实现

    1 学习回顾 1. Lucene  是Apache开源的全文检索的工具包 创建索引 查询索引 2. 遇到问题? 文件名 及文件内容  顺序扫描法  全文检索 3. 什么是全文检索? 这种先创建索引 再 ...

  2. solrj管理索引库

    solrj管理索引库 1.1. 什么是solrJ solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常在嵌入在业务系统中,通过SolrJ的API接口操作Solr服务, ...

  3. Dubbo入门到精通学习笔记(十七):FastDFS集群的安装、FastDFS集群的配置

    文章目录 FastDFS集群的安装 FastDFS 介绍(参考:http://www.oschina.net/p/fastdfs) FastDFS 上传文件交互过程: FastDFS 下载文件交互过程 ...

  4. Docker 与 K8S学习笔记(二十三)—— Kubernetes集群搭建

    小伙伴们,好久不见,这几个月实在太忙,所以一直没有更新,今天刚好有空,咱们继续k8s的学习,由于我们后面需要深入学习Pod的调度,所以我们原先使用MiniKube搭建的实验环境就不能满足我们的需求了, ...

  5. ELK学习笔记之简单适用的ES集群监控工具cerebro安装使用

    安装指导及使用简介 1.      下载安装包: https://github.com/lmenezes/cerebro/releases/download/v0.7.3/cerebro-0.7.3. ...

  6. Nginx网络架构实战学习笔记(六):服务器集群搭建、集群性能测试

    文章目录 服务器集群搭建 Nginx---->php-fpm之间的优化 302机器 202机器 压力测试 搭建memcached.mysql(数据准备) 今晚就动手-.- 集群性能测试 服务器集 ...

  7. Dubbo入门到精通学习笔记(十三):ZooKeeper集群的安装、配置、高可用测试、升级、迁移

    文章目录 ZooKeeper集群的安装.配置.高可用测试 ZooKeeper 与 Dubbo 服务集群架构图 1. 修改操作系统的/etc/hosts 文件,添加 IP 与主机名映射: 2. 下载或上 ...

  8. 【Kubernetes学习笔记】-kubeadm 手动搭建kubernetes 集群

    目录 K8S 组件构成 环境准备 (以ubuntu系统为例) 1. kubernetes集群机器 2. 安装 docker. kubeadm.kubelet.kubectl 2.1 在每台机器上安装 ...

  9. Windows系统环境下Solr之Java实战(三)使用solrJ管理索引库

    https://www.cnblogs.com/zhuxiaojie/p/5764680.html https://www.cnblogs.com/xieyupeng/p/9317158.html

随机推荐

  1. DAY16-Django之model

    Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据 ...

  2. matlab学习笔记(3)

    数据分析: 多项式: 多项式表示:p = [1 2 3 0]; //表示 1*x^3+2*x^2+3*x^1+0 ,系数从高次向低次项,0系数不能省略. roots函数:求解多项式的根.roots(p ...

  3. sql语句中的问号是干什么的???

    1.实例代码一: String sql = "SELECT userid,name FROM tuser WHERE userid=? AND password=?" ;  pst ...

  4. jQuery选择器大全整理

    一.选择网页元素 $(document) //选择整个文档对象 $('#myId') //选择ID为myId的网页元素 $('div.myClass') // 选择class为myClass的div元 ...

  5. Hibernate 执行sql语句返回yntax error: syntax error, expect LPAREN, actual NOT not

    hibernate自动创建表时提示 :  ERROR: sql injection violation, syntax error: syntax error, expect LPAREN, actu ...

  6. C++字符串流保存数据

    文件流是以外存文件为输入输出对象的数据流.字符串流是以内存中用户定义的字符数组(字符串)为输入输出对象的. 建立输出字符串流: ostrstream strout(c,sizeof(c));第一个参数 ...

  7. Entity Framework Tutorial Basics(23):Add Single Entity

    Add New Entity using DBContext in Disconnected Scenario: In this chapter you will learn how to add n ...

  8. Entity Framework Tutorial Basics(10):Entity Lifecycle

    Entity Lifecycle: Before we work on CRUD operation (Create, Read, Update, Delete), it's important to ...

  9. 天梯赛 L2-006 树的遍历(序列建树)

    L2-006 树的遍历 (25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点 ...

  10. Java代码执行机制

    一.java源码编译机制 一个java文件编译为.class文件的流程图. Parse an Enter阶段: Parse:将代码字符串转换为token序列(com.sun.tools.javac.p ...