[[TOC]]

一. 请求形式

curl是linux 命令
使用命令:curl [http://curl.haxx.se](http://curl.haxx.se)
这是最简单的使用方法。
用这个命令获得了[http://curl.haxx.se](http://curl.haxx.se)指向的页面。同样,如果这里的URL指向的是一个文件或者一幅图都可以直接下载到本地。如果下载的是HTML文档,那么缺省的将不显示文件头部,即HTML文档的header。要全部显示,请加参数 -i,要只显示头部,用参数 -I。任何时候,可以使用 -v 命令看curl是怎样工作的,它向服务器发送的所有命令都会显示出来。为了断点续传,可以使用-r参数来指定传输范围。
交互命令形式:

  1. curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'

被 < > 标记的部件:

  1. VERB 适当的 HTTP 方法 或 谓词 : GET`、 `POST`、 `PUT`、 `HEAD 或者 `DELETE`。
  2. PROTOCOL http 或者 https`(如果你在 Elasticsearch 前面有一个 `https 代理)
  3. HOST Elasticsearch 集群中任意节点的主机名,或者用 localhost 代表本地机器上的节点。
  4. PORT 运行 Elasticsearch HTTP 服务的端口号,默认是 9200 。
  5. PATH API 的终端路径(例如 _count 将返回集群中文档数量)。Path 可能包含多个组件,例如:_cluster/stats 和 _nodes/stats/jvm 。
  6. QUERY_STRING 任意可选的查询字符串参数 (例如 ?pretty 将格式化地输出 JSON 返回值,使其更容易阅读)
  7. BODY 一个 JSON 格式的请求体 (如果请求需要的话)

例子: 计算集群文档的数量

  1. curl -XGET 'http://master66:9200/_count?pretty' -d '
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }'

如果是http请求,直接输入:

  1. http://10.17.139.66:9200/_count?pretty' -d '{"query":{"match_all":{}}}'

二. 常用命令

1. 检查es版本信息

  1. `curl master66:9200`

例:

  1. >curl 10.37.139.66:9200
  2. {
  3. "name" : "master66",
  4. "cluster_name" : "SERVICE-ELASTICSEARCH-d6b815c84b7b4619b03dfdb47f160f0e",
  5. "version" : {
  6. "number" : "2.1.1",
  7. "build_hash" : "${buildNumber}",
  8. "build_timestamp" : "2017-01-22T07:33:54Z",
  9. "build_snapshot" : false,
  10. "lucene_version" : "5.3.1"
  11. },
  12. "tagline" : "You Know, for Search"
  13. }

2. 查看集群状态:_cat
_cat命令可以让你查看你有那些api去查看集群状态

  1. curl http://master66:9200/_cat
  2. /_cat/allocation
  3. /_cat/shards
  4. /_cat/shards/{index}
  5. /_cat/master
  6. /_cat/nodes
  7. /_cat/indices
  8. …….

命令说明:

  1. • Allocation: information about the resource allocation on each server in the cluster
  2. • Shards: information about the allocation of (specific) shards on each server in the cluster
  3. • Master: information about the master server in the cluster
  4. • Indices: information about (specific) indices in the cluster
  5. • Segments: information on how an index is segmented across several servers in the cluster
  6. • Count: count documents in (specific) indices
  7. • Recovery: information about shard recovery when a shard is moved to a different node in the cluster
  8. • Health: display the cluster health
  9. • Pending tasks: as the name indicates. What is the server doing right
  10. now?
  11. • Aliases: information about aliases given to specific indices
  12. • Thread pool: thread pool statistics per node
  13. • Plugins: a list of running plugins per node
  14. • Fielddata: information about loaded body & text fields per node

2.1 查看健康状态

  1. 1 http://master66:9200/_cat/health?v
  2. 2 curl master66:9200/_cat/health?v

2.2 查看节点列表

  1. 1 http://master66:9200/_cat/nodes?v
  2. 2 curl master66:9200/_cat/nodes?v

2.3 列出所有索引及存储大小

  1. 1 http://master66:9200/_cat/indices?v
  2. 2 curl master66:9200/_cat/indices?v

3. 索引管理
3.1 创建索引
创建索引名为XX,默认会有5个分片,1个索引, 可以添加一些设置:

  1. curl -XPUT 'http://master66:9200/ys_test' -d '{
  2. "settings" : {
  3. "analysis" : {
  4. "analyzer" : {
  5. "ik" : {
  6. "tokenizer" : "ik_max_word"
  7. }
  8. }
  9. }
  10. },
  11. "mappings" : {
  12. "person" : {
  13. "properties" : {
  14. "name" : { "type" : "string","analyzer": "ik_max_word"},
  15. "age" : {"type":"integer"},
  16. "sex":{"type" : "string","analyzer": "ik_max_word"}
  17. }
  18. }
  19. }
  20. }'

3.2 删除索引

  1. curl -XDELETE http://master66:9200/ys_test

也可以删除多个索引:

  1. curl –XDELETE http://master66:9200/index_one11,index_two22
  2. curl -XDELETE http://master66:9200/index_*

3.3 查看属性:_settings

  1. curl -XGET http://master66:9200/ys_test/_settings?pretty
  2. curl -XGET http://master66:9200/ys_test/_mapping?pretty

4. 别名
4.1 创建别名

  1. 1) _alias:单个操作
  2. 2) _aliases:多个操作, (多个操作合起来是)原子性的操作

已知存在一个索引dm_v1, 为其添加一个别名dm_alias

  1. 1) curl -XPUT 'localhost:9200/dm_v1/_alias/dm_alias'
  2. 2) curl -XPOST 'http://localhost:9200/_aliases' -d '
  3. {
  4. "actions" : [
  5. { "add" : { "index" : "dm_v1", "alias" : "dm_alias" } }
  6. ]
  7. }'

4.2 删除别名:

  1. 1) curl -XDELETE 'localhost:9200/dm_v1/_alias/dm_alias'
  2. 2) curl -XPOST 'http://localhost:9200/_aliases' -d '
  3. {
  4. "actions" : [
  5. { "remove" : { "index" : "dm_v1", "alias" : "dm_alias" } }
  6. ]
  7. }'

4.3 在线应用的索引迁移
删除别名的同时添加别名到新的索引,该操作是原子性的,不用担心存在别名没有指向任何索引的瞬间:

  1. curl -XPOST 'http://localhost:9200/_aliases' -d '
  2. {
  3. "actions" : [
  4. { "remove" : { "index" : "dm_v1", "alias" : "dm_alias" } },
  5. { "add" : { "index" : "dm_v2", "alias" : "dm_alias" } }
  6. ]
  7. }'

应用: 如果在线应用有需求更改,需要重新设计索引.就可以利用别名在零停机下从旧索引迁移到新索引
4.4 通过别名查询所指向的索引

  1. curl -XGET 'localhost:9200/_alias/dm_alias'
  2. curl -XGET 'localhost:9200/_alias/dm*'

查询指向该索引下的所有别名:

  1. curl -XGET 'localhost:9200/dm_v1/_alias/*'

4.5 别名的高级使用

  1. 聚合多个索引
    POST /_aliases
    {
    “actions”: [
    {
    “add”: {
    “index”: “dm_v1”,
    “alias”: “dm”
    }
    },
    {
    “add”: {
    “index”: “dm_v2”,
    “alias”: “dm”
    }
    }
    ]
    }

注:为索引dm_v1和索引dm_v2创建了一个共同的索引别名dm,这样在对dm的(仅限于)读,会同时作用于dm_v1和dm_v2。
2)filtered的别名

  1. POST /_aliases
  2. {
  3. "actions": [
  4. {
  5. "add": {
  6. "index": "my_index",
  7. "alias": "my_index__teamA_alias",
  8. "filter":{
  9. "term":{
  10. "team":"teamA"
  11. }
  12. }
  13. }
  14. },
  15. {
  16. "add": {
  17. "index": "my_index",
  18. "alias": "my_index__teamB_alias",
  19. "filter":{
  20. "term":{
  21. "team":"teamB"
  22. }
  23. }
  24. }
  25. },
  26. {
  27. "add": {
  28. "index": "my_index",
  29. "alias": "my_index__team_alias"
  30. }
  31. }
  32. ]
  33. }

.

  1. GET /my_index__teamA_alias/_search 只能看到teamA的数据
  2. GET /my_index__teamB_alias/_search 只能看到teamB的数据
  3. GET /my_index__team_alias/_search 既能看到teamA的,也能看到teamB的数据

三. 文档的管理

1. 添加
指定: _index 、 _type 和 _id, 如果已经存在会强制覆盖
注: 在相同的 _index 、 _type 和 _id 不存在时才接受我们的添加请求URL 末端使用 /_create

  1. curl -XPUT 'http://master66:9200/ys_test/person/1' -d '{
  2. "name":"刘明","age":"40","sex":"男"
  3. }'
  4. curl -XPUT 'http://master66:9200/ys_test/person/2' -d '{
  5. "name":"李明","age":"20","sex":"男"
  6. }'
  7. curl -XPUT 'http://master66:9200/ys_test/person/3' -d '{
  8. "name":"李莉","age":"25","sex":"女"
  9. }'
  10. curl -XPUT 'http://master66:9200/ys_test/person/4' -d '{
  11. "name":"刘明明","age":"35","sex":"男"
  12. }'
  13. curl -XPUT 'http://master66:9200/ys_test/person/5' -d '{
  14. "name":"刘明宇","age":"40","sex":"男"
  15. }'

ys_test/person/3/ 已经存在,下面的命令会报错

  1. curl -XPUT 'http://master66:9200/ys_test/person/3/_create' -d '{
  2. "name":"zhangyu2","age":"26"
  3. }'

2. 删除
指定你要删除的文档. _index/_type/_id

  1. curl -XDELETE 'http://master66:9200/ys_test/person/4'

3. 部分文档更新: _update
只更新部分文档时, json请求中加doc字段:

  1. curl -XPOST 'master66:9200/ys_test/person/2/_update' -d '{
  2. "doc" :{
  3. "age" : 23,
  4. "name" : "李霞"
  5. }
  6. }'

四. 查询命令:_search

1. DSL命令综述:

  1. {
  2. size: # number of results to return (defaults to 10) 返回多少个值
  3. from: # offset into results (defaults to 0) 偏移量
  4. fields: # 只在指定字段查询,并返回指定字段.
  5. _source: # 全部字段查询,但是返回结果中只返回指定字段,
  6. sort: # define sort order - see http://elasticsearch.org/guide/reference/api/search/sort.html
  7. query: {
  8. "query" object following the Query DSL: http://elasticsearch.org/guide/reference/query-dsl/},
  9. aggs: {
  10. # 统计数据
  11. Facets provide summary information about a particular field or fields in the data
  12. },
  13. filter: {
  14. #filter objects
  15. #a filter is a simple "filter" (query) on a specific field.
  16. #Simple means e.g. checking against a specific value or range of values
  17. },
  18. }

2. query
2.1 query 和 filter
fiter是精确查询,对待的文档检索的结果是 是/否 ;query对应文档检索是对文档相关性评分。
表现(性能Performance)区别:filter返回是和条件匹配的一个简单的列表这是很快可以计算得到的并且也很容易在内存中做缓存;query不仅要找到匹配的文档,而且还要计算每个文档的相关性(评分),这就很明显比filter花费更多的计算。
query与filter 区别如下:

  1. query是要相关性评分的,filter不要;
  2. query结果无法缓存,filter可以。

所以,选择参考:

  1. (1) 全文搜索、评分排序,使用query;
  2. (2) 是非过滤,精确匹配,使用filter。

注:

1 query和filter搜索命令是可以互用的. 2 不知道搜索那个字段时:可以用_all指定所有字段 3 index, type, id 也可以作为字段. 分别是_index, _type, _id

2.2 match: 查询语句匹配

  1. {
  2. "query": {
  3. "match": {
  4. "name" : { # content是es中的field
  5. "query" : "刘明" # query是es中命令字段, 是特定的
  6. }
  7. }
  8. }
  9. }

简写:

  1. {
  2. "query": {
  3. "match": {
  4. "name" : "刘明"
  5. }
  6. }
  7. }

全字段搜索

  1. {
  2. "query": {
  3. "match": {
  4. "_all" : "刘明"
  5. }
  6. }
  7. }

2.3 match_phrase: 精确匹配
不知道搜索那个字段时:可以用_all指定所有字段

  1. {
  2. "query": {
  3. "match_phrase": {
  4. "name" : {
  5. "query" : "刘明明"
  6. }
  7. }
  8. }
  9. }

简写

  1. {
  2. "query": {
  3. "match_phrase": {
  4. "name" : "刘明明"
  5. }
  6. }
  7. }

完全匹配可能比较严,我们会希望有个可调节因子,少匹配一个也满足,那就需要使用slop。

  1. {
  2. "query": {
  3. "match_phrase": {
  4. "name" : {
  5. "query" : "刘明明",
  6. "slop" : 1
  7. }
  8. }
  9. }
  10. }

4. multi_match: 多字段匹配
如果我们希望两个字段进行匹配,其中一个字段有这个文档就满足的话,使用multi_match

  1. {
  2. "query": {
  3. "multi_match": {
  4. "query" : "刘明",
  5. "fields" : ["name", "sex"]
  6. }
  7. }
  8. }

全匹配的文档占的评分比较高: 加字段"best_fields"
越多字段匹配的文档评分越高,就要使用most_fields
这个词条的分词词汇是分配到不同字段中的,那么就使用cross_fields

  1. {
  2. "query": {
  3. "multi_match": {
  4. "query": "刘明",
  5. "type": "best_fields",
  6. "fields" : ["name", "sex"],
  7. "tie_breaker": 0.3
  8. }
  9. }
  10. }

5. term and terms: 完全匹配, 不使用分词器
term 针对一个字段:

  1. {
  2. "query": {
  3. "term": {
  4. "name": "刘明"
  5. }
  6. }
  7. }

terms针对多个值, 相当于sql语句中的in

  1. {
  2. "filter": {
  3. "terms": {
  4. "sex": [
  5. "男",
  6. "女"
  7. ]
  8. }
  9. }
  10. }

6. 前缀查询(Prefix Query)

  1. {
  2. "query": {
  3. "prefix": {
  4. "name": "李"
  5. }
  6. }
  7. }

7.通配符: wildcard
它使用标准的shell通配符?用来匹配任意字符,*用来匹配零个或者多个字符

  1. {
  2. "query": {
  3. "wildcard": {
  4. "name": "李*"
  5. }
  6. }
  7. }

8.正则: regexp

  1. {
  2. "query": {
  3. "regexp": {
  4. "name": "刘.*"
  5. }
  6. }
  7. }

9.ids 匹配多个id

  1. {
  2. "query": {
  3. "ids": {
  4. "type": "my_type",
  5. "values": [
  6. "1",
  7. "4",
  8. "100"
  9. ]
  10. }
  11. }
  12. }

10. range 范围查询

  1. gt(>)、lt(< less then)、gte(>=)、lte(<=)

.

  1. {
  2. "query": {
  3. "range": {
  4. "age": {
  5. "gt": 30
  6. }
  7. }
  8. }
  9. }

11. exists and missing 字段是否存在
exists过滤指定字段没有值的文档, missing过滤缺失字段的文档

  1. {
  2. "query": {
  3. "exists": {
  4. "field": "name"
  5. }
  6. }
  7. }

12.组合过滤/查询
bool查询的使用

  1. must
  2. 返回的文档必须满足must子句的条件,并且参与计算分值
  3. filter
  4. 返回的文档必须满足filter子句的条件。但是不会像Must一样,参与计算分值
  5. should
  6. 返回的文档可能满足should子句的条件。在一个Bool查询中,如果没有must或者filter,有一个或者多个should子句,那么只要满足一个就可以返回。
  7. minimum_should_match参数定义了至少满足几个子句。
  8. must_not
  9. 返回的文档必须不满足must_not定义的条件。

.

  1. {
  2. "query": {
  3. "bool": {
  4. "must": {
  5. "term": {
  6. "sex": "男"
  7. }
  8. },
  9. "filter": {
  10. "match": {
  11. "name": "刘"
  12. }
  13. },
  14. "must_not": {
  15. "range": {
  16. "age": {
  17. "from": 10,
  18. "to": 38
  19. }
  20. }
  21. }
  22. }
  23. }
  24. }

13. 聚合:_search
官方指南
聚合

  1. • 高阶概念
  2. • 尝试聚合
  3. • 条形图
  4. • 按时间统计
  5. • 范围限定的聚合
  6. • 过滤和聚合
  7. • 多桶排序
  8. • 近似聚合
  9. • 通过聚合发现异常指标
  10. • Doc Values and Fielddata

13.1 metric度量
类似于sql 中的avg、max、min 等方法
13.1.1平均值,最大,最小,求和,唯一值

  1. 平均值:avg
  2. 最大:max
  3. 最小:min
  4. 唯一值: cardinality

.

  1. http://10.17.139.66:9200/ys_test/_search?size=0
  2. {
  3. "aggs": {
  4. "min_age": {
  5. "min": {
  6. "field": "age"
  7. }
  8. }
  9. }
  10. }

13.2 桶Buckets
Buckets相当于SQL中的分组group by

  1. {
  2. "aggs" : {
  3. "sexs" : { // 这次聚合的名字,没有意义
  4. "terms" : {
  5. "field" : "sex"
  6. }
  7. }
  8. }
  9. }

3.3 统计一个索引下,各类型的文档总数

  1. curl -XGET '10.17.139.66:9200/relation/_search?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "size" : 0,
  4. "aggs": {
  5. "type_count": {
  6. "terms": {
  7. "field": "_type",
  8. "size": 0
  9. }
  10. }
  11. }
  12. }'

注:

第一个size是为了不显示匹配中的文档(太多的文档数据, 不利于人观看返回结果) 第二个size是为了让每个聚合的类型都返回结果并显示,而不是在"sum_other_doc_count"字段中显示.在relation索引的统计过程中 由于relation类型比较多,就发现个别类型不显示在聚合结果里,而是在sum_other_doc_count字段中了。

五:总结

es中的API按照大类分为下面几种:

  1. • 文档API: 提供对文档的增删改查操作
  2. • 搜索API: 提供对文档进行某个字段的查询
  3. • 索引API: 提供对索引进行操作
  4. • 查看API: 按照更直观的形式返回数据,更适用于控制台请求展示
  5. • 集群API: 对集群进行查看和操作的API

文档API

  1. • Index API: 创建并建立索引
  2. • Get API: 获取文档
  3. • DELETE API: 删除文档
  4. • UPDATE API: 更新文档
  5. • Multi Get API: 一次批量获取文档
  6. • Bulk API: 批量操作,批量操作中可以执行增删改查
  7. • DELETE By Query API: 根据查询删除
  8. • Term Vectors: 词组分析,只能针对一个文档
  9. • Multi termvectors API: 多个文档的词组分析
  10. • multiGet的时候内部的行为是将一个请求分为多个,到不同的node中进行请求,再将结果合并起来。

如果某个node的请求查询失败了,那么这个请求仍然会返回数据,只是返回的数据只有请求成功的节点的查询数据集合。
词组分析的功能能查出比如某个文档中的某个字段被索引分词的情况。
对应的接口说明和例子
搜索API

  1. • 基本搜索接口: 搜索的条件在url中
  2. • DSL搜索接口: 搜索的条件在请求的body中
  3. • 搜索模版设置接口: 可以设置搜索的模版,模版的功能是可以根据不同的传入参数,进行不同的实际搜索
  4. • 搜索分片查询接口: 查询这个搜索会使用到哪个索引和分片
  5. • Suggest接口: 搜索建议接口,输入一个词,根据某个字段,返回搜索建议。
  6. • 批量搜索接口: 把批量请求放在一个文件中,批量搜索接口读取这个文件,进行搜索查询
  7. • Count接口: 只返回符合搜索的文档个数
  8. • 文档存在接口: 判断是否有符合搜索的文档存在
  9. • 验证接口: 判断某个搜索请求是否合法,不合法返回错误信息
  10. • 解释接口: 使用这个接口能返回某个文档是否符合某个查询,为什么符合等信息
  11. • 抽出器接口: 简单来说,可以用这个接口指定某个文档符合某个搜索,事先未文档建立对应搜索

对应的接口说明和例子
索引API

  1. • 创建索引接口(POST my_index)
  2. • 删除索引接口(DELETE my_index)
  3. • 获取索引信息接口(GET my_index)
  4. • 索引是否存在接口(HEAD my_index)
  5. • 打开/关闭索引接口(my_index/_close, my_index/_open)
  6. • 设置索引映射接口(PUT my_index/_mapping)
  7. • 获取索引映射接口(GET my_index/_mapping)
  8. • 获取字段映射接口(GET my_index/_mapping/field/my_field)
  9. • 类型是否存在接口(HEAD my_index/my_type)
  10. • 删除映射接口(DELTE my_index/_mapping/my_type)
  11. • 索引别名接口(_aliases)
  12. • 更新索引设置接口(PUT my_index/_settings)
  13. • 获取索引设置接口(GET my_index/_settings)
  14. • 分析接口(_analyze): 分析某个字段是如何建立索引的
  15. • 建立索引模版接口(_template): 为索引建立模版,以后新创建的索引都可以按照这个模版进行初始化
  16. • 预热接口(_warmer): 某些查询可以事先预热,这样预热后的数据存放在内存中,增加后续查询效率
  17. • 状态接口(_status): 索引状态
  18. • 批量索引状态接口(_stats): 批量查询索引状态
  19. • 分片信息接口(_segments): 提供分片信息级别的信息
  20. • 索引恢复接口(_recovery): 进行索引恢复操作
  21. • 清除缓存接口(_cache/clear): 清除所有的缓存
  22. • 输出接口(_flush)
  23. • 刷新接口(_refresh)
  24. • 优化接口(_optimize): 对索引进行优化
  25. • 升级接口(_upgrade): 这里的升级指的是把索引升级到lucence的最新格式

对应的接口说明和例子
查看API

  1. • 查看别名接口(_cat/aliases): 查看索引别名
  2. • 查看分配资源接口(_cat/allocation)
  3. • 查看文档个数接口(_cat/count)
  4. • 查看字段分配情况接口(_cat/fielddata)
  5. • 查看健康状态接口(_cat/health)
  6. • 查看索引信息接口(_cat/indices)
  7. • 查看master信息接口(_cat/master)
  8. • 查看nodes信息接口(_cat/nodes)
  9. • 查看正在挂起的任务接口(_cat/pending_tasks)
  10. • 查看插件接口(_cat/plugins)
  11. • 查看修复状态接口(_cat/recovery)
  12. • 查看线城池接口(_cat/thread_pool)
  13. • 查看分片信息接口(_cat/shards)
  14. • 查看lucence的段信息接口(_cat/segments)

对应的接口说明和例子
集群API

  1. • 查看集群健康状态接口(_cluster/health)
  2. • 查看集群状况接口(_cluster/state)
  3. • 查看集群统计信息接口(_cluster/stats)
  4. • 查看集群挂起的任务接口(_cluster/pending_tasks)
  5. • 集群重新路由操作(_cluster/reroute)
  6. • 更新集群设置(_cluster/settings)
  7. • 节点状态(_nodes/stats)
  8. • 节点信息(_nodes)
  9. • 节点的热线程(_nodes/hot_threads)
  10. • 关闭节点(\nodes/_master/_shutdown)

对应的接口说明和例子
附录: