solr6.1-----solrJ 程序管理索引库
solrJ 是solr 提供的一个客户端,就是一个jar 包,把jar 添加到工程中整合solr 服务。
所需jar 包
D:\solr-6.1.0\dist 下面的 solr-solrj-6.1.0.jar,以及其依赖 D:\solr-6.1.0\dist\solrj-lib 文件夹下面的所有jar
创建工程添加jar 包
添加 solr 库索引
@Test
public void create() throws Exception{
System.out.println("======================create===============");
@SuppressWarnings({ "resource", "deprecation" })
SolrClient solrClient = new HttpSolrClient("http://127.0.0.1:9090/solr/collection1");
List<SolrInputDocument> docList = new ArrayList<SolrInputDocument>();
String[] titles = {
"chihirotj",
"chihirotj",
"CAS latency",
"CAS latency",
"CAS latency",
"CAS latency chihirotj"};
String[] contents = {
"chihirotj this a vear popular book",
"chihirotj i love you dlp",
"CAS latency a a a a a a a",
"CAS latency you ara bearuiflu",
"CAS latency i love this computer",
"CAS latency hao are you"};
int i = 100;
for(int j = 0; j <= 5; j ++){
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id",i++);
//需要在core1/conf/managed-schema中有对应的field
//在solr 6.1 版本中已经不需要了,会自己动态的创建域,类型为strings
doc.addField("title", titles[j]);
doc.addField("content", contents[j]);
docList.add(doc);
}
UpdateResponse rsp = solrClient.add(docList);
System.out.println("Add doc size" + docList.size() + " result:" + rsp.getStatus() + " Qtime:" + rsp.getQTime());
UpdateResponse rspcommit = solrClient.commit();
System.out.println("commit doc to index" + " result:" + rsp.getStatus() + " Qtime:" + rspcommit.getQTime());
}
之后在 管理员页面查询数据:点击query 能够查询出,刚刚添加的数据,则添加数据成功。

默认添加的域为 strings 类型。我们需要修改类型,使用能够解析字符串的分词器(title 域lz已经做了修改)

查询 solr 索引库
@Test
public void query(){
System.out.println("======================query===================");
SolrClient solrClient = new HttpSolrClient("http://127.0.0.1:9090/solr/collection1");
SolrQuery query = new SolrQuery();
//查询条件
query.setQuery("chihirotj");
//高亮字段
query.setParam("hl.fl", "title");
//开启高亮
query.setHighlight(true);
//返回的字符个数
query.setHighlightFragsize(200);
//是否需要完全匹配
//query.setHighlightRequireFieldMatch(true);
//前缀
query.setHighlightSimplePre("<em>");
//后缀
query.setHighlightSimplePost("</em>");
QueryResponse response = null;
try {
response = solrClient.query(query);
System.out.println(response.toString());
System.out.println();
SolrDocumentList docs = response.getResults();
Map<String, Map<String, List<String>>> maps = response.getHighlighting();
System.out.println("文档个数:" + docs.getNumFound());
System.out.println("查询时间:" + response.getQTime());
for (SolrDocument doc : docs) {
//配置分词后
System.out.println("id: " + doc.getFieldValue("id") + " title: " + doc.getFieldValue("title"));
}
for (Map<String, List<String>> map : maps.values()) {
List<String> title = map.get("title");
//高量的值
System.out.println(title.iterator().next());
}
} catch (Exception e) {
e.printStackTrace();
}
}
运行结果:
删除 索引库
@Test
public void deleteById() {
System.out.println("======================deleteById ===================");
SolrClient solrClient = new HttpSolrClient("http://127.0.0.1:9090/solr/collection1");
try {
String[] ids = {"1", "2", "4"};
for (String id : ids) {
UpdateResponse rsp = solrClient.deleteById(id);
System.out.println("delete id:" + id + " result:" + rsp.getStatus() + " Qtime:" + rsp.getQTime());
}
solrClient.commit();
} catch (SolrServerException | IOException e) {
e.printStackTrace();
}
}
参考:
http://www.cnblogs.com/feiye512/p/5630684.html
http://lucene.apache.org/solr/6_1_0/solr-solrj/org/apache/solr/client/solrj/SolrClient.html
solr6.1-----solrJ 程序管理索引库的更多相关文章
- JAVAEE——Solr:安装及配置、后台管理索引库、 使用SolrJ管理索引库、仿京东的电商搜索案例实现
1 学习回顾 1. Lucene 是Apache开源的全文检索的工具包 创建索引 查询索引 2. 遇到问题? 文件名 及文件内容 顺序扫描法 全文检索 3. 什么是全文检索? 这种先创建索引 再 ...
- solrj管理索引库
solrj管理索引库 1.1. 什么是solrJ solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常在嵌入在业务系统中,通过SolrJ的API接口操作Solr服务, ...
- (转)淘淘商城系列——使用solrj来测试索引库
http://blog.csdn.net/yerenyuan_pku/article/details/72892280 我们使用solrj来操作索引库,一般习惯先建一个单元测试类测试下增删改查方法是否 ...
- Solr学习笔记(3) —— SolrJ管理索引库&集群
一.什么是SolrJ solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常嵌入在业务系统中,通过SolrJ的API接口操作Solr服务,如下图: 二.SolrJ的基本 ...
- 使用solrj操作solr索引库
(solrj)初次使用solr的开发人员总是很郁闷,不知道如何去操作solr索引库,以为只能用<五分钟solr4.5教程(搭建.运行)>中讲到的用xml文件的形式提交数据到索引库,其实没有 ...
- 使用solrj操作solr索引库,solr是lucene服务器
客户端开发 Solrj 客户端开发 Solrj Solr是搭建好的lucene服务器 当然不可能完全满足一般的业务需求 可能 要针对各种的架构和业务调整 这里就需要用到Solrj了 Solrj是Sol ...
- lucene&solr学习——solr学习(二) Solr管理索引库
1.什么是solrJ solrj是访问Solr服务的java客户端,提供索引和搜索的请求方法,SolrJ通常在嵌入在业务系统中,通过SolrJ的API接口操作Solr服务,如下图: 依赖jar包: 2 ...
- Solr管理索引库——(十三)
a) 维护索引 1. 添加/更新文档 添加或更新单个文档
- Windows系统环境下Solr之Java实战(三)使用solrJ管理索引库
https://www.cnblogs.com/zhuxiaojie/p/5764680.html https://www.cnblogs.com/xieyupeng/p/9317158.html
随机推荐
- iOS 自定义对象转NSDictionary
我们在向后台Post数据的时候,常常需要把某个对象作为参数,比如在AF的框架中,我们进行Post时,其中的para参数就是需要NSdictionary的 Alamofire.request(.POST ...
- 基本概率分布Basic Concept of Probability Distributions 2: Poisson Distribution
PDF version PMF A discrete random variable $X$ is said to have a Poisson distribution with parameter ...
- django admin中保存添加的数据提示need string or buffer, int found
原因 def __unicode__(self): return unicode(self.pk) 此处如果没有unicode就会报这个错误,原因就是编码错误 以为是文件开始没有加utf-8导致的,然 ...
- 墙内无缝更新Android SDK
https://www.caoqq.net/android-sdk-offine-download.html Lucas · 10 个月前 打开Android SDK Manager, 打开设置 2. ...
- 捉襟见肘之UIView中contentMode属性
UIView.h @property(nonatomic) UIViewContentMode contentMode; // default is UIViewContentModeScaleToF ...
- Json对象与Json字符串互转(转载)
一.jQuery插件支持的转换方式 1 $.paseJSON(jsonstr);//将json字符串转换为json对象 二.浏览器支持的转换方式(Firefox,Chrome,Opera,Safair ...
- 过滤字符串的Html标记 c#函数 .
.public static string StripHTML(string strHtml) . { . string[] aryReg ={ . @"<script[^>]* ...
- JNI笔记
由于要做一个能够加红字体的dialog,而cocos2d中的CCMessageBox是系统内带的,我无法修改其字体颜色.事实上是可以修改的,通过观察发现CCMessageBox被调用后,在安卓平台中会 ...
- python学习笔记-(七)python基础--集合、文件操作&函数
本节内容 1.集合操作 2.文件操作 3.字符编码与转码 4.函数操作 1.集合操作 集合是一个无序的.不重复的数据组合: 1.1 常用操作 它的作用是: 1)自动去重:列表变成集合,自动去重: &g ...
- Java数据结构——链表-单链表
<1>链表 <2>引用和基本类型 <3>单链表 //================================================= // Fil ...