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中的增删改查的更多相关文章

  1. java中编写增删改查

    按照图书数据库来说 //查询 :查询的返回值有两种类型,如果返回的数据你不确定是一条还是多条就返回一个List集合.如果你确定数据返回的是一条,可以把返回值换成Book实体类型.public List ...

  2. 【设计模式】【应用】使用模板方法设计模式、策略模式 处理DAO中的增删改查

    原文:使用模板方法设计模式.策略模式 处理DAO中的增删改查 关于模板模式和策略模式参考前面的文章. 分析 在dao中,我们经常要做增删改查操作,如果每个对每个业务对象的操作都写一遍,代码量非常庞大. ...

  3. Elasticsearch之文档的增删改查以及ik分词器

    文档的增删改查 增加文档 使用elasticsearch-head查看 修改文档 使用elasticsearch-head查看 删除文档 使用elasticsearch-head查看 查看文档的三种方 ...

  4. java DMO及增删改查代码的自动生成

    在web开发过程中,尤其是后台管理系统的开发中,少不了增删改成的基础操作,原来我自己的做法是一份一份的拷贝粘贴,然后修改其中的不同,然而这样既枯燥无味又浪费了大量的时间,所以根据自己项目结构的特点写了 ...

  5. 【简易版】Java ArrayList(增删改查)

    1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: (1)动态的增加和减少元素 (2)实现了ICollectio ...

  6. SQLite中的增删改查

    虽然android提供了sql查询的封装方法,但是理解起来还是麻烦,所以我这里用sql语句来完成工作. 首先是建立一个类,继承SQLiteOpenHelper 这里面会建立一个数据库,并且初始化一个表 ...

  7. MongoDB(二)-- Java API 实现增删改查

    一.下载jar包 http://central.maven.org/maven2/org/mongodb/mongo-java-driver/ 二.代码实现 package com.xbq.mongo ...

  8. LR接口测试---Java Vuser之增删改查

    import lrapi.lr; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Prepared ...

  9. MongoDB(六)java操作mongodb增删改查

    java操作mysql数据库的代码我们已经了如指掌了.增删改查,java对mongodb数据库也是类似的操作,先是数据库连接.再是进行操作. 首先我们进入进入admin数据库.然后建立自己的数据库te ...

随机推荐

  1. unity 四叉树管理场景

    当场景元素过多时,需要实时的显示及隐藏物体使得性能提示,但是物体那么多,怎么知道哪些物体需要显示,哪些物体不需要显示的.当然,遍历物体判断该物体是否可以显示是最容易想到的方法,但是每次更新要遍历所有物 ...

  2. 63342 接口 奇遇 IDEA

    今天遇到一件很奇怪的事情,本来是想做一些手机页面看看效果,用IDEA 打搭建了一个静态页面网站,可是手机死活就是访问不了,网上的配置方法试过也没有用,其中包括这篇很详细博客: http://fanni ...

  3. hadoop开启Service Level Authorization 服务级认证-SIMPLE认证-过程中遇到的坑

    背景描述: 最近在进行安全扫描的时候,说hadoop存在漏洞,Hadoop 未授权访问[原理扫描],然后就参考官方文档及一些资料,在测试环境中进行了开启,中间就遇到了很多的坑,或者说自己没有想明白的问 ...

  4. C++ “::” 作用域符 双冒号

    C++ "::" 作用域符 双冒号 作用域符 :: 是作用域符,是运算符中等级最高的,它分为三种: 1)global scope(全局作用域符),用法(::name) 2)clas ...

  5. Ubuntu 17 安装Chrome浏览器

    1.进入下载文件存放目录 cd Downloads 2.下载chrome文件 2.1 32位使用如下命令 wget https://dl.google.com/linux/direct/google- ...

  6. AutoResetEvent控制线程用法

    本文主要来自一道面试题,由于之前对AutoResetEvent的用户很模糊(即使已经使用过了).面试题题目很简洁:两个线程交替打印0~100的奇偶数.你可以先动手试试,我主要是尝试在一个方法里面完成这 ...

  7. python 23 继承

    目录 继承--inheritance 1. 面向对象继承: 2. 单继承 2.1 类名执行父类的属性.方法 2.2 子类对象执行父类的属性.方法 2.3 执行顺序 2.4 既要执行子类的方法,又要执行 ...

  8. ionic app 优化三件套,让其更贴近原生app

    这里推荐一个ionic大神的简书,里面有好多关于好多ionic的技术分享! http://www.jianshu.com/u/c2e637a941ef 捣鼓了好久的ionic,终于在优化过程终于有所进 ...

  9. shell操作钉钉机器人实现告警提醒

    我们知道,之前的运维告警多通过mail 等方式通知到相应的人员,难以实现随时随地的查看.随着手机APP的发展,很多告警开始发送到IM软件上去.目前比较常用的是发送到微信和钉钉上,今天我们将重点放在钉钉 ...

  10. c#搭建jenkins自动构建环境

    这边我使用的是参数化构建过程,文笔比较差劲,就直接上干货了 1.定义构造参数 2.设置jenkins工作空间目录,在此定义了版本号和工作目录构建参数 3.配置svn 4.构建触发器,用于轮询查看svn ...