elasticsearch rest api 学习记录

elasticsearch版本:1.4.1

学习记录

学习的博客社区

集群健康查看

epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks

1441940569 11:02:49 elasticsearch yellow 1 1 7 7 0 0 7 0

host ip heap.percent ram.percent load node.role master name

acer 169.254.9.202 32 52 d * Mys-Tech

列出所有的indices

health status index pri rep docs.count docs.deleted store.size pri.store.size

yellow open .marvel-2015.09.11 1 1 3233 0 10.5mb 10.5mb

yellow open .marvel-2015.09.10 1 1 1996 0 3.9mb 3.9mb

yellow open news 5 1 3455 0 17.8mb 17.8mb

创建索引

使用PUT请求创建一个countries的索引

curl -XPUT http://127.0.0.1:9200/countries?pretty

输出:

{

"acknowledged": true

}

查看索引列表

curl -XGET http://127.0.0.1:9200/college/_mapping?pretty

输出:

health status index pri rep docs.count docs.deleted store.size pri.store.size

yellow open countries 5 1 0 0 575b 575b

yellow open .marvel-2015.09.11 1 1 3436 0 11.4mb 11.4mb

yellow open .marvel-2015.09.10 1 1 1996 0 3.9mb 3.9mb

yellow open news 5 1 3455 0 17.8mb 17.8mb

查看mapping

curl -XGET http://127.0.0.1:9200/_cat/indices?v

输出:

{

"college" : {

"mappings" : {

"college" : {

"properties" : {

"city" : {

"type" : "string",

"store" : true,

"analyzer" : "ik"

},

"desc" : {

"type" : "string",

"store" : true,

"analyzer" : "ik"

},

"id" : {

"type" : "integer",

"store" : true

},

"name" : {

"type" : "string",

"store" : true,

"analyzer" : "ik"

}

}

}

}

}

}

Jest Client对Elasticsearch的操作

创建索引(index)

public static String createIndex(String indices) throws IOException {

JestClient jestClient = JestExample.getJestClient();

//判断索引是否存在

TypeExist indexExist = new TypeExist.Builder(indices).build();

JestResult result = jestClient.execute(indexExist);

System.out.println("index exist result " + result.getJsonString());

Object indexFound = result.getValue("found");

if (indexFound != null && indexFound.toString().equals("false")) {

//index 不存在,创建 index

System.out.println("index found == false");

JestResult createIndexresult = jestClient.execute(new CreateIndex.Builder(indices).build());

System.out.println("create index:"+createIndexresult.isSucceeded());

if(createIndexresult.isSucceeded()) {

return "ok";

}else{

return "create index fail";

}

}else{

return "ok";

}

}

创建映射(mapping)

public static String createMapping(String indices,String mappingType,String analyzer) throws IOException {

JestClient jestClient = JestExample.getJestClient();

String message = createIndex(indices);

if(!message.equals("ok")){

return "create index fail";

}

//判断mapping是否存在

TypeExist typeExist = new TypeExist.Builder(indices).addType(mappingType).build();

JestResult mappingResult = jestClient.execute(typeExist);

Object mappingFound = mappingResult.getValue("found");

if (mappingFound != null && mappingFound.toString().equals("false")) {

//索引和mapping不存在可以添加

System.out.println("mapping found == false");

XContentBuilder builder = XContentFactory.jsonBuilder()

.startObject()

.startObject(indices)

.startObject("properties")

.startObject("id").field("type", "integer").field("store", "yes").endObject()

.startObject("name").field("type", "string").field("store", "yes").field("indexAnalyzer", analyzer).field("searchAnalyzer", analyzer).endObject()

.startObject("time").field("type", "date").field("store", "yes").endObject()

.endObject()

.endObject()

.endObject();

String mappingString = builder.string();

//构造PutMapping

PutMapping putMapping = new PutMapping.Builder(indices, mappingType, mappingString).build();

JestResult maapingResult = jestClient.execute(putMapping);

return maapingResult.getJsonString();

}else {

return "mapping existing";

}

}

判断索引目录是否存在

/**

* 判断索引目录是否存在

* @throws Exception

*/

private static JestResult indicesExists(String index) throws Exception {

JestClient jestClient = JestExample.getJestClient();

IndicesExists indicesExists = new IndicesExists.Builder(index).build();

JestResult result = jestClient.execute(indicesExists);

return result;

}

存在的话返回:

{"ok" : true, "found" : true}

关闭索引

/**

* 关闭索引

* @throws Exception

*/

private static JestResult closeIndex(String index) throws Exception {

JestClient jestClient = JestExample.getJestClient();

CloseIndex closeIndex = new CloseIndex.Builder(index).build();

JestResult result = jestClient.execute(closeIndex);

return result;

}

关闭成功返回:

{"acknowledged":true}

访问被关闭的索引:

{

"error" : "IndexClosedException[[news] closed]",

"status" : 403

}

打开索引

/**

* 打开索引

* @throws Exception

*/

private static JestResult openIndex(String index) throws Exception {

JestClient jestClient = JestExample.getJestClient();

OpenIndex openIndex = new OpenIndex.Builder(index).build();

JestResult result = jestClient.execute(openIndex);

return result;

}

查看节点信息

/**

* 查看节点信息

* @throws Exception

*/

private static JestResult nodesInfo() throws Exception {

JestClient jestClient = JestExample.getJestClient();

NodesInfo nodesInfo = new NodesInfo.Builder().build();

JestResult result = jestClient.execute(nodesInfo);

return result;

}

查看集群健康信息

/**

* 查看集群健康信息

* @throws Exception

*/

private static JestResult health() throws Exception {

JestClient jestClient = JestExample.getJestClient();

Health health = new Health.Builder().build();

JestResult result = jestClient.execute(health);

return result;

}

返回结果:

{

"cluster_name": "elasticsearch",

"status": "yellow",

"timed_out": false,

"number_of_nodes": 1,

"number_of_data_nodes": 1,

"active_primary_shards": 25,

"active_shards": 25,

"relocating_shards": 0,

"initializing_shards": 0,

"unassigned_shards": 25

}

查看节点状态

/**

* 节点状态

* @throws Exception

*/

private static JestResult nodesStats() throws Exception {

JestClient jestClient = JestExample.getJestClient();

NodesStats nodesStats = new NodesStats.Builder().build();

JestResult result = jestClient.execute(nodesStats);

return result;

}

jest java_使用JestClient操作ElasticSearch的简单demo相关推荐

  1. elasticsearch新增_SpringBoot 使用JestClient操作Elasticsearch

    1.Jest介绍 操作Elasticsearch的客户端有很多,SpringBoot也提供了方式去操作,这里介绍另外一种方式去使用Elasticsearch --- JestClient JestCl ...

  2. 使用JestClient操作ElasticSearch

    可参考:  https://www.blog-china.cn/template/documentHtml/1484101683485.html https://github.com/searchbo ...

  3. Java使用JestClient操作ElasticSearch

    个人使用Java操作Elasticsearch的记录,综合网络上很多的片段,自己进行修改后的,亲测可以使用,故上传做个备份. Java操作代码: package cn.xgs.JestClient;i ...

  4. 通过contentWindow操作iframe的简单demo

    页面A; <!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8 ...

  5. 系列课程 ElasticSearch 之第 3 篇 —— 简单认识 Kibana 操作 ElasticSearch,ElasticSearch 的版本控制

    接上一篇博客继续讲解 Kibana 如何操作 ElasticSearch. 首先我们认识 Kibana 1.管理后台:http://127.0.0.1:5601/ 2.锁定左边的菜单栏 不然点击一个就 ...

  6. SpringBoot 操作 ElasticSearch 详解(万字长文)

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:超级小豆丁 http://www.mydlq.club/ar ...

  7. 万字长文:详解 Spring Boot 中操作 ElasticSearch

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 超级小豆丁 来源 | http://www.m ...

  8. 基于Python操作ElasticSearch

    基于Python操作ElasticSearch 原文:https://blog.csdn.net/hanyuyang19940104/article/details/81168763?utm_sour ...

  9. elasticsearch 客户端工具_万字长文:详解 Spring Boot 中操作 ElasticSearch

    点击上方"小强的进阶之路",选择"星标"公众号 优质文章,及时送达 预计阅读时间: 15分钟 一.ElasticSearch 简介 1.简介 ElasticSe ...

最新文章

  1. jquery插件Loadmask
  2. nethogs 查看 Linux 进程的网络使用
  3. UA MATH563 概率论的数学基础 鞅论初步5 鞅的定义
  4. AI:2020年6月22日北京智源大会演讲分享之认知神经基础专题论坛——14:20-15:00方方教授《Maps and Functions of Human Attention》
  5. 倒计时 2 天 | 神策 2019 数据驱动大会即将开幕
  6. HDU 5253 最小生成树(kruskal)+ 并查集
  7. 自然语言交流系统 phxnet团队 创新实训 项目博客 (五)
  8. 高德面试官问我:JVM内存溢出后服务还能运行吗,我一顿操作行云流水
  9. 喜庆红色C4D立体电商首焦模板|PSD分层格式,设计师大呼真香!
  10. 竖排书A5双面打印设置指南
  11. VB连接oracle数据库
  12. docker安装centos7镜像
  13. 粒子群算法-讲解+实例
  14. 安卓模拟器安装xposed和插件
  15. TEMPO ESTIMATION近几年文章总结
  16. 日知录-章0:当我们在谈论大数据时,我们在谈什么?
  17. 逸仙电商将携Galenic法国科兰黎、EVE LOM参展进博会 | 进博会倒计时
  18. 安装minidwep-gtk出现了“离开目录”的错误
  19. 应届生如何轻松通过Java面试
  20. [校园网]绕过校园网使用自己服务器流量教程

热门文章

  1. 【ElasticSearch】Es 源码之 IndicesService 源码解读
  2. 【clickhouse】clickhouse 表引擎之 Merge
  3. 【Docker】Docker java shell ssh
  4. 10-20-030-简介-Kafka Briker IO
  5. 【Spring】Spring Boot 和 Redis 常⽤操作
  6. Mac下安装Flink的local模式(flink-1.5.2)
  7. 分布式任务调度平台XXL-JOB一
  8. shiro mysql_Shiro系列之Shiro+Mysql实现用户认证(Authentication)
  9. 批处理框架 Spring Batch,数据迁移量过大如何保证内存?
  10. 记一次项目代码重构:使用Spring容器干掉条件判断