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. JavaWeb无框架,借助反射采用精巧设计模式实现放微信PC聊天页面

    本周开始在写仿写一个微信PC端的聊天页面,没有使用ssh.ssm等框架,采用JavaWeb.反射.MySQL.C3P0等技术.这里把其中和核心技术列出来请大家指教. 与传统JavaWeb项目的区别 传 ...

  2. linux后台运行的几种方式

    1.nohup将程序以忽略挂起信号的方式运行起来 补充说明nohup命令 可以将程序以忽略挂起信号的方式运行起来,被运行的程序的输出信息将不会显示到终端.无论是否将 nohup 命令的输出重定向到终端 ...

  3. 《统计学习方法》极简笔记P5:决策树公式推导

    <统计学习方法>极简笔记P2:感知机数学推导 <统计学习方法>极简笔记P3:k-NN数学推导 <统计学习方法>极简笔记P4:朴素贝叶斯公式推导

  4. [程序人生]那些IT界“活久见”的奇葩现象

    常言道,人活久了什么稀奇古怪的事都会见到.本文盘点几件刚毕业工作时想当然,工作若干年后啪啪打脸的“奇葩”事. (1)去年推荐一朋友来我们公司面试时,朋友说起当年她去某游戏公司时,那公司HR说这家公司是 ...

  5. SQL奇技淫巧(01):给查出的数据排序编个号【row_number() over(order by c)】(mysql,db2,oracle,sqlserver通用)

    我们天天都在跟数据库打交道,写下的代码不计其数,写下的SQL更是可以绕地球几圈.这里收集关于SQL的神奇语法及用法,虽然你可能没有用过,但这些SQL却可以在关键的时候,派上用场. 我对SQL语句的理解 ...

  6. 怎样才算精通Linux

    1.掌握至少50个以上的常用命令(包括grep.awk.sed.ps.find等等吧,熟练使用,基础的选项不用man) 2.熟悉Gnome/KDE等X-windows桌面环境操作 3.掌握.tgz.. ...

  7. spring加载bean流程解析

    spring作为目前我们开发的基础框架,每天的开发工作基本和他形影不离,作为管理bean的最经典.优秀的框架,它的复杂程度往往令人望而却步.不过作为朝夕相处的框架,我们必须得明白一个问题就是sprin ...

  8. mysql安装-yum方式

    1.环境 查看当前系统环境,使用的是 centos release 6.5 (Final). 2.检查当前系统是否已经安装过mysql rpm -qa | grep mysql 3.如果有,那么删除已 ...

  9. 分享一个赚钱方法:用趣分类app在家轻松赚钱

    什么是趣分类 近期,垃圾分类是社会各界和广大市民关心的一个热门话题,随着垃圾分类工作的推进,各地都掀起学习垃圾分类的热潮.为了我们的美好生活,打响"垃圾分类"这场硬仗刻不容缓.据了 ...

  10. ACM-ICPC 2018 徐州赛区(网络赛)

    目录 A. Hard to prepare B.BE, GE or NE F.Features Track G.Trace H.Ryuji doesn't want to study I.Charac ...