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 ...
随机推荐
- win10和浏览器快捷键
1. Win10快捷键[Win+↑/↓/←/→] 将当前窗口按比例固定到屏幕的四个边角,如左上.右上.左下.右下.[Win+1/2/3…] 按顺序打开任务栏上的已固定程序(不包括第一个“任务视图”按钮 ...
- DT-06 For MQTT
感谢关注深圳四博智联科技有限公司产品!我公司提供完整的WiFi信号强度采集方案,包括WiFi信号采集.设备远程管理平台.智能终端应用等. Doit_MQTT透传固件基于乐鑫ESP_IOT_SDK使用C ...
- selenium中的setUp,tearDown与setUpClass,tearDownClass的区别
def setUpClass(cls): cls.driver = webdriver.Chrome() cls.driver.maximize_window() def setUp(self): s ...
- 洛谷 P4127 [AHOI2009]同类分布
题意简述 求l~r之间各位数字之和能整除原数的数的个数. 题解思路 数位DP 代码 #include <cstdio> #include <cstring> typedef l ...
- import 和from…import
import 和from-import 一.import模块名 import time print(time.time()) import首次导入模块发生了3件事: 使用import time导入的时 ...
- java 局部变量成员变量区别
首先,成员变量可直接初始化(即赋值),也可不赋值,不赋值java按照以下类型自动赋值 局部变量调用前必须初始化(赋值),java不会自动处理 局部变量可以和成员变量重名(但不建议),如果在当前对象的方 ...
- Linux系统@根目录下各目录作用归纳图
- Yarn上常驻Spark-Streaming程序调优
对于长时间运行的Spark Streaming作业,一旦提交到YARN群集便需要永久运行,直到有意停止.任何中断都会引起严重的处理延迟,并可能导致数据丢失或重复.YARN和Apache Spark都不 ...
- @RequestBody 注意的问题
contentType : "application/json", //只能是这个 RequestBody 不能和form/data共存: @RequestMapping(valu ...
- 打包一沓开源的 C/C++ 包管理工具送给你!
本文作者:HelloGitHub-ChungZH 博客地址:https://chungzh.cn/ 包管理器可以帮助你更方便地安装依赖关系,并决定所安装的版本,提高你的开发幸福感.许多语言都有自己的包 ...