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 ...
随机推荐
- Java连载14-补码简介&浮点型整数
一.补码简介 1.计算机中的符号数有三种表示方式,即为:原码.反码.补码.三种表示方法均有符号位和数值位,符号位都是0表示正数,符号位都是1表示负数. 2.计算机中的数字的存储方式:在计算机系统中,数 ...
- sql语句优化:尽量使用索引避免全表扫描
1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...
- 【数学+思维】ZZULIOJ 1531: 小L的区间求和
题目链接 题目描述 在给定的一个整数序列中,小L希望找到一个连续的区间,这个区间的和能够被k整除,请你帮小L算一下满足条件的最长的区间长度是多少. 输入 第一行输入两个整数n.k.(1 <= n ...
- F#周报2019年第33期
新闻 宣告.NET Core 3.0预览版8 新的fable.io站点伴随着更多文档发布 正在努力使你的团队相信F#的益处?Compositional IT能够提供帮助 提名2019年度F#社区英雄 ...
- 如何处理scrum中未完成的用户故事?
你听过柏林新建机场的故事吗?机场原定2006年开工,2007年启用,但由于机场建设过程中到处出现施工和安全问题,补东墙漏西墙,导致工期一拖再拖,预算一涨再涨,以至于2019年了还没开张,预计开业时间已 ...
- 由于Microsoft\VisualStudio\14.0\Designer\ShadowCache导致的一个异常问题
本文引用了一个DynamicDataDisplay和DynamicControl两个类库,本来使用的时候都时正常的,愉快的运行着. DynamicDataDisplay:这是一个用于动态数据可视化的W ...
- Pycharm2019.2.1永久激活
Pycharm2019.2.1永久激活 Pycharm官网自7月24更新到pycharm2019.2版本后,在短短的一个月内与8月23又带来新版本2019.2.1,不可说更新不快,对于"喜新 ...
- GLFW+GLAD OpenGL Mac开发环境搭建
前言 OpenGL 是什么?The Industry Standard for High Performance Graphics 这是官方解释.说白了他就是一套标准接口.对,是接口,并没有实现具体的 ...
- Shell总结2
1.---------->>sed命令用来取指定范围行. 2.--------------->>>>“$?“返回值的用法 (1)判断命令.脚本或函数等程序是否执行成 ...
- Scala 系列(二)—— 基本数据类型和运算符
一.数据类型 1.1 类型支持 Scala 拥有下表所示的数据类型,其中 Byte.Short.Int.Long 和 Char 类型统称为整数类型,整数类型加上 Float 和 Double 统称为数 ...