Elasticsearch在Java中的增删改查
public class ElasticAPI {
private static RestClient restClient;
static {
restClient=RestClient.builder(new HttpHost("localhost",9200,"http")).build();
}
/***
* Index API
* @throws IOException
*/
@Test
public void IndexAPI() throws IOException {
String method = "PUT";
String endpoint = "twitter/_doc/1";
HttpEntity entity = new NStringEntity(
"{\n" +
" \"user\" : \"kimchy\",\n" +
" \"post_date\" : \"2009-11-15T14:12:12\",\n" +
" \"message\" : \"trying out Elasticsearch\"\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 获取
*/
@Test
public void GetIndexAPI() throws IOException {
String method = "GET";
String endpoint = "twitter/_doc/1";
Response response = restClient.performRequest(method,endpoint);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* API还允许使用以下方式检查文档是否存在 HEAD:
* 获取
*/
@Test
public void HeadIndexAPI() throws IOException {
String method = "HEAD";
String endpoint = "twitter/_doc/1";
Response response = restClient.performRequest(method,endpoint);
System.out.println(response.getEntity());
}
/**
* ××××××
* @throws IOException
*/
public void OperationType() throws IOException {
String method = "PUT";
String endpoint = "twitter/docs/3";
HttpEntity entity = new NStringEntity(
"{\n" +
" \"user\" : \"ssss\",\n" +
" \"post_date\" : \"2009-11-15T14:12:12\",\n" +
" \"message\" : \"Elasticsearch\"\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* ××××××
* 获取
*/
@Test
public void GetOperationType() throws IOException {
String method = "GET";
String endpoint ="twitter/docs/3";
Response response = restClient.performRequest(method,endpoint);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* POST 产生一个随机id
* @throws IOException
*/
@Test
public void IndexAPIPost() throws IOException {
String method = "POST";
String endpoint = "twitter/_doc";
HttpEntity entity = new NStringEntity(
"{\n" +
" \"user\" : \"kimchy\",\n" +
" \"post_date\" : \"2009-11-15T14:12:12\",\n" +
" \"message\" : \"trying out Elasticsearch\"\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 获取 根据随机id
* @throws IOException
*/
@Test
public void GetIndexAPIPost() throws IOException {
String method = "GET";
String endpoint = "twitter/_doc/EWq-LmkBioM-ebzVc1rq";
Response response = restClient.performRequest(method,endpoint);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
*Stored Fields
*get操作允许指定将通过传递stored_fields参数返回的一组存储字段。如果未存储请求的字段,则将忽略它们。
* 首先得添加相应映射
*/
@Test
public void Stored() throws IOException {
String method = "PUT";
String endpoint = "twitters";
HttpEntity entity = new NStringEntity(
"{\n" +
" \"mappings\": {\n" +
" \"_doc\": {\n" +
" \"properties\": {\n" +
" \"counter\": {\n" +
" \"type\": \"integer\",\n" +
" \"store\": false\n" +
" },\n" +
" \"tags\": {\n" +
" \"type\": \"keyword\",\n" +
" \"store\": true\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
@Test
public void putS() throws IOException {
String method = "PUT";
String endpoint = "twitters/_doc/1";
HttpEntity entity = new NStringEntity(
"{\n" +
" \"counter\" : 1,\n" +
" \"tags\" : [\"red\"]\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
@Test
public void getS() throws IOException {
String method = "GET";
String endpoint = "twitters/_doc/1?stored_fields=tags,counter";
Response response = restClient.performRequest(method,endpoint);
System.out.println(EntityUtils.toString(response.getEntity()));
/**
* {"_index":"twitters","_type":"_doc","_id":"1","_version":3,"found":true,"fields":{"tags":["red"]}}
由于该counter字段未存储,因此get请求在尝试获取时只是忽略它stored_fields。map映射时,counter的store属性为false
*/
}
@Test
public void putS2() throws IOException {
String method = "PUT";
String endpoint = "twitters/_doc/2?routing=user11";
HttpEntity entity = new NStringEntity(
"{\n" +
" \"counter\" : 1,\n" +
" \"tags\" : [\"white\"]\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
@Test
public void getS2() throws IOException {
String method = "GET";
String endpoint = "twitters/_doc/2?routing=user11&stored_fields=tags,counter";
/**
* {"_index":"twitters","_type":"_doc","_id":"2","_version":2,"_routing":"user11","found":true,"fields":{"tags":["white"]}}
* 使用控制路由的能力进行索引时,为了获取文档,还应提供路由值。
*/
Response response = restClient.performRequest(method,endpoint);
System.out.println(EntityUtils.toString(response.getEntity()));
}
@Test
public void getTest() throws IOException {
String method = "GET";
String endpoint = "twitters/_doc/1/_source";
/**
* 只获取_source文档的字段,而不包含任何其他内容
{
"counter" : 1,
"tags" : ["red"]
}
*/
Response response = restClient.performRequest(method,endpoint);
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
Elasticsearch在Java中的增删改查的更多相关文章
- java中编写增删改查
按照图书数据库来说 //查询 :查询的返回值有两种类型,如果返回的数据你不确定是一条还是多条就返回一个List集合.如果你确定数据返回的是一条,可以把返回值换成Book实体类型.public List ...
- 【设计模式】【应用】使用模板方法设计模式、策略模式 处理DAO中的增删改查
原文:使用模板方法设计模式.策略模式 处理DAO中的增删改查 关于模板模式和策略模式参考前面的文章. 分析 在dao中,我们经常要做增删改查操作,如果每个对每个业务对象的操作都写一遍,代码量非常庞大. ...
- Elasticsearch之文档的增删改查以及ik分词器
文档的增删改查 增加文档 使用elasticsearch-head查看 修改文档 使用elasticsearch-head查看 删除文档 使用elasticsearch-head查看 查看文档的三种方 ...
- java DMO及增删改查代码的自动生成
在web开发过程中,尤其是后台管理系统的开发中,少不了增删改成的基础操作,原来我自己的做法是一份一份的拷贝粘贴,然后修改其中的不同,然而这样既枯燥无味又浪费了大量的时间,所以根据自己项目结构的特点写了 ...
- 【简易版】Java ArrayList(增删改查)
1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: (1)动态的增加和减少元素 (2)实现了ICollectio ...
- SQLite中的增删改查
虽然android提供了sql查询的封装方法,但是理解起来还是麻烦,所以我这里用sql语句来完成工作. 首先是建立一个类,继承SQLiteOpenHelper 这里面会建立一个数据库,并且初始化一个表 ...
- MongoDB(二)-- Java API 实现增删改查
一.下载jar包 http://central.maven.org/maven2/org/mongodb/mongo-java-driver/ 二.代码实现 package com.xbq.mongo ...
- LR接口测试---Java Vuser之增删改查
import lrapi.lr; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepared ...
- MongoDB(六)java操作mongodb增删改查
java操作mysql数据库的代码我们已经了如指掌了.增删改查,java对mongodb数据库也是类似的操作,先是数据库连接.再是进行操作. 首先我们进入进入admin数据库.然后建立自己的数据库te ...
随机推荐
- ASP.NET Core MVC 之控制器(Controller)
操作(action)和操作结果(action result)是 ASP.NET MVC 构建应用程序的一个基础部分. 在 ASP.NET MVC 中,控制器用于定义和聚合一组操作.操作是控制器中处理传 ...
- Java——数据结构(顺序表)
这是一个顺序表的类,初始化的时候就已经确定了表的长度,之后不能添加数据,因为使用的是数组存储的数据,不过这个表的类型是泛型的. public class List { private Object[] ...
- POI通用导出Excel数据(包括样式设计)
前言 前一段时间我写过通用的导入Excel,前几天也写了导出pdf格式的,还有我之前搞得导出Word,我在之前的博客也都介绍了导出和导入是一个道理,无非是一个获取一个是赋值.昨天有一位同仁看了我的Ex ...
- 记一次mysql主从同步因断电产生的不能同步问题 1236 1032
背景: 项目新上线一个月,qa需要测试断电服务拉起,服务拉起成功后,发现mysql主从异常,以下是发现的问题以及解决方案 问题1: Slave_IO_Running: No 一方面原因是因为网络通信 ...
- Java实现ZooKeeper的zNode监控
上一篇文章已经完成了ZooKeeper的基本搭建和使用的介绍,现在开始用代码说话.参考 https://zookeeper.apache.org/doc/current/javaExample.htm ...
- SQLServer数据库处于恢复挂起状态的解决办法
一.总结 如果数据库处于一个恢复挂起的状态,并且对数据库做脱机和分离的操作,报出数据库文件不可访问的错误,可能是因为数据库的数据文件和日志文件在数据库正常连接的情况下,文件所在的磁盘脱机了,导致数据库 ...
- 信安周报-第02周:SQL基础
信安之路 第02周 Code:https://github.com/lotapp/BaseCode/tree/master/safe 前言 本周需要自行研究学习的任务贴一下: 1.概念(推荐) 数据库 ...
- monkey学习总结笔记
一.什么是monkey? Monkey是Android中的一个命令行工具,可以运行在模拟器里或实际设备中.它向系统发送伪随机的用户事件流(如按键输入.触摸屏输入.手势输入等),实现对正在开发的应用程序 ...
- v语言怎么玩
直接上github: https://github.com/vlang/v 前戏 大概是在6月份的时候,在github上看到了这个玩意,我以为是??? 我下意识的去查了一下有没有人在讨论这个语言,但是 ...
- Mybatis延迟加载的实现以及使用场景
首先我们先思考一个问题,假设:在一对多中,我们有一个用户,他有100个账户. 问题1:在查询用户的时候,要不要把关联的账户查出来? 问题2:在查询账户的时候,要不要把关联的用户查出来? 解答:在查询用 ...