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

使用示例:

[lizhiwei@localhost ~]$ curl -XGET http://192.168.110.100:9200
{
  "status" : 200,
  "name" : "node000",
  "cluster_name" : "elasticsearchTest",
  "version" : {
    "number" : "1.7.2",
    "build_hash" : "e43676b1385b8125d647f593f7202acbd816e8ec",
    "build_timestamp" : "2015-09-14T09:49:53Z",
    "build_snapshot" : false,
    "lucene_version" : "4.10.4"
  },
  "tagline" : "You Know, for Search"
}

2.索引创建与删除

”的action.auto_create_index属性值。

创建索引设置分片数、副本数,格式 curl -XPUT http://IP:9200/<index> -d <Json数据> ,如下:

[lizhiwei@localhost ElasticSearch]$ curl -XPUT http://192.168.110.100:9200/test -d @data.json
{"acknowledged":true}
---------------------------------------------data.json内容
{
    "settings" : {
        "index" : {
            "number_of_shards" : "4",
            "number_of_replicas" : "2"
        }
    }
}

number_of_shards:设置分片数

number_of_replicas:设置副本数

创建索引设置映射,格式 curl -XPUT http://IP:9200/<index> -d <Json数据> ,如下:

[lizhiwei@localhost ElasticSearch]$ curl -XPUT http://192.168.110.100:9200/test -d @data.json
{"acknowledged":true}
---------------------------------------------data.json内容
{
    "settings" : {
        "index" : {
            "number_of_shards" : "4",
            "number_of_replicas" : "1"
        }
    },
    "mappings" : {
        "DocType001" : {
            "_source" : {
                "enabled" : false
            },
            "properties" : {
                "field1" : {
                    "type" : "string",
                    "index" : "not_analyzed"
                },
                "field2" : {
                    "type" : "string",
                    "store" : "yes"
                }
            }
        }
    }
}

DocType001。

删除索引,格式 curl -XDELETE http://IP:9200/<index> ,如下:

[lizhiwei@localhost ElasticSearch]$ curl -XDELETE http://192.168.110.100:9200/test
{"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" }'

[lizhiwei@localhost ElasticSearch]$ curl -XPOST http://192.168.110.100:9200/test/People?pretty -d '{ "tag" : "bad" }'
{
  "_index" : "test",
  "_type" : "People",
  "_id" : "AVBRAKASiFg2t1Ow-SEW",
  "_version" : 1,
  "created" : true
}

# 使用指定的ID新建记录

curl -XPOST http://IP:9200/<index>/<type>/3 -d '{ "tag" : "bad" }'

[lizhiwei@localhost ElasticSearch]$ curl -XPOST http://192.168.110.100:9200/test/People/3?pretty -d '{ "tag" : "bad" }' 
{
  "_index" : "test",
  "_type" : "People",
  "_id" : "3",
  "_version" : 1,
  "created" : true
}

(2).查询

# 查询所有的index和type的记录
curl -XGET http://IP:9200/_search?pretty
# 查询某个index下所有type的记录
curl -XGET http://IP:9200/<index>/_search?pretty
# 查询某个index下某个type下所有的记录
curl -XGET http://IP:9200/<index>/<type>/_search?pretty
# 使用参数查询所有的记录
curl -XGET http://IP:9200/_search?q=tag:bad&pretty;
# 使用参数查询某个index下的所有记录
curl -XGET http://IP:9200/<index>/_search?q=tag:bad&pretty;
# 使用参数查询某个index下某个type下所有的记录
curl -XGET http://IP:9200/<index>/<type>/_search?q=tag:bad&pretty;
# 使用JSON参数查询所有的记录,-d代表一个JSON格式的对象
curl -XGET http://IP:9200/_search?pretty -d '{ "query" : { "term" : { "tag" : "bad" } } }'
# 使用JSON参数查询某个index下的所有记录
curl -XGET http://IP:9200/<index>/_search?pretty -d '{ "query" : { "term" : { "tag" : "bad" } } }'
# 使用JSON参数查询某个index下某个type下所有的记录
curl -XGET http://IP:9200/<index>/<type>/_search?pretty -d '{ "query" : { "term" : { "tag" : "bad" } } }'

例子:

[lizhiwei@localhost ElasticSearch]$ curl -XGET http://192.168.110.100:9200/test/People/_search?pretty
{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 1.0,
    "hits" : [ {
      "_index" : "test",
      "_type" : "People",
      "_id" : "AVBRBKcOiFg2t1Ow-SEy",
      "_score" : 1.0,
      "_source":{ "tag" : "bad" }
    }, {
      "_index" : "test",
      "_type" : "People",
      "_id" : "3",
      "_score" : 1.0,
      "_source":{ "tag" : "bad" }
    } ]
  }
}

(3).更新

更新操作格式:curl -XPUT http://IP:9200/<index>/<type>/<id> -d '{ "tag" : "good" }'

[lizhiwei@localhost ElasticSearch]$ curl -XPUT http://192.168.110.100:9200/test/People/3?pretty -d '{ "tag" : "good" }'
{
  "_index" : "test",
  "_type" : "People",
  "_id" : "3",
  "_version" : 3,
  "created" : false
}

(4).删除

删除操作格式:curl -XDELETE http://IP:9200/<index>/[<type>]/[<id>],<type>、<id>可选,如果不存在type删除的是整个索引,如果id不存在删除的是整个文档类型。

[lizhiwei@localhost ElasticSearch]$ curl -XDELETE http://192.168.110.100:9200/test/People/3?pretty
{
  "found" : true,
  "_index" : "test",
  "_type" : "People",
  "_id" : "3",
  "_version" : 4
}

4.其他操作介绍

使用Restful API也可以监控ElasticSearch服务,例如查看集群健康:

[lizhiwei@localhost ElasticSearch]$ curl -XGET http://192.168.110.100:9200/_cluster/health?pretty
{
  "cluster_name" : "elasticsearchTest",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 4,
  "number_of_data_nodes" : 4,
  "active_primary_shards" : 7,
  "active_shards" : 14,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0
}

要获取更全面的Restful API可参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html

3.常用的Restful API

# 检查集群健康:
curl -XGET http://127.0.0.1:9200/_cluster/health?pretty
# 关闭整个集群: 
curl -XPOST http://127.0.0.1:9200/_cluster/nodes/_shutdown
# 关闭单台节点:
curl -XPOST http://127.0.0.1:9200/_cluster/nodes/{node.name}/_shutdown
# 查看集群节点:
curl -XGET http://127.0.0.1:9200/_cluster/nodes?pretty
# 查看集群状态:
curl -XGET http://127.0.0.1:9200/_cluster/state?pretty
# 查看节点状态:
curl -XGET http://127.0.0.1:9200/_nodes/stats?pretty
# 查看本机节点:
curl -XGET http://127.0.0.1:9200/_nodes/_local?pretty
# 查看集群节点信息:
curl -XGET http://127.0.0.1:9200/_cluster/state/nodes
# 查看索引映射:
curl -XGET http://127.0.0.1:9200/.marvel-kibana/_mapping?pretty

以上所有查询都可以针对json节点的子节点进行查询,关键词如:settings, os, process, jvm, thread_pool, network, transport, http , plugins
例如:
curl -XGET 'http://localhost:9200/_nodes?pretty'
curl -XGET 'http://localhost:9200/_nodes/process?pretty'
curl -XGET 'http://localhost:9200/_nodes/os?pretty'
curl -XGET 'http://localhost:9200/_nodes/settings?pretty'

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