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的更多相关文章

  1. Solr学习之四-Solr配置说明之二

    上一篇的配置说明主要是说明solrconfig.xml配置中的查询部分配置,在solr的功能中另外一个重要的功能是建索引,这是提供快速查询的核心. 按照Solr学习之一所述关于搜索引擎的原理中说明了建 ...

  2. Solr学习记录:Getting started

    目录 Solr学习记录:Getting started 1.Solr Tutorial 2. A Quick Overview Solr学习记录:Getting started 本教程使用环境:jav ...

  3. Solr学习总结(六)SolrNet的高级用法(复杂查询,分页,高亮,Facet查询)

    上一篇,讲到了SolrNet的基本用法及CURD,这个算是SolrNet 的入门知识介绍吧,昨天写完之后,有朋友评论说,这些感觉都被写烂了.没错,这些基本的用法,在网上百度,资料肯定一大堆,有一些写的 ...

  4. Solr学习总结(五)SolrNet的基本用法及CURD

    上一篇已经讲到了Solr 查询的相关的参数.这里在讲讲C#是如何通过客户端请求和接受solr服务器的数据, 这里推荐使用SolrNet,主要是:SolrNet使用非常方便,而且用户众多,一直都在更新, ...

  5. solr 学习片段

    全文检索技术——Solr 1 主要内容 1.站内搜索技术选型 2.什么是solr Solr和lucene的区别 3.solr服务器的安装及配置 Solr整合tomcat Solr的演示 4.维护索引 ...

  6. Solr学习笔记之3、Solr dataimport - 从SQLServer导入数据建立索引

    Solr学习笔记之3.Solr导入SQLServer数据建立索引 一.下载MSSQLServer的JDBC驱动 下载:Microsoft JDBC Driver 4.0 for SQL Server ...

  7. Solr学习笔记之2、集成IK中文分词器

    Solr学习笔记之2.集成IK中文分词器 一.下载IK中文分词器 IK中文分词器 此文IK版本:IK Analyer 2012-FF hotfix 1 完整分发包 二.在Solr中集成IK中文分词器 ...

  8. Solr学习笔记之1、环境搭建

    Solr学习笔记之1.环境搭建 一.下载相关安装包 1.JDK 2.Tomcat 3.Solr 此文所用软件包版本如下: 操作系统:Win7 64位 JDK:jdk-7u25-windows-i586 ...

  9. Solr学习(2) Solr4.2.0+IK Analyzer 2012

    Solr学习(二) Solr4.2.0+IK Analyzer 2012 开场白: 本章简单讲述如何在solr中配置著名的 IK Analyzer 分词器. 本章建立在 Solr学习(一)  基础上进 ...

随机推荐

  1. python2.7入门---CGI编程&表单操作&cookie操作

        看到标题我们首先有个疑问,什么是CGI?CGI 目前由NCSA维护,NCSA定义CGI为:CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上 ...

  2. 路由追踪:traceroute/tcptraceroute

    一.工作原理 traceroute:IP路由过程中对数据包TTL(Time to Live,存活时间)进行处理.当路由器收到一个IP包时,会修改IP包的TTL(及由此造成的头部检验和checksum变 ...

  3. VINS(六)边缘化

    通常的边缘化是将联合概率分布分解为边缘概率分布和条件概率分布的过程,这样可以将Sliding Window中较旧的状态边缘化出Sliding Window,同时保留其信息.并且保证了对应H海塞矩阵的稀 ...

  4. hive中的优化问题

    一.fetch抓取 fetch 抓取是指,hive中对某些情况的查询可以不必使用MapReduce计算.(1)把hive.fetch.task.conversion 设置成none,然后执行查询语句, ...

  5. SpringBoot-03:SpringBoot+Idea热部署

      ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 所谓热部署,就是在项目启动中,修改class类中做的修改操作,无需重新启动项目,就可以变更,在网页展示中有 ...

  6. 虚拟机ubuntu使用串口

    1. 电脑的串口默认是在windows系统上,需要把串口转到ubuntu上面,按照下面的步骤先 2. 找到需要使用的串口 3. 在VMWARE里面连接该串口 或者使用方法 4. 成功之后,检查一下ls ...

  7. C# 委托 / 跨线程访问UI / 线程间操作无效: 从不是创建控件“Form1”的线程访问它

    C# 委托 / 跨线程访问UI /  线程间操作无效: 从不是创建控件“Form1”的线程访问它 网上的代码都比较复杂,还是这个简单 见代码, 简易解决办法: 主窗体代码 using System; ...

  8. 「题目代码」P1049~P1053(Java)

    P1049 谭浩强C语言(第三版)习题6.5 import java.util.*; import java.io.*; import java.math.BigInteger; import jav ...

  9. Objective-C 类和对象

    面向对象 面向对象(Object-Oriented)是基于面向过程(procedure-oriented)而言的 面向对象 强调对象<指挥者> OC, Java语言就是面向对象 面向过程 ...

  10. 域名、IP地址、URL关系

    域名是个文字形式记录的IP地址 IP地址是计算机在网络中的门牌号! URL是网页地址 例如1: http://zhidao.baidu.com/question/14674128.html 是URL ...