1.什么是solrJ

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

依赖jar包:

2.用solrJ对solr后台进行增删改查

2.1 添加文档

//向索引库中添加索引
@Test
public 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删除
@Test
public void deleteDocumentByid() throws Exception {
//创建连接
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
//根据id删除文档
solrServer.deleteById("c0001");
//提交修改
solrServer.commit();
}
//根据查询条件删除文档
@Test
public void deleteDocumentByQuery() throws Exception {
//创建连接
SolrServer solrServer = new HttpSolrServer("http://localhost:8080/solr");
//根据查询条件删除文档
solrServer.deleteByQuery("*:*");
//提交修改
solrServer.commit();
}

2.3 修改文档

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

2.4 查询文档

  2.4.1 简单查询

  

//查询索引
@Test
public 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")); }
}

  2.4.2 复杂查询

//复杂查询索引
@Test
public 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("<em>");
//高亮显示的后缀
query.setHighlightSimplePost("</em>");
//执行查询
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<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
List<String> 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")); }
}

lucene&solr学习——solr学习(二) Solr管理索引库的更多相关文章

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

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

  2. solrj管理索引库

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

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

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

  4. Solr学习笔记(2) —— Solr管理索引库

    一.维护索引 1.1 添加/更新文档 1.2 批量导入数据(使用dataimport) 第一步:把mysql的数据驱动.以及dataimport插件依赖的jar包添加到solrcore(collect ...

  5. Solr管理索引库——(十三)

    a)          维护索引 1.  添加/更新文档 添加或更新单个文档

  6. solr6.1-----solrJ 程序管理索引库

    solrJ 是solr 提供的一个客户端,就是一个jar 包,把jar 添加到工程中整合solr 服务. 所需jar 包 D:\solr-6.1.0\dist 下面的 solr-solrj-6.1.0 ...

  7. Lucene第二篇【抽取工具类、索引库优化、分词器、高亮、摘要、排序、多条件搜索】

    对Lucene代码优化 我们再次看回我们上一篇快速入门写过的代码,我来截取一些有代表性的: 以下代码在把数据填充到索引库,和从索引库查询数据的时候,都出现了.是重复代码! Directory dire ...

  8. 第一个lucene程序,把一个信息写入到索引库中、根据关键词把对象从索引库中提取出来、lucene读写过程分析

    新建一个Java Project :LuceneTest 准备lucene的jar包,要加入的jar包至少有: 1)lucene-core-3.1.0.jar     (核心包) 2) lucene- ...

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

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

随机推荐

  1. 《X86汇编语言:从实模式到保护模式》读书笔记之引言

    有幸结识了<X86汇编语言:从实模式到保护模式>一书.我觉得这本书非常好,语言活泼,通俗易懂,源码丰富,受益匪浅.读罢一遍,意犹未尽.于是打算再读一遍,并把自己的读书所学总结成笔记,一来给 ...

  2. VRRP协议介绍--转

    http://www.cnblogs.com/jony413/articles/2697404.html VRRP协议介绍 参考资料: RFC 3768 1. 前言 VRRP(Virtual Rout ...

  3. 【Xshell】设置XShell最大的显示行数

    选择会话,依次点击“文件"->"属性”,打开“会话属性”窗体   在“会话属性”窗体中,选择“终端”,下图中红框标注的地方是“缓冲区大小”,修改其中的值,其范围在0~2,14 ...

  4. [转]Debugging into .NET Core源代码的两种方式

    本文转自:http://www.cnblogs.com/maxzhang1985/p/6015719.html 阅读目录 一.前言 二.符号服务器 三.项目中添加ASP.NET Core源代码 四.写 ...

  5. UiPath进阶

    最近RPA比较火,UiPath工具排名前几位并且免费试用,很多朋友们都选择了学习自动化工具UiPath,今天我就向大家介绍一下UiPath的学习过程,希望对后来的学习这个工具的人有所帮助. UiPat ...

  6. Maven入门之简介与安装

    一.Maven简介 1.什么是Maven? Maven是一个项目管理工具和集成编译工具,它主要包含如下内容: –一个项目对象模型(Project Object Model), –一组标准集合, –一个 ...

  7. module.exports和 exports 方法暴露

    在Node.js包管理时需要把方法暴露给外部文件 文件:Hello.js    Hello方法是模仿面向对象类的写法 function Hello() { var name; this.setName ...

  8. poi学习

    需要节点  <dependency>  <groupId>org.apache.poi</groupId>  <artifactId>poi</a ...

  9. Django的路由层(URLconf)

    URL配置(URLconf)就像Django 所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表:你就是以这种方式告诉Django,对于客户端发来的某个URL调用哪一段逻辑代码 ...

  10. CompletionService的异常处理

    一.采用take()方法时发生异常 示例代码: 情况一:异常比另一个正确任务,较晚出现,正确任务的结果会打印出 import java.util.concurrent.Callable; import ...