热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Solr学习笔记(3)——SolrJ管理索引库集群

一、什么是SolrJsolrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常嵌入在业务系统中,通过SolrJ
一、什么是SolrJ

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

  

二、SolrJ的基本操作

2.1 添加文档

  • 实现步骤:

  第一步:创建一个java工程

  第二步:导入相关jar包

     

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

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

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

  第六步:提交

  • 代码实现

    //向索引库中添加索引
    @Testpublic void addDocument() throws Exception {//和solr服务器创建连接//参数:solr服务器的地址SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");//创建一个文档对象SolrInputDocument document = new SolrInputDocument();//向文档中添加域//第一个参数:域的名称,域的名称必须是在schema.xml中定义的//第二个参数:域的值document.addField("id", "c0001");document.addField("title_ik", "使用solrJ添加的文档");document.addField("content_ik", "文档的内容");document.addField("product_name", "商品名称");//把document对象添加到索引库中
    solrServer.add(document);//提交修改
    solrServer.commit();} 

2.2 删除文档

  • 根据id删除

    //删除文档,根据id删除
    @Testpublic void deleteDocumentByid() throws Exception {//创建连接SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");//根据id删除文档solrServer.deleteById("c0001");//提交修改
    solrServer.commit();} 

  • 根据查询删除

    //根据查询条件删除文档
    @Testpublic void deleteDocumentByQuery() throws Exception {//创建连接SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");//根据查询条件删除文档solrServer.deleteByQuery("*:*");//提交修改
    solrServer.commit();}

2.3 修改文档

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

2.4 查询文档

  • 简单查询

    //查询索引
    @Testpublic void queryIndex() throws Exception {//创建连接SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");//创建一个query对象SolrQuery query = new SolrQuery();//设置查询条件query.setQuery("*:*");//执行查询QueryResponse queryResponse = solrServer.query(query);//取查询结果SolrDocumentList solrDocumentList = queryResponse.getResults();//共查询到商品数量System.out.println("共查询到商品数量:" + solrDocumentList.getNumFound());//遍历查询的结果for (SolrDocument solrDocument : solrDocumentList) {System.out.println(solrDocument.get("id"));System.out.println(solrDocument.get("product_name"));System.out.println(solrDocument.get("product_price"));System.out.println(solrDocument.get("product_catalog_name"));System.out.println(solrDocument.get("product_picture"));}}

  • 复杂查询(其中包含查询、过滤、分页、排序、高亮显示等处理)

    //复杂查询索引
    @Testpublic void queryIndex2() throws Exception {//创建连接SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");//创建一个query对象SolrQuery query = new SolrQuery();//设置查询条件query.setQuery("钻石");//过滤条件query.setFilterQueries("product_catalog_name:幽默杂货");//排序条件query.setSort("product_price", ORDER.asc);//分页处理query.setStart(0);query.setRows(10);//结果中域的列表query.setFields("id","product_name","product_price","product_catalog_name","product_picture");//设置默认搜索域query.set("df", "product_keywords");//高亮显示query.setHighlight(true);//高亮显示的域query.addHighlightField("product_name");//高亮显示的前缀query.setHighlightSimplePre("");//高亮显示的后缀query.setHighlightSimplePost("");//执行查询QueryResponse queryResponse = solrServer.query(query);//取查询结果SolrDocumentList solrDocumentList = queryResponse.getResults();//共查询到商品数量System.out.println("共查询到商品数量:" + solrDocumentList.getNumFound());//遍历查询的结果for (SolrDocument solrDocument : solrDocumentList) {System.out.println(solrDocument.get("id"));//取高亮显示String productName = "";Map>> highlighting = queryResponse.getHighlighting();List list = highlighting.get(solrDocument.get("id")).get("product_name");//判断是否有高亮内容if (null != list) {productName = list.get(0);} else {productName = (String) solrDocument.get("product_name");}System.out.println(productName);System.out.println(solrDocument.get("product_price"));System.out.println(solrDocument.get("product_catalog_name"));System.out.println(solrDocument.get("product_picture"));}} 

 三、使用SolrJ管理集群

3.1 添加文档

  使用步骤:

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

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

  第三步:需要设置DefaultCollection属性

  第四步:创建一SolrInputDocument对象。

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

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

  第七步:提交。

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

3.2 查询文档

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

@Testpublic void testSolrCloudQueryDocument() throws Exception {CloudSolrServer cloudSolrServer = new CloudSolrServer("192.168.25.135:2181,192.168.25.135:2182,192.168.25.135:2183");cloudSolrServer.setDefaultCollection("collection2");// 创建一个query对象SolrQuery query = new SolrQuery();// 设置查询条件query.setQuery("*:*");// 执行查询QueryResponse queryResponse = cloudSolrServer.query(query);// 取查询结果SolrDocumentList solrDocumentList = queryResponse.getResults();// 共查询到商品数量System.out.println("共查询到商品数量:"+solrDocumentList.getNumFound());// 遍历查询结果for (SolrDocument solrDocument : solrDocumentList) {System.out.println(solrDocument.get("id"));System.out.println(solrDocument.get("item_title"));System.out.println(solrDocument.get("item_price"));}}

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

xml version="1.0" encoding="UTF-8"?>
<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:context&#61;"http://www.springframework.org/schema/context" xmlns:p&#61;"http://www.springframework.org/schema/p"xmlns:aop&#61;"http://www.springframework.org/schema/aop" xmlns:tx&#61;"http://www.springframework.org/schema/tx"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context4.2.xsdhttp://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.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util4.2.xsd"><bean id&#61;"cloudSolrServer" class&#61;"org.apache.solr.client.solrj.impl.CloudSolrServer"><constructor-arg name&#61;"zkHost" value&#61;"192.168.25.135:2181,192.168.25.135:2182,192.168.25.135:2183">constructor-arg> <property name&#61;"defaultCollection" value&#61;"collection2">property>bean>
beans>

 

转:https://www.cnblogs.com/yft-javaNotes/p/10107022.html



推荐阅读
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • 本文介绍了Redis的基础数据结构string的应用场景,并以面试的形式进行问答讲解,帮助读者更好地理解和应用Redis。同时,描述了一位面试者的心理状态和面试官的行为。 ... [详细]
  • 本文介绍了Perl的测试框架Test::Base,它是一个数据驱动的测试框架,可以自动进行单元测试,省去手工编写测试程序的麻烦。与Test::More完全兼容,使用方法简单。以plural函数为例,展示了Test::Base的使用方法。 ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 本文介绍了计算机网络的定义和通信流程,包括客户端编译文件、二进制转换、三层路由设备等。同时,还介绍了计算机网络中常用的关键词,如MAC地址和IP地址。 ... [详细]
  • 本文详细介绍了如何使用MySQL来显示SQL语句的执行时间,并通过MySQL Query Profiler获取CPU和内存使用量以及系统锁和表锁的时间。同时介绍了效能分析的三种方法:瓶颈分析、工作负载分析和基于比率的分析。 ... [详细]
  • Imtryingtofigureoutawaytogeneratetorrentfilesfromabucket,usingtheAWSSDKforGo.我正 ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • 本文介绍了在iOS开发中使用UITextField实现字符限制的方法,包括利用代理方法和使用BNTextField-Limit库的实现策略。通过这些方法,开发者可以方便地限制UITextField的字符个数和输入规则。 ... [详细]
author-avatar
迎风拂忆_768
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有