ES学习

Posted by Alexander Wang on May 5, 2017

curl

一个 Elasticsearch 请求和任何 HTTP 请求一样由若干相同的部件组成:

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

< > 标记的部件:

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

集群中文档的数量这个:

curl -XGET 'http://localhost:9200/_count?pretty' -d '
{
    "query": {
        "match_all": {}
    }
}
'

put

curl -XPUT 'http://localhost:9200/megacorp/employee/2' -d '
{
    "first_name" :  "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}
'

get

curl -XGET 'http://localhost:9200/megacorp/employee/2' 
GET /megacorp/employee/_search
GET /megacorp/employee/_search?q=last_name:Smith

GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "last_name" : "Smith"
        }
    }
}

## (last_name="smith" && age>30),gt 表示_大于(_great than)
{
    "query" : {
        "bool": {
            "must": {
                "match" : {
                    "last_name" : "smith" 
                }
            },
            "filter": {
                "range" : {
                    "age" : { "gt" : 30 } 
                }
            }
        }
    }
}
## 全文搜索,ES返回的结果是按照相关性排序,并不是完全匹配
GET /megacorp/employee/_search
{
    "query" : {
        "match" : {
            "about" : "rock climbing"
        }
    }
}
集群健康
curl -XGET 'http://localhost:9200/_cluster/health'
{"cluster_name":"rating","status":"yellow","timed_out":false,"number_of_nodes":1,"number_of_data_nodes":1,"active_primary_shards":40,"active_shards":40,"relocating_shards":0,"initializing_shards":0,"unassigned_shards":5,"delayed_unassigned_shards":0,"number_of_pending_tasks":0,"number_of_in_flight_fetch":0,"task_max_waiting_in_queue_millis":0,"active_shards_percent_as_number":88.88888888888889}
## status
green : 所有的主分片和副本分片都正常运行。
yellow : 所有的主分片都正常运行,但不是所有的副本分片都正常运行。
red : 有主分片没能正常运行。

一个分片是一个luncence实例。在同一个节点上既保存原始数据又保存副本是没有意义的,一丢全丢。所以可以多少分片就多少节点。

"unassigned_shards":5表示:5个副本都没有挂在节点上。

设置主分片和副本
PUT /blogs
{
   "settings" : {
      "number_of_shards" : 3,//主分片
      "number_of_replicas" : 1//副本
   }
}

写索引都到主分片,读可以主副,所以主分片决定数据容量,副本数量可以决定吞吐量。


Creative Commons License
This work is licensed under a CC A-S 4.0 International License.