Elasticsearch使用REST API实现全文检索
Elasticsearch REST API
elasticsearch支持通过http请求响应服务,http请求默认使用9200断开,因此通过curl命令,可以发送http请求,并得到json返回内容。常用的REST API包括一下几个:
检查ES集群状态
curl http://localhost:9200/_cat/health?v
检查ES节点的状态
curl http://localhost:9200/_cat/nodes?v
查询所有的索引
curl http://localhost:9200/_cat/indices?v
创建索引
curl -XPUT http://localhost:9200/myindex/mytype/id -H 'Content-Type: application/json' -d '{"name":"ysl"}'
删除索引
curl -XDELETE http://localhost:9200/myindex
查询索引
curl -XGET http://localhost:9200/myindex/mytype/id
添加数据
curl -XPUT 'http://localhost:9200/kimchy/doc/1?pretty' -H 'Content-Type: application/json' -d '
{"user": "kimchy","post_date": "2009-11-15T13:12:00","message": "Trying out Elasticsearch, so far so good?"
}'
查询数据
curl -XGET 'http://localhost:9200/kimchy,another_user/_search?pretty=true' -H 'Content-Type: application/json' -d '
{"query" : {"match_all" : {}}
}'
批量添加数据
动态映射无法添加数据,不要担心!可以使用bulk命令,添加json文件内的数据
定义json数据
{"index":{"_index":"test123","_id":1}}
{"name":"ysl","age":25}
{"index":{"_index":"test123","_id":2}}
{"name":"wdd","age":25}
{"index":{"_index":"test123","_id":3}}
{"name":"yjb","age":25}
注意的是,json文件名称随意指定,第一行定义了索引和一些常用字段:
- _index定义了索引的名称,如果没有指定需要在curl命令中添加索引名称字段
- _type定义了索引的类型,如果没有指定需要在curl命令中添加索引类型字段
- _id定义了该行数据的id,如果没有指定,会随机生成一串数字。
执行命令
进入到json文件所在的目录,执行一下命令:
curl localhost:9200/索引名称/索引类型/_bulk?pretty --data-binary @data.json
注意的是:如果json文件中定义了_index和_type,那么这里可以不写(即便写了也会按照json文件中的生成)。
curl localhost:9200/_bulk?pretty --data-binary @data.json
类似的,如果按照上面我们定义了_index却没有定义_type,那么索引会是test123,类型为我们curl命令中指定的类型。
执行上述命令
curl http://localhost:9200/test123/test123/_bulk?pretty --data-binary @data.json
得到以下结果
{"took" : 1233,"errors" : false,"items" : [ {"index" : {"_index" : "test123","_type" : "test123","_id" : "1","_version" : 1,"_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"status" : 201}}, {"index" : {"_index" : "test123","_type" : "test123","_id" : "2","_version" : 1,"_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"status" : 201}} ]
}
转载于:https://www.cnblogs.com/senlinyang/p/8337076.html
Elasticsearch使用REST API实现全文检索相关推荐
- elasticsearch使用指南之Elasticsearch Document Index API详解、原理与示例
作者介绍:<RocketMQ技术内幕>作者,中间件兴趣圈微信公众号维护者. 本节将重点介绍ElasticSearch Doucment Index API(新增索引). 从上节可知,Ela ...
- ElasticSearch 使用Java Api访问集群
ElasticSearch 使用Java Api访问集群 1.创建maven工程导入pom依赖 <dependencies><dependency><groupId> ...
- 【Elasticsearch】Elasticsearch通过reroute api 重新分配分片
1.概述 转载:[重新分配分片]Elasticsearch通过reroute api重新分配分片 elasticsearch可以通过reroute api来手动进行索引分片的分配. 不过要想完全手动, ...
- 【Elasticsearch】使用 Elasticsearch Freeze index API 创建冻结索引
1.概述 首先官网已经写得很清楚了 使用 Elasticsearch Freeze index API 创建冻结索引 下面说说怎么获取冻结的索引 GET _cat/indices 执行get后,获取所 ...
- elasticsearch中的API
elasticsearch中的API es中的API按照大类分为下面几种: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作 查看A ...
- 一文讲解Elasticsearch java restful api 跨版本兼容解决方案
需求来源 之前的文章elasticsearch&kibana从6.0升级到7.9完整过程记录已经已经介绍了该需求的来源,也已经将elasticsearch的server端从6.0.0升级到了7 ...
- 探索Elasticsearch集群API
Elasticsearch提供了一个非常全面和强大的REST API,您可以使用与您的集群进行交互.为数不多的可以用API的事情如下: 检查您的集群.节点和索引健康状态和统计数据 管理集群.节点和索引 ...
- 五、Elasticsearch中的API的简单使用(Python版本)
@Author : By Runsen @Date : 2020/6/12 作者介绍:Runsen目前大三下学期,专业化学工程与工艺,大学沉迷日语,Python, Java和一系列数据分析软件.导致翘 ...
- java search 不能使用方法_ElasticSearch实战系列三: ElasticSearch的JAVA API使用教程
前言 在上一篇中介绍了ElasticSearch实战系列二: ElasticSearch的DSL语句使用教程---图文详解,本篇文章就来讲解下 ElasticSearch 6.x官方Java API的 ...
最新文章
- 防止对 Visual Basic .NET 或 C# 代码进行反相工程
- python爬虫28 | 你爬下的数据不分析一波可就亏了啊,使用python进行数据可视化...
- 锤子辩论有感——我该如何定义成功
- jQuery中的slideUp()、slideDown()、hide()、show()
- 【NC14 按之字形顺序打印二叉树】
- android 居右属性,使用layoutDirection属性设置布局靠左或靠右
- 学生管理系统(简易)7/26
- 文成小盆友python-num6 -反射 ,常用模块
- [BScroll warn]: Can not resolve the wrapper DOM.
- 纪念我那悲苦的黑色草莓米糕
- 网记者采访时,一直称李某萍为“妻子”
- ubuntu 操作系统的目录结构
- TXT文件批量生成二维码
- Python|线程和进程|阻塞|非阻塞|同步|异步|生成器和协程|资源竞争|进程间通信|aiohttp库|daemon属性值详解|语言基础50课:学习(11)
- duet二重奏hp_《二重奏》Duet
- 蓄电池内阻测量系统设计
- #ifdef __cplusplus 是什么意思?
- 服务器系统使用30金手指,金手指
- 2018年TalentQ的语言Verbal测试渣打、法国兴业、蔚来汽车网上测评(英文版)
- 3.5寸linux平板电脑,3.5寸触屏 摩托罗拉智能新机MC55登场[图文]
热门文章
- python的赋值与参数传递(python和linux切换)
- CNN for Visual Recognition (assignment1_Q1)
- altiumer designer学习
- 告别程序员生涯,一点感慨,与诸君共勉(转)
- Docker安装部署ELK教程 (Elasticsearch+Kibana+Logstash)
- Intent.ACTION_MAIN
- 用Javascript代码实现浏览器菜单命令(以下代码在 Windows XP下的浏览器中调试通过
- 数据结构03栈和队列
- java maven 操作 收集的一些命令
- 解密阿里云七武器之高性能消息服务ONS