一、Solrj的使用

  1.什么是Solrj

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

  

  2.如何使用

    需要的是solrj的包与拓展服务包

    

    使用solrj完成索引的维护:

    在solr中,索引库中都会存在一个唯一键,如果一个Document的id存在,则执行修改操作,如果不存在,则执行添加操作。

      添加/修改索引:   

    1、 创建HttpSolrServer对象,通过它和Solr服务器建立连接。

    2、 创建SolrInputDocument对象,然后通过它来添加域。

    3、 通过HttpSolrServer对象将SolrInputDocument添加到索引库。

    4、 提交。

@Test
public void addDocument() throws Exception { // 1、 创建HttpSolrServer对象,通过它和Solr服务器建立连接。
// 参数:solr服务器的访问地址
HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/");
// 2、 创建SolrInputDocument对象,然后通过它来添加域。
SolrInputDocument document = new SolrInputDocument();
// 第一个参数:域的名称,域的名称必须是在schema.xml中定义的
// 第二个参数:域的值
// 注意:id的域不能少
document.addField("id", "c0001");
document.addField("title_ik", "使用solrJ添加的文档");
document.addField("content_ik", "文档的内容");
document.addField("product_name", "商品名称");
// 3、 通过HttpSolrServer对象将SolrInputDocument添加到索引库。
server.add(document);
// 4、 提交。
server.commit();
}

      在界面查询索引查看效果:使用域名:名称,例如 id:c001的格式

    

      根据ID删除索引

@Test
public void deleteDocument() throws Exception {
// 1、 创建HttpSolrServer对象,通过它和Solr服务器建立连接。
// 参数:solr服务器的访问地址
HttpSolrServer server = new HttpSolrServer(
"http://localhost:8080/solr/");
// 根据ID删除
server.deleteById("c0001");
// 提交
server.commit();
}

      根据条件删除

@Test
public void deleteDocumentByQuery() throws Exception {
// 1、 创建HttpSolrServer对象,通过它和Solr服务器建立连接。
// 参数:solr服务器的访问地址
HttpSolrServer server = new HttpSolrServer(
"http://localhost:8080/solr/");
// 根据ID删除
server.deleteByQuery("id:c0001");
// 全部删除
// server.deleteByQuery("*:*");
// 提交
server.commit();
}

      查询索引

@Test
public void queryIndex() throws Exception {
// 创建HttpSolrServer对象,通过它和Solr服务器建立连接。
// 参数:solr服务器的访问地址
HttpSolrServer server = new HttpSolrServer(
"http://localhost:8080/solr/"); // 创建SolrQuery对象
SolrQuery query = new SolrQuery();
// 设置查询条件,名称“q”是固定的且必须 的
query.set("q", "id:2"); // 调用server的查询方法,查询索引库
QueryResponse response = server.query(query); // 查询结果
SolrDocumentList results = response.getResults(); // 查询结果总数
long cnt = results.getNumFound();
System.out.println("查询结果总数:" + cnt); for (SolrDocument solrDocument : results) {
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")); }
}

      复杂查询

@Test
public void queryIndex2() throws Exception {
// 创建HttpSolrServer对象,通过它和Solr服务器建立连接。
// 参数:solr服务器的访问地址
HttpSolrServer server = new HttpSolrServer("http://localhost:8080/solr/"); // 创建SolrQuery对象
SolrQuery query = new SolrQuery(); // 设置查询条件
query.setQuery("钻石");
// 设置过滤条件
query.setFilterQueries("product_catalog_name:幽默杂货");
// 设置排序
query.setSort("product_price", ORDER.desc);
// 设置分页信息
query.setStart(0);
query.setRows(10); // 设置显得的域的列表
query.setFields("id", "product_name", "product_price",
"product_catalog_name", "product_picture"); // 设置默认搜索域
query.set("df", "product_name"); // 设置高亮
query.setHighlight(true);
query.addHighlightField("product_name");
query.setHighlightSimplePre("<em>");
query.setHighlightSimplePost("</em>"); // 调用server的查询方法,查询索引库
QueryResponse response = server.query(query); // 查询结果
SolrDocumentList results = response.getResults(); // 查询结果总数
long cnt = results.getNumFound();
System.out.println("查询结果总数:" + cnt); for (SolrDocument solrDocument : results) {
System.out.println(solrDocument.get("id")); String productName = (String) solrDocument.get("product_name"); //获取高亮列表
Map<String, Map<String, List<String>>> highlighting = response
.getHighlighting();
//获得本文档的高亮信息
List<String> list = highlighting.get(solrDocument.get("id")).get(
"product_name");
//如果有高亮,则把商品名称赋值为有高亮的那个名称
if (list != null) {
productName = list.get(0);
} 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")); }
}

Solr第二讲——SolrJ客户端的使用与案例的更多相关文章

  1. 使用SolrJ客户端管理SolrCloud(Solr集群)

    1.使用SolrJ客户端管理SolrCloud(Solr集群). package com.taotao.search.service; import java.io.IOException; impo ...

  2. 购物商城学习--第二讲(maven工程介绍)

    接下来第二讲介绍整体工程如何使用maven搭建的. 使用maven管理工程的好处: jar包的管理: 工程之间的依赖管理: 自动打包 maven常见打包方式:jar.war和pom三种.jar工程,是 ...

  3. MySQL实战45讲学习笔记:日志系统(第二讲)

    一.重要的日志模块:redo log 1.通过酒店掌柜记账思路刨析redo log工作原理 2.InnoDB 的 redo log 是固定大小的 只要赊账记录在了粉板上或写了账本上,之后即使掌柜忘记了 ...

  4. (转)【风宇冲】Unity3D教程宝典之AssetBundles:第二讲

    原创文章如需转载请注明:转载自风宇冲Unity3D教程学院                             AssetBundles第二讲:AssetBundles与脚本 所有Unity的As ...

  5. solr 学习之solrJ

    solrJ是访问Solr服务的JAVA客户端,提供索引和搜索的请求方法,SolrJ通常嵌入在业务系统中,通过solrJ的API接口操作Solr服务. <!-- https://mvnreposi ...

  6. WebApp 安全风险与防护课堂(第二讲)开课了!

    本文由葡萄城技术团队于原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 在昨天的公开课中,由于参与的小伙伴们积极性和热情非常高,我们的讲师Carl ...

  7. POI教程之第二讲:创建一个时间格式的单元格,处理不同内容格式的单元格,遍历工作簿的行和列并获取单元格内容,文本提取

    第二讲 1.创建一个时间格式的单元格 Workbook wb=new HSSFWorkbook(); // 定义一个新的工作簿 Sheet sheet=wb.createSheet("第一个 ...

  8. Stanford机器学习---第二讲. 多变量线性回归 Linear Regression with multiple variable

    原文:http://blog.csdn.net/abcjennifer/article/details/7700772 本栏目(Machine learning)包括单参数的线性回归.多参数的线性回归 ...

  9. 【军哥谈CI框架】之入门教程之第二讲:分析CI结构和CI是怎么工作的

    [军哥谈CI框架]之入门教程之第二讲:分析CI结构和CI是怎么工作的   之入门教程之第二讲:分析CI结构和CI是如何工作的大家好!上一节,我们共同部署了一个CI网站,做到这一点非常简单,但是,亲们, ...

随机推荐

  1. MongoDB排序记录

    MongoDB sort()方法 要在MongoDB中排序文档,需要使用sort()方法. 该方法接受包含字段列表及其排序顺序的文档.使用指定排序顺序1和-1. 1用于升序,而-1用于降序. 语法 s ...

  2. 在WAS控制台,环境下添加新的虚拟主机别名

    错误现象: 11/16/13 16:52:22:612 CST] 00000021 util          W com.ibm.ws.webcontainer.util.VirtualHostCo ...

  3. Oracle中序列的操作以及使用前对序列的初始化

    Oracle中序列的操作以及使用前对序列的初始化   一 创建序列 create sequence myseq start with 1 increment by 1 nomaxvalue minva ...

  4. PHP读取文件内容的五种方式(转载)

    php读取文件内容的五种方式 分享下php读取文件内容的五种方法:好吧,写完后发现文件全部没有关闭.实际应用当中,请注意关闭 fclose($fp); php读取文件内容: -----第一种方法--- ...

  5. 关于Ftp服务器

    步骤/方法 首先在本地机器上创建一个用户!这些用户是用来登录到FTP的!我的电脑右键->管理->本地用户和组->用户->“右键”新建用户->输入用户名和密码再点创建就行了 ...

  6. 关于Git学习推荐

    Git学习除了推荐官方网站:https://git-scm.com/之外, 我个人比较推荐初学者或者被动使用者可以学习参考廖雪峰的这个教程:https://www.liaoxuefeng.com/wi ...

  7. jQuery Cookie操作cookie

    jQuery cookie下载地址:http://plugins.jquery.com/cookie/ 使用jquery.cookie.js依赖于jquery 基本用法:   1. 创建cookie ...

  8. 【题解】洛谷P2679 [NOIP2015TG] 子串(DP+滚动数组)

    次元传送门:洛谷P2679 思路 蒟蒻一开始并没有思路而去看了题解 我们发现对于两个字串的位置 我们只需要管他们匹配成功或者匹配失败即可 f[i][j][k] 记录当前 a[i]不论等不等于b[j] ...

  9. HDU 2021 发工资咯:)(最水贪心)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2021 发工资咯:) Time Limit: 2000/1000 MS (Java/Others)    ...

  10. 【Nginx】使用Nginx作为Http代理的配置文件

    请看配置文件中的注释~ #user nobody; worker_processes 1; #pid logs/nginx.pid; events { worker_connections 1024; ...