Java操作Solr之SolrJ
添加SolrJ的jar包
solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常在嵌入在业务系统中,通过SolrJ的API接口操作Solr服务,
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.4</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
创建索引
使用SolrJ创建索引,通过调用SolrJ提供的API请求Solr服务,Document通过SolrInputDocument进行构建。
// 创建索引
public static void createIndex() throws Exception {
SolrServer solrServer = new HttpSolrServer("http://192.168.50.50:8080/solr/collection1");
SolrInputDocument document = new SolrInputDocument();
document.addField("id", "00001");
document.addField("name", "solr全文检索");
document.addField("price", 86.5f);
document.addField("description", "这是一本关于solr的书籍!");
UpdateResponse response = solrServer.add(document);
solrServer.commit();
}
删除索引
//删除索引
public void deleteIndex() throws Exception {
SolrServer solrServer = new HttpSolrServer(solrUrl);
//根据id删除
UpdateResponse response = solrServer.deleteById("c0001");
//根据多个id删除
solrServer.deleteById("0001,0002");
//自动查询条件删除
solrServer.deleteByQuery("name:教程");
solrServer.commit();
}
搜索索引
//查询索引
public static void selectIndex() throws Exception {
SolrServer solr = new HttpSolrServer(solrUrl);
// 查询对象
SolrQuery query = new SolrQuery();
//设置查询条件,名称“q”是固定的且必须的
//搜索keywords域,keywords是复制域包括name和description
query.set("q", "keywords:java教程");
// 设置商品分类、关键字查询
query.setQuery("name:数据 AND price:11.1");
// 设置价格范围
query.set("fq", "price:[1 TO 20]");
// 查询结果按照价格降序排序
//query.set("sort", "price desc");
query.addSort("price", ORDER.desc);
// 请求查询
QueryResponse response = solr.query(query);
// 查询结果
SolrDocumentList docs = response.getResults();
// 查询文档总数
System.out.println("查询文档总数" + docs.getNumFound());
for (SolrDocument doc : docs) {
String id = (String) doc.getFieldValue("id");
String name = (String)doc.getFieldValue("name");
Float price = (Float)doc.getFieldValue("price");
String description = (String)doc.getFieldValue("description");
System.out.println(id);
}
}
高亮搜索索引
// 分页和高亮
public static void selectHeightLight() throws Exception {
SolrServer solr = new HttpSolrServer(solrUrl);
// 查询对象
SolrQuery query = new SolrQuery();
// text是name、title等众多字段的复制域
query.setQuery("text:算");
// 每页显示记录数
int pageSize = 2;
// 当前页码
int curPage = 1;
// 开始记录下标
int begin = pageSize * (curPage - 1);
// 起始下标
query.setStart(begin);
// 结束下标
query.setRows(pageSize);
// 设置高亮参数
query.setHighlight(true); // 开启高亮组件
query.addHighlightField("name");// 高亮字段
query.setHighlightSimplePre("<span color='red'>");//前缀标记
query.setHighlightSimplePost("</span>");// 后缀标记
// 请求查询
QueryResponse response = solr.query(query);
// 查询结果
SolrDocumentList docs = response.getResults();
// 查询文档总数
System.out.println("查询文档总数" + docs.getNumFound());
for (SolrDocument doc : docs) {
// 商品主键
String id = (String) doc.getFieldValue("id");
// 商品名称
String name = (String)doc.getFieldValue("name");
// 高亮信息
if(response.getHighlighting() != null) {
if(response.getHighlighting().get(id) != null) {
// 取出高亮片段
Map<String, List<String>> map = response.getHighlighting().get(id);
if(map.get("name") != null) {
for(String s : map.get("name")) {
System.out.println(s);
}
}
}
}
}
}

Java操作Solr之SolrJ的更多相关文章
- java操作solr
<dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</a ...
- java操作solr实现查询功能
一.封装的查询方法 /** * solr查询方法 * @param client solr客户端 * @param query solr查询对象 * @return list集合 * @throws ...
- Solr 14 - SolrJ操作SolrCloud集群 (Solr的Java API)
目录 1 pom.xml文件的配置 2 SolrJ操作SolrCloud 1 pom.xml文件的配置 项目的pom.xml依赖信息请参照: Solr 09 - SolrJ操作Solr单机服务 (So ...
- Solr 09 - SolrJ操作Solr单机服务 (Solr的Java API)
目录 1 SolrJ是什么 2 SolrJ对索引的CRUD操作 2.1 创建Maven工程(打包方式选择为jar) 2.2 配置pom.xml文件, 加入SolrJ的依赖 2.3 添加和修改索引 2. ...
- 使用solrj操作solr索引库
(solrj)初次使用solr的开发人员总是很郁闷,不知道如何去操作solr索引库,以为只能用<五分钟solr4.5教程(搭建.运行)>中讲到的用xml文件的形式提交数据到索引库,其实没有 ...
- 使用solrj操作solr索引库,solr是lucene服务器
客户端开发 Solrj 客户端开发 Solrj Solr是搭建好的lucene服务器 当然不可能完全满足一般的业务需求 可能 要针对各种的架构和业务调整 这里就需要用到Solrj了 Solrj是Sol ...
- 利用SolrJ操作solr API完成index操作
使用SolrJ操作Solr会比利用httpClient来操作Solr要简单.SolrJ是封装了httpClient方法,来操作solr的API的.SolrJ底层还是通过使用httpClient中的方法 ...
- 使用solrJ操作solr常用方法 【注释非常详细,非常好】
转: 使用solrJ操作solr常用方法 2017年08月07日 22:49:06 成都往右 阅读数:8990 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- solr的客户端操作:使用solrj进行curd操作
导入相关的jar包 <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-s ...
随机推荐
- nginx 多级反向代理获取客户端真实IP
set_real_ip_from ; set_real_ip_from ; set_real_ip_from ; set_real_ip_from ; set_real_ip_from 127.0.0 ...
- [LeetCode] 884. Uncommon Words from Two Sentences_Easy tag: Hash Table
We are given two sentences A and B. (A sentence is a string of space separated words. Each word co ...
- Charles 使用教程
Charles 的简介 如何安装 Charles 将 Charles 设置成系统代理 Charles 主界面介绍 过滤网络请求 截取 iPhone 上的网络封包 截取 Https 通讯信息 模拟慢速网 ...
- 日期格式化(类似QQ邮箱中的邮件列表显示日期)
日期格式化(类似QQ邮箱中的邮件列表显示日期) public static string FormatDateDisplay(DateTime _datetime) { var ts = DateTi ...
- 特征选择:Filter/Wrapper/Embedded
一.特征的来源 在做数据分析的时候,特征的来源一般有两块,一块是业务已经整理好各种特征数据,我们需要去找出适合我们问题需要的特征:另一块是我们从业务特征中自己去寻找高级数据特征.我们就针对这两部分来分 ...
- jmeter 发送加密请求 beanshell断言 线程组间传递参数
原文地址https://www.cnblogs.com/wnfindbug/p/5817038.html 最近在做http加密接口,请求头的uid参数及body的请求json参数都经过加密再发送请求, ...
- python repr和str
都是将对象转换为字符串 repr """ repr(object) -> string Return the canonical string representa ...
- python list成员函数extend与append的区别
extend 原文解释,是以list中元素形式加入到列表中 extend list by appending elements from the iterable append(obj) 是将整个ob ...
- 解决Windows内存问题的两个小工具RamMap和VMMap
解决Windows内存问题需要对操作系统的深入理解,同时对于如何运用Windows调试器或性能监控器要有工作认知.如果你正试着得到细节,诸如内核堆栈大小或硬盘内存消耗,你会需要调试器命令和内核数据架构 ...
- 41.SEO----前端SEO技巧
一.搜索引擎工作原理 当我们在输入框中输入关键词,点击搜索或查询时,然后得到结果.深究其背后的故事,搜索引擎做了很多事情. 在搜索引擎网站,比如百度,在其后台有一个非常庞大的数据库,里面存储了海量的关 ...