solr 学习之solrJ
solrJ是访问Solr服务的JAVA客户端,提供索引和搜索的请求方法,SolrJ通常嵌入在业务系统中,通过solrJ的API接口操作Solr服务。
<!-- https://mvnrepository.com/artifact/org.apache.solr/solr-solrj -->
<dependency>
<groupId>org.apache.solr</groupId>
<artifactId>solr-solrj</artifactId>
<version>4.10.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
一、添加数据
public static void addDocument() throws Exception{
//创建Solr的客户端链接对象
HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");
for(int i=3;i<100;i++){
//创建一个文档对象
SolrInputDocument sd=new SolrInputDocument();
//添加域
sd.addField("id", UUID.randomUUID());
sd.addField("item_title", "商品"+i);
sd.addField("item_sell_point", "好看"+i);
sd.addField("item_price", 100L);
sd.addField("item_desc", "商品"+i+"这个东西很不错啊");
sd.addField("item_image", "2"+i+".jpg");
sd.addField("item_category_name", "分类"+i);
solrServer.add(sd);
solrServer.commit();
}
}
二、删除
//根据document的Id直接删除
public static void deleteDocument() throws Exception{
//创建Solr的客户端链接对象
HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");
solrServer.deleteById("8ceee0a5-52ee-43b6-88ba-249b02c8279c");
solrServer.commit();
}
//根据条件查询删除
public static void deleteQueryDocument() throws Exception{
//创建Solr的客户端链接对象
HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");
//查询删除
solrServer.deleteByQuery("item_title:商品1");
solrServer.commit();
}
三、查询
public static void queryDocument() throws Exception{
//创建Solr的客户端链接对象
HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");
//创建solr的查询对象
SolrQuery sq=new SolrQuery();
//设置查询条件
sq.set("q","item_title:3" );
//查询
QueryResponse qr=solrServer.query(sq);
//获取查询结果
SolrDocumentList sds=qr.getResults();
//获取查询的记录数
long total=sds.getNumFound();
System.out.println("数量:"+total);
for(SolrDocument sd:sds){//默认取出10条记录
String id=(String) sd.getFieldValue("id");
String item_title=(String) sd.getFieldValue("item_title");
String item_sell_point=(String) sd.getFieldValue("item_sell_point");
long item_price=(Long) sd.getFieldValue("item_price");
String item_desc=(String) sd.getFieldValue("item_desc");
String item_image=(String) sd.getFieldValue("item_image");
String item_category_name=(String) sd.getFieldValue("item_category_name");
System.out.println("========================================");
System.out.println("id:"+id);
System.out.println("item_title:"+item_title);
System.out.println("item_sell_point:"+item_sell_point);
System.out.println("item_price:"+item_price);
System.out.println("item_desc:"+item_desc);
System.out.println("item_image:"+item_image);
System.out.println("item_category_name:"+item_category_name);
}
}
1、多条件查询
//设置查询条件
sq.set("q","item_title:3 AND item_desc:东西 OR item_sell_point:好看" );
2、设置过滤条件
//设置过滤条件
sq.set("fq", "item_price:[1 TO 20]");
3、设置排序
//设置排序
sq.addSort("item_title", ORDER.desc);
4、设置分页
//设置分页
sq.setStart(0);//开始位置
sq.setRows(3);//每页3条
5、设置高亮
public static void queryDocument() throws Exception{
//创建Solr的客户端链接对象
HttpSolrServer solrServer=new HttpSolrServer("http://192.168.6.179:8080/solr/collection1");
//创建solr的查询对象
SolrQuery sq=new SolrQuery();
//设置查询条件
sq.set("q","item_title:商品" );
//设置过滤条件
// sq.set("fq", "item_price:[1 TO 20]");
//设置排序
sq.addSort("item_title", ORDER.desc);
//设置分页
sq.setStart(0);//开始位置
sq.setRows(3);//每页3条 //开启高亮
sq.setHighlight(true);
sq.addHighlightField("item_title");//设置高亮域
sq.setHighlightSimplePre("<b>");//设置高亮样式
sq.setHighlightSimplePost("</b>");
//查询
QueryResponse qr=solrServer.query(sq);
//获取查询结果
SolrDocumentList sds=qr.getResults();
//获取查询的记录数
long total=sds.getNumFound();
System.out.println("数量:"+total);
for(SolrDocument sd:sds){//默认取出10条记录
String id=(String) sd.getFieldValue("id");
String item_title=(String) sd.getFieldValue("item_title");
String item_sell_point=(String) sd.getFieldValue("item_sell_point");
long item_price=(Long) sd.getFieldValue("item_price");
String item_desc=(String) sd.getFieldValue("item_desc");
String item_image=(String) sd.getFieldValue("item_image");
String item_category_name=(String) sd.getFieldValue("item_category_name");
System.out.println("========================================");
System.out.println("id:"+id);
System.out.println("item_title:"+item_title);
System.out.println("item_sell_point:"+item_sell_point);
System.out.println("item_price:"+item_price);
System.out.println("item_desc:"+item_desc);
System.out.println("item_image:"+item_image);
System.out.println("item_category_name:"+item_category_name);
//获取高亮显示的结构
Map<String, Map<String, List<String>>> highlighting=qr.getHighlighting();
if(highlighting!=null){
//根据Id获得每个域的高亮内容
Map<String, List<String>> map=highlighting.get(id);
//根据具体的域获取高亮内容
List<String> list=map.get("item_title");
if(list!=null && !list.isEmpty()){
for(String str:list){
System.out.println("str:"+str);
}
}
}
}
}
solr 学习之solrJ的更多相关文章
- Solr学习之四-Solr配置说明之二
上一篇的配置说明主要是说明solrconfig.xml配置中的查询部分配置,在solr的功能中另外一个重要的功能是建索引,这是提供快速查询的核心. 按照Solr学习之一所述关于搜索引擎的原理中说明了建 ...
- Solr学习记录:Getting started
目录 Solr学习记录:Getting started 1.Solr Tutorial 2. A Quick Overview Solr学习记录:Getting started 本教程使用环境:jav ...
- Solr学习总结(六)SolrNet的高级用法(复杂查询,分页,高亮,Facet查询)
上一篇,讲到了SolrNet的基本用法及CURD,这个算是SolrNet 的入门知识介绍吧,昨天写完之后,有朋友评论说,这些感觉都被写烂了.没错,这些基本的用法,在网上百度,资料肯定一大堆,有一些写的 ...
- Solr学习总结(五)SolrNet的基本用法及CURD
上一篇已经讲到了Solr 查询的相关的参数.这里在讲讲C#是如何通过客户端请求和接受solr服务器的数据, 这里推荐使用SolrNet,主要是:SolrNet使用非常方便,而且用户众多,一直都在更新, ...
- solr 学习片段
全文检索技术——Solr 1 主要内容 1.站内搜索技术选型 2.什么是solr Solr和lucene的区别 3.solr服务器的安装及配置 Solr整合tomcat Solr的演示 4.维护索引 ...
- Solr学习笔记之3、Solr dataimport - 从SQLServer导入数据建立索引
Solr学习笔记之3.Solr导入SQLServer数据建立索引 一.下载MSSQLServer的JDBC驱动 下载:Microsoft JDBC Driver 4.0 for SQL Server ...
- Solr学习笔记之2、集成IK中文分词器
Solr学习笔记之2.集成IK中文分词器 一.下载IK中文分词器 IK中文分词器 此文IK版本:IK Analyer 2012-FF hotfix 1 完整分发包 二.在Solr中集成IK中文分词器 ...
- Solr学习笔记之1、环境搭建
Solr学习笔记之1.环境搭建 一.下载相关安装包 1.JDK 2.Tomcat 3.Solr 此文所用软件包版本如下: 操作系统:Win7 64位 JDK:jdk-7u25-windows-i586 ...
- Solr学习(2) Solr4.2.0+IK Analyzer 2012
Solr学习(二) Solr4.2.0+IK Analyzer 2012 开场白: 本章简单讲述如何在solr中配置著名的 IK Analyzer 分词器. 本章建立在 Solr学习(一) 基础上进 ...
随机推荐
- Python学习:8.小项目之登录注册验证
简介 本次项目登录注册验证是对之前学习知识点的加深学习,这次项目的练习的知识点有函数.判断语句.循环语句.文件操作等. 项目流程 运行代码之后,输出登录或者注册选项. 当选择登录之后,输入用户名密码, ...
- web頁面優化以及SEO
轉載:https://blog.csdn.net/xustart7720/article/details/79960591 浏览器访问优化浏览器请求处理流程如下图: Etag:實體標籤.ETag是HT ...
- github上的golang双向rpc,基于原生“net/rpc”库实现,可以注册回调
github上的golang双向rpc,基于原生“net/rpc”库实现,可以注册回调.仅支持一个server和一个client交互. 地址:https://github.com/rocket049/ ...
- gg_pie
gg_pie gg_pie PeRl 今天尝试了一下用ggplot2画饼图,转换一下极坐标就可以实现,但是和以前画heatmap的时候不一样的是,我们在卷坐标的时候需要让数据集中在一个坐标轴上. 另一 ...
- 数据库 MySQL part3
外键约束 如果表A的主关键字是表B中的字段,则该字段称为表B的外键,表A称为主表,表B称为从表. 外键是用来实现参照完整性的,不同的外键约束方式将可以使两张表紧密的结合起来,特别是修改或者删除的级联操 ...
- 免考final linux提权与渗透入门——Exploit-Exercise Nebula学习与实践
免考final linux提权与渗透入门--Exploit-Exercise Nebula学习与实践 0x0 前言 Exploit-Exercise是一系列学习linux下渗透的虚拟环境,官网是htt ...
- 成都Uber优步司机奖励政策(1月18日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- P2351 [SDOi2012]吊灯
P2351 [SDOi2012]吊灯 https://www.luogu.org/problemnew/show/P2351 题意: 一棵树,能否全部分成大小为x的联通块. 分析: 显然x是n ...
- == vs === in Javascript
本文来自网易云社区 作者:魏文庆 如果你只想知道==与===的区别,请直接看总结,当然我更希望您能耐心看完全文.Javascript中用于相等比较的操作符有两个==和===.==我们通常称为" ...
- 在ubuntu trusty下安装python的rasterio库
就这些吧.. apt-get update -y apt-get install -y software-properties-common add-apt-repository ppa:ubuntu ...