elasticsearch2.1.0 curl 相关操作命令(一)

来源:互联网 发布:国外足球预测网站 知乎 编辑:程序博客网 时间:2024/05/16 08:25

这是我自己在学习和使用es过程中记录下来的curl相关命令,这里做一个记录,以备后续查找。

(1)打开监控 http://10.4.30.81:9200/_plugin/head/http://10.4.30.151:9200/_plugin/head/(2)查看集群基本状态curl 'http://10.4.30.81:9200/?pretty'curl 'http://10.4.30.151:9200/?pretty'java客户端api文档https://www.elastic.co/guide/en/elasticsearch/client/java-api/2.0/node-client.htmlhttps://www.elastic.co/guide/en/elasticsearch/client/java-api/2.1/transport-client.html(3-1)索引一个文档(插入一个文档到索引中)curl -XPUT 'http://10.4.30.151:9200/megacorp/employee/11' -d '{    "first_name" : "John4",    "last_name" :  "Smith4",    "age" :        35,    "about" :      "I love to go rock climbing3",    "interests": [ "sports4" ],"yujie_name":"wangyujie"}'在 Elasticsearch 中每个文档都有一个版本号。当每次对文档进行修改时(包括删除), _version 的值会递增(3-2) 使用post 自动创建文档idcurl -XPOST 'http://10.4.30.151:9200/megacorp/employee?pretty' -H 'Content-Type: application/json' -d'{    "first_name" : "John7",    "last_name" :  "Smith7",    "age" :        37,    "about" :      "I love to go rock climbing7",    "interests": [ "sports7" ]}'响应如下:{  "_index" : "megacorp",  "_type" : "employee",  "_id" : "AWBNtXOoxFnukAxvHpYx",  "_version" : 1,  "_shards" : {    "total" : 2,    "successful" : 2,    "failed" : 0  },  "created" : true}(3-3)如果文档不存在 才创建;如果文档已存在则报错curl -XPUT 'http://10.4.30.151:9200/megacorp/employee/1?op_type=create' -d '{    "first_name" : "John4",    "last_name" :  "Smith4",    "age" :        35,    "about" :      "I love to go rock climbing3",    "interests": [ "sports4" ]}'或者curl -XPUT 'http://10.4.30.151:9200/megacorp/employee/1/_create' -d '{    "first_name" : "John4",    "last_name" :  "Smith4",    "age" :        35,    "about" :      "I love to go rock climbing3",    "interests": [ "sports4" ]}'自动生成的 ID 是 URL-safe、 基于 Base64 编码且长度为20个字符的 GUID 字符串。 这些 GUID 字符串由可修改的 FlakeID 模式生成,这种模式允许多个节点并行生成唯一 ID ,且互相之间的冲突概率几乎为零 (4-1)根据索引 类型 id 取回一个文档curl -XGET 'http://10.4.30.151:9200/megacorp/employee/1'响应:{"_index":"megacorp","_type":"employee","_id":"1","_version":1,"found":true,"_source":{    "first_name" : "John",    "last_name" :  "Smith",    "age" :        25,    "about" :      "I love to go rock climbing",    "interests": [ "sports", "music" ]}}(4-2)根据索引 类型 id 取回一个文档 只获取_source数据curl -XGET 'http://10.4.30.151:9200/megacorp/employee/1/_source?pretty'响应如下:{    "first_name" : "John",    "last_name" :  "Smith",    "age" :        25,    "about" :      "I love to go rock climbing",    "interests": [ "sports", "music" ]}(4-3)检查一个文档是否存在 使用HEADcurl -i -XHEAD 'http://10.4.30.151:9200/megacorp/employee/1'如果文档存在, Elasticsearch 将返回一个 200 ok 的状态码,不存在返回404(4-4) 删除一个文档curl -XDELETE 'http://10.4.30.151:9200/megacorp/employee/1?pretty'(4-5)es使用基于版本号的乐观并发锁策略 实现数据的并发访问curl -XPUT 'http://10.4.30.151:9200/megacorp/employee/1?version=1&pretty' -H 'Content-Type: application/json' -d'{  "title": "My first blog entry",  "text":  "Starting to get the hang of this..."}'此时如果es中这个文档的version=2,这时 请求将会返回409 Conflict HTTP 响应码。请求的版本如果小于es的版本,意味着数据已经被修改,持有版本1的请求数据已经是旧数据。(4-6)更新文档curl -XPOST 'http://10.4.30.151:9200/megacorp/employee/4/_update?pretty' -H 'Content-Type: application/json' -d'{   "doc" : {      "tags" : [ "testing" ],      "views": 0   }}'部分更新文档字段已有则替换,字段没有则新增(4-7)取回多个文档curl -XGET 'http://10.4.30.151:9200/_mget?pretty' -H 'Content-Type: application/json' -d'{   "docs" : [      {         "_index" : "megacorp",         "_type" :  "employee",         "_id" :    2      },      {         "_index" : "website",         "_type" :  "pageviews",         "_id" :    1,         "_source": "views"      }   ]}'如果索引和文档类型都一样,可以直接提供一个ids列表curl -XGET 'http://10.4.30.151:9200/megacorp/employee/_mget?pretty' -H 'Content-Type: application/json' -d'{"ids" : [ "1", "2" ,"3","4","5"]}'(5-1)获取所有文档 默认一次返回10个文档curl -XGET 'http://10.4.30.151:9200/megacorp/employee/_search'(5-2)执行下列空搜索 返回命中的前10个结果curl -XGET 'http://10.4.30.151:9200/_search?pretty'curl -XGET 'http://10.4.30.151:9200/_search?pretty' -H 'Content-Type: application/json' -d'{    "query": {        "match_all": {}    }}'(5-3)分页搜索 size:每次返回的数量,from:应该跳过的初始结果的数量curl -XGET 'http://10.4.30.151:9200/_search?size=5&pretty'curl -XGET 'http://10.4.30.151:9200/_search?size=5&from=5&pretty'curl -XGET 'http://10.4.30.151:9200/_search?size=5&from=10&pretty'curl -XGET 'http://10.4.30.151:9200/_search?pretty' -H 'Content-Type: application/json' -d'{  "from": 5,  "size": 4}'(6)(轻量查询)在索引megacorp中的类型为employee中查询last_name为Smith的用户curl -XGET 'http://10.4.30.151:9200/megacorp/employee/_search?q=last_name:wang6&pretty'curl -XGET 'http://10.4.30.151:9200/_search?pretty' -H 'Content-Type: application/json' -d '{    "query": {        "match": {            "last_name": "wang6"        }    }}'(7)DSL(领域特定语言) 使用查询表达式 和(6)效果一样评分查询curl -XGET 'http://10.4.30.151:9200/_search?pretty' -H 'Content-Type: application/json' -d '{    "query": {        "match": {            "last_name": "wang6"        }    }}'filter 过滤查询 不评分curl -XGET 'http://10.4.30.151:9200/_search?pretty' -H 'Content-Type: application/json' -d '{       "query" : {        "bool" : {            "filter" : {                "term" : {                    "last_name": "wang6"                }            }        }    }}'(8)使用过滤器curl -XGET 'http://10.4.30.151:9200/megacorp/employee/_search' -d '{ "query" : {        "bool": {            "must": {                "match" : {                    "last_name" : "smith"                 }            },            "filter": {                "range" : {                    "age" : { "gt" : 30 } //年龄大于30的 grant than                }            }        }    }}'(9)全文搜索curl -XGET 'http://10.4.30.151:9200/megacorp/employee/_search' -d '{    "query" : {        "match" : {            "about" : "rock climbing"        }    }}'(10)精确匹配短语curl -XGET 'http://10.4.30.151:9200/megacorp/employee/_search' -d '{    "query" : {        "match_phrase" : {            "about" : "rock climbing"        }    }}'(11)高亮搜索curl -XGET 'http://10.4.30.151:9200/megacorp/employee/_search' -d '{    "query" : {        "match_phrase" : {            "about" : "rock climbing"        }    },    "highlight": {        "fields" : {            "about" : {}        }    }}'(12)检查集群的健康状态curl -XGET 'http://10.4.30.151:9200/_cluster/health?pretty'(13)创建索引curl -XPUT 'http://10.4.30.151:9200/blogs' -d '{ "settings" : {      "number_of_shards" : 3,      "number_of_replicas" : 1   }}'索引名,这个名字必须小写,不能以下划线开头,不能包含逗号_type 命名可以是大写或者小写,但是不能以下划线或者句号开头,不应该包含逗号, 并且长度限制为256个字符我们可以提供自定义的 _id 值,或者让 index API 自动生成。(14) 水平扩容 动态调整分片副本的数量curl -XPUT 'http://10.4.30.151:9200/blogs/_settings' -d '{"number_of_replicas" : 2}'当然,如果只是在相同节点数目的集群上增加更多的副本分片并不能提高性能,因为每个分片从节点上获得的资源会变少。 你需要增加更多的硬件资源来提升吞吐量


原创粉丝点击