Elasticsearch支持Http类型的Restful风格API请求,需要打开9200端口。Elasticsearch服务会监听两个端口9200和9300,9200提供Http Restful访问,9300端口用于集群内节点内部通信。
    关于Elasticsearch Http Restful API可参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html 

1.Elasticsearch基本概念

1.索引(index)、文档类型(document type)、映射(mapping)、文档(document)
    索引(index):在Elasticsearch中创建索引的概念与数据库中的创建数据库类似,索引就是文档类型的集合。
    文档类型(document type):文档类型的概念与数据库中的表类似,文档类型用来区分不同文档。
    映射(mapping):映射的概念与数据库中的表结构类似,每一个文档类型都有一个映射,映射用来定义文档类型的结构。
    文档(document):文档的概念与数据库中的表中的一条记录类似,一个文档就是索引中的一条数据,一个文档也可以包含多个字段。
总结:一个Elasticsearch服务中可以有多个索引,一个索引中可以有多个文档类型,一个文档类型可以有一个映射,一个文档类型中可以有多个文档。
2.集群(cluster)、节点(node)、分片(shard)、副本(relica)
    集群(cluster): Elasticsearch支持集群以提供高可用的服务,一个集群由多个Elasticsearch服务节点组成。
    节点(node):一个Elasticsearch服务就是一个节点。
    分片(shard):在Elasticsearch中的索引可以分片,每一个分片是一个完整的Lucene索引。给索引分片的目的就是为了提高索引的可用性。
    副本(relica):副本其本质就是一个分片,副本是逻辑上的概念,它是分片的一个备份。当其分片由于某种原因不可用用时,副本就会顶替原来的分片。
补充:Elasticsearch的索引通常会有多个分片,默认是一个索引有5个分片,每个分片有1个副本。有时候也把副本称为分片,为了区分分片与副本就有了主分片之说。

2.使用Restful API简单操作ElasticSearch

1.关于curl命令
    curl是利用URL语法在命令行方式下工作的开源文件传输工具。它被广泛应用在Unix、多种Linux发行版中,并且有DOS和Win32、Win64下的移植版本。
    在学习Elasticsearch时为了方便可以使用该命令。
    在Linux系统中经常自带了curl命令,而Windows系统中默认没有,需要下载。下载地址:http://curl.haxx.se/download.html
使用示例:
  1. [lizhiwei@localhost ~]$ curl -XGET http://192.168.110.100:9200
  2. {
  3. "status" : 200,
  4. "name" : "node000",
  5. "cluster_name" : "elasticsearchTest",
  6. "version" : {
  7. "number" : "1.7.2",
  8. "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
  9. "build_timestamp" : "2015-09-14T09:49:53Z",
  10. "build_snapshot" : false,
  11. "lucene_version" : "4.10.4"
  12. },
  13. "tagline" : "You Know, for Search"
  14. }
2.索引创建与删除
    默认配置下,在创建一个文档时若这个文档所在的索引不存在就会创建这个索引。如果你不自动创建索引可以修改配置文件“elasticsearch.yml”的action.auto_create_index属性值。
创建索引设置分片数、副本数,格式 curl -XPUT http://IP:9200/<index> -d <Json数据> ,如下:
  1. [lizhiwei@localhost ElasticSearch]$ curl -XPUT http://192.168.110.100:9200/test -d @data.json
  2. {"acknowledged":true}
  3. ---------------------------------------------data.json内容
  4. {
  5. "settings" : {
  6. "index" : {
  7. "number_of_shards" : "4",
  8. "number_of_replicas" : "2"
  9. }
  10. }
  11. }
    number_of_shards:设置分片数
    number_of_replicas:设置副本数
创建索引设置映射,格式 curl -XPUT http://IP:9200/<index> -d <Json数据> ,如下:
  1. [lizhiwei@localhost ElasticSearch]$ curl -XPUT http://192.168.110.100:9200/test -d @data.json
  2. {"acknowledged":true}
  3. ---------------------------------------------data.json内容
  4. {
  5. "settings" : {
  6. "index" : {
  7. "number_of_shards" : "4",
  8. "number_of_replicas" : "1"
  9. }
  10. },
  11. "mappings" : {
  12. "DocType001" : {
  13. "_source" : {
  14. "enabled" : false
  15. },
  16. "properties" : {
  17. "field1" : {
  18. "type" : "string",
  19. "index" : "not_analyzed"
  20. },
  21. "field2" : {
  22. "type" : "string",
  23. "store" : "yes"
  24. }
  25. }
  26. }
  27. }
  28. }
    此次创建索引时除了设置分片和副本还设置了映射,这个映射设置了一个文档类型DocType001。
删除索引,格式 curl -XDELETE http://IP:9200/<index> ,如下:
  1. [lizhiwei@localhost ElasticSearch]$ curl -XDELETE http://192.168.110.100:9200/test
  2. {"acknowledged":true}
3.文档的增删查改
CURD的URL格式:http://IP:9200/<index>/<type>/[<id>]
id是可选的,不提供的话Elasticsearch会自动生成。index和type将信息进行分层,便于管理。可以将index理解为数据库,type理解为数据表。
(1).创建
# 使用自动生成ID的方式新建纪录
curl -XPOST http://IP:9200/<index>/<type> -d '{ "tag" : "bad" }'
  1. [lizhiwei@localhost ElasticSearch]$ curl -XPOST http://192.168.110.100:9200/test/People?pretty -d '{ "tag" : "bad" }'
  2. {
  3. "_index" : "test",
  4. "_type" : "People",
  5. "_id" : "AVBRAKASiFg2t1Ow-SEW",
  6. "_version" : 1,
  7. "created" : true
  8. }
# 使用指定的ID新建记录
curl -XPOST http://IP:9200/<index>/<type>/3 -d '{ "tag" : "bad" }'
  1. [lizhiwei@localhost ElasticSearch]$ curl -XPOST http://192.168.110.100:9200/test/People/3?pretty -d '{ "tag" : "bad" }'
  2. {
  3. "_index" : "test",
  4. "_type" : "People",
  5. "_id" : "3",
  6. "_version" : 1,
  7. "created" : true
  8. }
(2).查询
  1. # 查询所有的index和type的记录
  2. curl -XGET http://IP:9200/_search?pretty
  3. # 查询某个index下所有type的记录
  4. curl -XGET http://IP:9200/<index>/_search?pretty
  5. # 查询某个index下某个type下所有的记录
  6. curl -XGET http://IP:9200/<index>/<type>/_search?pretty
  7. # 使用参数查询所有的记录
  8. curl -XGET http://IP:9200/_search?q=tag:bad&pretty;
  9. # 使用参数查询某个index下的所有记录
  10. curl -XGET http://IP:9200/<index>/_search?q=tag:bad&pretty;
  11. # 使用参数查询某个index下某个type下所有的记录
  12. curl -XGET http://IP:9200/<index>/<type>/_search?q=tag:bad&pretty;
  13. # 使用JSON参数查询所有的记录,-d代表一个JSON格式的对象
  14. curl -XGET http://IP:9200/_search?pretty -d '{ "query" : { "term" : { "tag" : "bad" } } }'
  15. # 使用JSON参数查询某个index下的所有记录
  16. curl -XGET http://IP:9200/<index>/_search?pretty -d '{ "query" : { "term" : { "tag" : "bad" } } }'
  17. # 使用JSON参数查询某个index下某个type下所有的记录
  18. curl -XGET http://IP:9200/<index>/<type>/_search?pretty -d '{ "query" : { "term" : { "tag" : "bad" } } }'
例子:
  1. [lizhiwei@localhost ElasticSearch]$ curl -XGET http://192.168.110.100:9200/test/People/_search?pretty
  2. {
  3. "took" : 4,
  4. "timed_out" : false,
  5. "_shards" : {
  6. "total" : 5,
  7. "successful" : 5,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : 2,
  12. "max_score" : 1.0,
  13. "hits" : [ {
  14. "_index" : "test",
  15. "_type" : "People",
  16. "_id" : "AVBRBKcOiFg2t1Ow-SEy",
  17. "_score" : 1.0,
  18. "_source":{ "tag" : "bad" }
  19. }, {
  20. "_index" : "test",
  21. "_type" : "People",
  22. "_id" : "3",
  23. "_score" : 1.0,
  24. "_source":{ "tag" : "bad" }
  25. } ]
  26. }
  27. }
(3).更新
更新操作格式:curl -XPUT http://IP:9200/<index>/<type>/<id> -d '{ "tag" : "good" }'
  1. [lizhiwei@localhost ElasticSearch]$ curl -XPUT http://192.168.110.100:9200/test/People/3?pretty -d '{ "tag" : "good" }'
  2. {
  3. "_index" : "test",
  4. "_type" : "People",
  5. "_id" : "3",
  6. "_version" : 3,
  7. "created" : false
  8. }
(4).删除
删除操作格式:curl -XDELETE http://IP:9200/<index>/[<type>]/[<id>],<type>、<id>可选,如果不存在type删除的是整个索引,如果id不存在删除的是整个文档类型。
  1. [lizhiwei@localhost ElasticSearch]$ curl -XDELETE http://192.168.110.100:9200/test/People/3?pretty
  2. {
  3. "found" : true,
  4. "_index" : "test",
  5. "_type" : "People",
  6. "_id" : "3",
  7. "_version" : 4
  8. }
4.其他操作介绍
使用Restful API也可以监控ElasticSearch服务,例如查看集群健康:
  1. [lizhiwei@localhost ElasticSearch]$ curl -XGET http://192.168.110.100:9200/_cluster/health?pretty
  2. {
  3. "cluster_name" : "elasticsearchTest",
  4. "status" : "green",
  5. "timed_out" : false,
  6. "number_of_nodes" : 4,
  7. "number_of_data_nodes" : 4,
  8. "active_primary_shards" : 7,
  9. "active_shards" : 14,
  10. "relocating_shards" : 0,
  11. "initializing_shards" : 0,
  12. "unassigned_shards" : 0,
  13. "delayed_unassigned_shards" : 0,
  14. "number_of_pending_tasks" : 0,
  15. "number_of_in_flight_fetch" : 0
  16. }

3.常用的Restful API

  1. # 检查集群健康:
  2. curl -XGET http://127.0.0.1:9200/_cluster/health?pretty
  3. # 关闭整个集群:
  4. curl -XPOST http://127.0.0.1:9200/_cluster/nodes/_shutdown
  5. # 关闭单台节点:
  6. curl -XPOST http://127.0.0.1:9200/_cluster/nodes/{node.name}/_shutdown
  7. # 查看集群节点:
  8. curl -XGET http://127.0.0.1:9200/_cluster/nodes?pretty
  9. # 查看集群状态:
  10. curl -XGET http://127.0.0.1:9200/_cluster/state?pretty
  11. # 查看节点状态:
  12. curl -XGET http://127.0.0.1:9200/_nodes/stats?pretty
  13. # 查看本机节点:
  14. curl -XGET http://127.0.0.1:9200/_nodes/_local?pretty
  15. # 查看集群节点信息:
  16. curl -XGET http://127.0.0.1:9200/_cluster/state/nodes
  17. # 查看索引映射:
  18. curl -XGET http://127.0.0.1:9200/.marvel-kibana/_mapping?pretty
  19. 以上所有查询都可以针对json节点的子节点进行查询,关键词如:settings, os, process, jvm, thread_pool, network, transport, http , plugins
  20. 例如:
  21. curl -XGET 'http://localhost:9200/_nodes?pretty'
  22. curl -XGET 'http://localhost:9200/_nodes/process?pretty'
  23. curl -XGET 'http://localhost:9200/_nodes/os?pretty'
  24. curl -XGET 'http://localhost:9200/_nodes/settings?pretty'

-------------------------------------------------------------------------------------------------------------------------------

02.Elasticsearch入门的更多相关文章

  1. 《读书报告 -- Elasticsearch入门 》-- 安装以及简单使用(1)

    <读书报告 – Elasticsearch入门 > 第一章 Elasticsearch入门 Elasticsearch是一个实时的分布式搜索和分析引擎,使得人们可以在一定规模上和一定速度上 ...

  2. 全文搜索引擎Elasticsearch入门实践

    全文搜索引擎Elasticsearch入门实践 感谢阮一峰的网络日志全文搜索引擎 Elasticsearch 入门教程 安装 首先需要依赖Java环境.Elasticsearch官网https://w ...

  3. springboot整合elasticsearch入门例子

    springboot整合elasticsearch入门例子 https://blog.csdn.net/tianyaleixiaowu/article/details/72833940 Elastic ...

  4. ElasticSearch入门-搜索如此简单

    搜索引擎我也不是很熟悉,但是数据库还是比较了解.可以把搜索理解为数据库的like功能的替代品.因为like有以下几点不足: 第一.like的效率不行,在使用like时,一般都用不到索引,除非使用前缀匹 ...

  5. ElasticSearch入门知识扫盲

    ElasticSearch 入门介绍 tags: 第三方 lucene [toc] 1. what Elastic Search(ES)是什么 全文检索和lucene 全文检索 优点:高效,准确,分词 ...

  6. 《读书报告 -- Elasticsearch入门 》--简单使用(2)

    <读书报告 – Elasticsearch入门 > ' 第四章 分布式文件存储 这章的主要内容是理解数据如何在分布式系统中存储. 4.1 路由文档到分片 创建一个新文档时,它是如何确定应该 ...

  7. ElasticSearch入门 附.Net Core例子

    1.什么是ElasticSearch? Elasticsearch是基于Lucene的搜索引擎.它提供了一个分布式,支持多租户的全文搜索引擎,它具有HTTP Web界面和无模式JSON文档. Elas ...

  8. ElasticSearch入门点滴

    这是Elasticsearch-6.2.4 版本系列的第一篇: ElasticSearch入门 第一篇:Windows下安装ElasticSearch ElasticSearch入门 第二篇:集群配置 ...

  9. Elasticsearch Elasticsearch入门指导

    Elasticsearch入门指导 By:授客 QQ:1033553122 1. 开启elasticsearch服务器 1 2. 基本概念 2 <1> 集群(Cluster) 2 < ...

随机推荐

  1. vim中不同模式的帮助信息的查找

    vim的模式有多种,比如normal(普通模式),insert(插入模式),command(命令行模式),visual(可视化模式).相同的命令和快捷键在不同的模式下功能是不一样的,因此帮助信息也是分 ...

  2. 基于FPGA的PCIe接口实现(具体讲解了数据流向)

    时间:2014-12-09 来源:西安电子科技大学电子工程学院 作者:姜 宁,陈建春,王 沛,石 婷 摘要 PCI Express是一种高性能互连协议,被广泛应用于网络适配.图形加速器.网络存储.大数 ...

  3. PHP中把stdClass Object转array的几个方法

    方法一: 复制代码代码如下: //PHP stdClass Object转array function object_array($array) { if(is_object($array)) { $ ...

  4. Redis 学习笔记四 Mysql 与Redis的同步实践

    一.测试环境在Ubuntu kylin 14.04 64bit 已经安装Mysql.Redis.php.lib_mysqludf_json.so.Gearman. 点击这里查看测试数据库及表参考 本文 ...

  5. HDU 2844 Coin 多重背包

    Coins Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. flume+kafka

    这里演示在单机fulume环境下,kafka作为source ,chanel , sink时三种情况 下面的测试都是基于下面的基本的配置文件进行修改的 a1.sources = r1 a1.sinks ...

  7. phpadmin 装了6666端口只能在IE打开,在阿里云改了 开放端口85好了

    phpadmin 装了6666端口只能在IE打开,在阿里云改了 开放端口85好了 非常用端口谷歌浏览器识别不了phpadmin

  8. Jquery仿IGoogle实现可拖动窗口

    google可谓是ajax的特效用的淋漓尽致,google suggest, google map,igoogle 可拖动窗口等等...今天要做一个网站的类似效果,与编程人生的站长沟通了一下,仿照iG ...

  9. 关于Unity的C#基础学习(二)

    一.Debug的使用 int a=3; Debug.Log("a="+a); 二.整数的定义 int m; Debug.Log(m);  //C#比C更严谨,没有初始化的变量打印出 ...

  10. 使用HashMap,put()表示放置元素,get()表示取元素

    SortedSet可自动为元素排序. SortedSet的实现类是TreeSet:它的作用是字为添加到TreeSet中的元素排序. 与HashSet不同,TreeSet并不需要实现HashCode() ...