JestClient
JestService.java
[html] view plain copy 在CODE上查看代码片派生到我的代码片
public class JestService {
/**
* 获取JestClient对象
* @return
*/
public JestClient getJestClient() {
JestClientFactory factory = new JestClientFactory();
factory.setHttpClientConfig(new HttpClientConfig
.Builder("http://localhost:9200")
.gson(new GsonBuilder().setDateFormat("yyyy-MM-dd'T'hh:mm:ss").create())
.connTimeout(1500)
.readTimeout(3000)
.multiThreaded(true)
.build());
return factory.getObject();
}
/**
* 创建索引
* @param jestClient
* @param indexName
* @return
* @throws Exception
*/
public boolean createIndex(JestClient jestClient, String indexName) throws Exception {
JestResult jr = jestClient.execute(new CreateIndex.Builder(indexName).build());
return jr.isSucceeded();
}
/**
* Put映射
* @param jestClient
* @param indexName
* @param typeName
* @param source
* @return
* @throws Exception
*/
public boolean createIndexMapping(JestClient jestClient, String indexName, String typeName, String source) throws Exception {
PutMapping putMapping = new PutMapping.Builder(indexName, typeName, source).build();
JestResult jr = jestClient.execute(putMapping);
return jr.isSucceeded();
}
/**
* Get映射
* @param jestClient
* @param indexName
* @param typeName
* @return
* @throws Exception
*/
public String getIndexMapping(JestClient jestClient, String indexName, String typeName) throws Exception {
GetMapping getMapping = new GetMapping.Builder().addIndex(indexName).addType(typeName).build();
JestResult jr = jestClient.execute(getMapping);
return jr.getJsonString();
}
/**
* 索引文档
* @param jestClient
* @param indexName
* @param typeName
* @param objs
* @return
* @throws Exception
*/
public boolean index(JestClient jestClient, String indexName, String typeName, List<Object> objs) throws Exception {
Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(typeName);
for (Object obj : objs) {
Index index = new Index.Builder(obj).build();
bulk.addAction(index);
}
BulkResult br = jestClient.execute(bulk.build());
return br.isSucceeded();
}
/**
* 搜索文档
* @param jestClient
* @param indexName
* @param typeName
* @param query
* @return
* @throws Exception
*/
public SearchResult search(JestClient jestClient, String indexName, String typeName, String query) throws Exception {
Search search = new Search.Builder(query)
.addIndex(indexName)
.addType(typeName)
.build();
return jestClient.execute(search);
}
/**
* Count文档
* @param jestClient
* @param indexName
* @param typeName
* @param query
* @return
* @throws Exception
*/
public Double count(JestClient jestClient, String indexName, String typeName, String query) throws Exception {
Count count = new Count.Builder()
.addIndex(indexName)
.addType(typeName)
.query(query)
.build();
CountResult results = jestClient.execute(count);
return results.getCount();
}
/**
* Get文档
* @param jestClient
* @param indexName
* @param typeName
* @param id
* @return
* @throws Exception
*/
public JestResult get(JestClient jestClient, String indexName, String typeName, String id) throws Exception {
Get get = new Get.Builder(indexName, id).type(typeName).build();
return jestClient.execute(get);
}
/**
* Delete索引
* @param jestClient
* @param indexName
* @return
* @throws Exception
*/
public boolean delete(JestClient jestClient, String indexName) throws Exception {
JestResult jr = jestClient.execute(new DeleteIndex.Builder(indexName).build());
return jr.isSucceeded();
}
/**
* Delete文档
* @param jestClient
* @param indexName
* @param typeName
* @param id
* @return
* @throws Exception
*/
public boolean delete(JestClient jestClient, String indexName, String typeName, String id) throws Exception {
DocumentResult dr = jestClient.execute(new Delete.Builder(id).index(indexName).type(typeName).build());
return dr.isSucceeded();
}
/**
* 关闭JestClient客户端
* @param jestClient
* @throws Exception
*/
public void closeJestClient(JestClient jestClient) throws Exception {
if (jestClient != null) {
jestClient.shutdownClient();
}
}
}
(3)UserTest.java
[html] view plain copy 在CODE上查看代码片派生到我的代码片
public class UserTest {
private JestService jestService;
private JestClient jestClient;
private String indexName = "hwd";
private String typeName = "user";
@Before
public void setUp() throws Exception {
jestService = new JestService();
jestClient = jestService.getJestClient();
}
@After
public void tearDown() throws Exception {
jestService.closeJestClient(jestClient);
}
@Test
public void createIndex() throws Exception {
boolean result = jestService.createIndex(jestClient, indexName);
System.out.println(result);
}
@Test
public void createIndexMapping() throws Exception {
String source = "{\"" + typeName + "\":{\"properties\":{"
+ "\"id\":{\"type\":\"integer\"}"
+ ",\"name\":{\"type\":\"string\",\"index\":\"not_analyzed\"}"
+ ",\"birth\":{\"type\":\"date\",\"format\":\"strict_date_optional_time||epoch_millis\"}"
+ "}}}";
System.out.println(source);
boolean result = jestService.createIndexMapping(jestClient, indexName, typeName, source);
System.out.println(result);
}
@Test
public void getIndexMapping() throws Exception {
String result = jestService.getIndexMapping(jestClient, indexName, typeName);
System.out.println(result);
}
@Test
public void index() throws Exception {
List<Object> objs = new ArrayList<Object>();
objs.add(new User(1, "T:o\"m-", new Date()));
objs.add(new User(2, "J,e{r}r;y:", new Date()));
boolean result = jestService.index(jestClient, indexName, typeName, objs);
System.out.println(result);
}
@Test
public void termQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.termQuery("name", "T:o\"m-");//单值完全匹配查询
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
SearchResult result = jestService.search(jestClient, indexName, typeName, query);
List<Hit<User, Void>> hits = result.getHits(User.class);
System.out.println("Size:" + hits.size());
for (Hit<User, Void> hit : hits) {
User user = hit.source;
System.out.println(user.toString());
}
}
@Test
public void termsQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.termsQuery("name", new String[]{ "T:o\"m-", "J,e{r}r;y:" });//多值完全匹配查询
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
SearchResult result = jestService.search(jestClient, indexName, typeName, query);
List<Hit<User, Void>> hits = result.getHits(User.class);
System.out.println("Size:" + hits.size());
for (Hit<User, Void> hit : hits) {
User user = hit.source;
System.out.println(user.toString());
}
}
@Test
public void wildcardQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.wildcardQuery("name", "*:*");//通配符和正则表达式查询
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
SearchResult result = jestService.search(jestClient, indexName, typeName, query);
List<Hit<User, Void>> hits = result.getHits(User.class);
System.out.println("Size:" + hits.size());
for (Hit<User, Void> hit : hits) {
User user = hit.source;
System.out.println(user.toString());
}
}
@Test
public void prefixQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.prefixQuery("name", "T:o");//前缀查询
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
SearchResult result = jestService.search(jestClient, indexName, typeName, query);
List<Hit<User, Void>> hits = result.getHits(User.class);
System.out.println("Size:" + hits.size());
for (Hit<User, Void> hit : hits) {
User user = hit.source;
System.out.println(user.toString());
}
}
@Test
public void rangeQuery() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.rangeQuery("birth")
.gte("2016-09-01T00:00:00")
.lte("2016-10-01T00:00:00")
.includeLower(true)
.includeUpper(true);//区间查询
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
SearchResult result = jestService.search(jestClient, indexName, typeName, query);
List<Hit<User, Void>> hits = result.getHits(User.class);
System.out.println("Size:" + hits.size());
for (Hit<User, Void> hit : hits) {
User user = hit.source;
System.out.println(user.toString());
}
}
@Test
public void queryString() throws Exception {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders
.queryString(QueryParser.escape("T:o\""));//文本检索,应该是将查询的词先分成词库中存在的词,然后分别去检索,存在任一存在的词即返回,查询词分词后是OR的关系。需要转义特殊字符
searchSourceBuilder.query(queryBuilder);
searchSourceBuilder.size(10);
searchSourceBuilder.from(0);
String query = searchSourceBuilder.toString();
System.out.println(query);
SearchResult result = jestService.search(jestClient, indexName, typeName, query);
List<Hit<User, Void>> hits = result.getHits(User.class);
System.out.println("Size:" + hits.size());
for (Hit<User, Void> hit : hits) {
User user = hit.source;
System.out.println(user.toString());
}
}
@Test
public void count() throws Exception {
String[] name = new String[]{ "T:o\"m-", "Jerry" };
String from = "2016-09-01T00:00:00";
String to = "2016-10-01T00:00:00";
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
QueryBuilder queryBuilder = QueryBuilders.boolQuery()
.must(QueryBuilders.termsQuery("name", name))
.must(QueryBuilders.rangeQuery("birth").gte(from).lte(to));
searchSourceBuilder.query(queryBuilder);
String query = searchSourceBuilder.toString();
System.out.println(query);
Double count = jestService.count(jestClient, indexName, typeName, query);
System.out.println("Count:" + count);
}
@Test
public void get() throws Exception {
String id = "2";
JestResult result = jestService.get(jestClient, indexName, typeName, id);
if (result.isSucceeded()) {
User user = result.getSourceAsObject(User.class);
System.out.println(user.toString());
}
}
@Test
public void deleteIndexDocument() throws Exception {
String id = "2";
boolean result = jestService.delete(jestClient, indexName, typeName, id);
System.out.println(result);
}
@Test
public void deleteIndex() throws Exception {
boolean result = jestService.delete(jestClient, indexName);
System.out.println(result);
}
}
JestClient的更多相关文章
- JestClient 使用教程,教你完成大部分ElasticSearch的操作。
本篇文章代码实现不多,主要是教你如何用JestClient去实现ElasticSearch上的操作. 授人以鱼不如授人以渔. 一.说明 1.elasticsearch版本:6.2.4 . jdk版本: ...
- elasticsearch jestclient api
1.es search sroll 可以遍历索引下所有数据 public class TestDemo { @Test public void searchSroll() { JestClientFa ...
- 3.2_springBoot2.1.x检索之JestClient操作ElasticSearch
这里介绍Jest方式交互, 导入jest版本 <!--导入jest--> <dependency> <groupId>io.searchbox</groupI ...
- elasticsearch scroll api--jestclient invoke
@Test public void testScroll(){ JestClientFactory factory = new JestClientFactory(); factory.setHttp ...
- jest for elasticsearch
*elasticsearch(后面简称es) 背景: 目前项目应用中对es的操作用的是http(自己封装)的一套方法:有些数据处理起来还是需要定制开发处理,不是很方便.正好需要对本项目重新进行改造,于 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- springboot 注册dao层 service 层
可以使用三种注解来引入DAO层的接口到spring容器中.1.@Mapper,写在每一个DAO层接口上,如下: 2.@MapperScan和@ComponentScan两者之一.前者的意义是将指定包中 ...
- Spring Boot 整合 elasticsearch
一.简介 我们的应用经常需要添加检索功能,开源的 ElasticSearch 是目前全文搜索引擎的 首选.他可以快速的存储.搜索和分析海量数据.Spring Boot通过整合Spring Data E ...
- 史上最全面的Elasticsearch使用指南
Elasticsearch使用指南 Elasticsearch使用指南 前言 ES是什么 什么是全文检索 ES的应用场景 ES的存储结构 第一章:安装 1.下载 2.解压 3.配置 4.启动 5.查看 ...
随机推荐
- 文件缓存(配合JSON数组)
1. 写入缓存:建立文件夹,把list集合里面的数组转换为JSON数组,存入文件夹2. 读取缓存:把JSON数组从文件夹里面读取出来,然后放入list集合,返回list集合 private fin ...
- C# 在Repeater 的ItemDataBound 如何转换e.Item.DataItem 的类型
1.使用DataSet和DataTable绑定数据源时,用 DataRowView view = (DataRowView)e.Item.DataItem; 2.DataReader绑定数据源时,用 ...
- 使Python IDLE也变得高颜值
初学Python,大家应该都是用Python自带的IDLE了,我们可以让他颜值高一些,这样敲出的代码就赏心悦目,比如像下面这样: 我们首先要找到名为config-highlight.cfg的文件, ...
- ajax请求成功后打开新开窗口(window.open())被拦截的解决方法
问题:今天在做项目时需要在ajax请求成功后打开一个新的窗口,此时遇到浏览拦截了新窗口的问题,尝试在ajax 回调函数中模拟执行 click 或者 submit 等用户行为(trigger('clic ...
- 最实用的IT类网站及工具大集合
1.聚合数据 大家在开发过程中,可能会用到各种各样的数据,想找一些接口来提供一些数据.比如天气预报查询,火车时刻表查询,彩票查询,身份证查询等等.有了这个接口,直接调用即可.各种各样的API接口满足你 ...
- IBM Bluemix体验:Containers
国际版的Bluemix目前有三个region,US South,United Kingdom和Sydney.其中US South是功能最全的,UK其次,Sydney功能最少.Containers服务在 ...
- 让我们用心感受泛型接口的协变和抗变out和in
关键字out和in相信大家都不陌生,系统定义的很多泛型类型大家F12都或多或少看见了.但是实际中又很少会用到,以前在红皮书里看到,两三页就介绍完了.有的概念感觉直接搬出来的,只是说这样写会怎样,并没有 ...
- python爬虫学习(11) —— 也写个AC自动机
0. 写在前面 本文记录了一个AC自动机的诞生! 之前看过有人用C++写过AC自动机,也有用C#写的,还有一个用nodejs写的.. C# 逆袭--自制日刷千题的AC自动机攻克HDU OJ HDU 自 ...
- JS 阶段练习~ 仿flash的图片轮换效果
结合了所学的简单运动框架~ 做这样一个综合小实例~~ -------------------------主要问题: 1.getByClassName IE低版的兼容性 2.DOM不够严谨 … 各种 ...
- 第9章 Shell基础(2)_Bash基本功能
3. Bash的基本功能 3.1 历史命令与命令补全 (1)历史命令:#history [选项] [历史命令保存文件] ①选项:-c:清空历史命令: -w:把缓存中的历史命令写入文件~/.bash_h ...