一、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. c++内存区域结构及堆栈的一些知识

    一.c++在内存区域的分配图 可以看出,对于Linux系统下的,存储空间的分配有着较为层次清晰的分层.单片机大概也遵循这个分区架构. 二进制代码以及常量(CONST修饰)以及全局变量在最底层,存储空间 ...

  2. 我上线的android版app

    把自己开发的几个小的app上线了,在自己的博客中推广一下吧: 聊天兔子: 下载地址:http://android.myapp.com/myapp/detail.htm?apkName=com.fuly ...

  3. linux常用搜索文件命令

    使用linux系统难免会忘记文件所在的位置,可以使用以下命令对系统中的文件进行搜索.搜索文件的命令为”find“:”locate“:”whereis“:”which“:”type“ 方法/步骤     ...

  4. Dos操作基础

    dos命令大全 使用技巧 dos命令不区分大小写,比如C盘的Program Files,在dos命令中完全可以用"program files"代替,加上英文引号是因为名称的中间有空 ...

  5. window使用结束进程

    在cmd中输入下面信息: 1. 查看所有进程占用的端口 :netstat -ano 2.查看占用指定端口的程序:netstat –ano|findstr 指定端口号 3.杀死相关的进程: 方法一:使用 ...

  6. caffe 学习(3)——Layer Catalogue

    layer是建模和计算的基本单元. caffe的目录包含各种state-of-the-art model的layers. 为了创建一个caffe model,我们需要定义模型架构在一个protocol ...

  7. PAT——1001 害死人不偿命的(3n+1)猜想 (15)

    对给定的任一不超过1000的正整数n,简单地数一下,需要多少步(砍几下)才能得到n=1? 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值. 输出格式:输出从n计算到1需要的步数. 输入样例 ...

  8. linux iptables 开启和关闭服务端口号

    需求: 模拟数据库挂掉,服务正常但访问数据库报错,恢复数据库端口后,服务是否能正常访问数据库 步骤:首先,断掉端口号5432,测试服务运行情况:其次,开启端口号5432,测试服务运行情况: 具体操作: ...

  9. 【题解】洛谷P1896 [SCOI2005] 互不侵犯(状压DP)

    洛谷P1896:https://www.luogu.org/problemnew/show/P1896 前言 这是一道状压DP的经典题 原来已经做过了 但是快要NOIP 复习一波 关于一些位运算的知识 ...

  10. Google Fonts导致网页加载速度慢

    最近在做商城项目时候发现在加载一个html页面反应非常慢,查看发现是Google Font导致的网页加载速度缓慢,删除掉该样式会发现很多内容出错. 上网百度发现问题在于: 谷歌香港(google.co ...