@1234567890
2018-03-05T09:56:03.000000Z
字数 4012
阅读 1072
ES
列出所有索引
curl 'localhost:9200/_cat/indices?v'
创建一个索引
curl -XPUT 'localhost:9200/customer?pretty'
上传数据C
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '{"name":"wht"}'
curl -XPOST 'localhost:9200/customer/external?pretty' -d '{"name":"Jane Doe"}'
注意,在上面的情形中,由于我们没有指定一个ID,我们使用的是POST而不是PUT。
数据查询R
curl -XGET 'localhost:9200/customer/external/1?pretty'
curl -XGET 'localhost:9200/customer/external/_search?pretty' -d '{"query":{"match":{"name":"wht"}}}'
curl -XGET 'localhost:9200/customer/external/_search?pretty' -d '{"query":{"match_all":{}}}'
curl 'localhost:9200/customer/_search?q=*&pretty'
更新文档U
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '{"doc": { "name":"Jane Doe", "age": 20 }}'
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '{"script" :"ctx._source.age += 5"}'
在上面的例子中,ctx._source指向当前要被更新的文档。
删除文档D
curl -XDELETE 'localhost:9200/customer/external/2?pretty'
curl -XDELETE 'localhost:9200/customer/external/_query?pretty' -d '{"query": { "match":{ "name": "John" } }}'
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 4,
"max_score" : 1.0,
"hits" : [
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "wht",
"age" : 20
}
},
{
"_index" : "customer",
"_type" : "external",
"_id" : "AVi5SsOlPAt5b1643npc",
"_score" : 1.0,
"_source" : {
"name" : "hhhh3"
}
}
]
}
}
对于这个响应,我们看到了以下的部分:
- took —— Elasticsearch执行这个搜索的耗时,以毫秒为单位
- timed_out —— 指明这个搜索是否超时
- _shards —— 指出多少个分片被搜索了,同时也指出了成功/失败的被搜索的shards的数量
- hits —— 搜索结果
- hits.total —— 能够匹配我们查询标准的文档的总数目
- hits.hits —— 真正的搜索结果数据(默认只显示前10个文档)
- _score和max_score —— 现在先忽略这些字段
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"size": 1
}'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}'
返回地址中包含“mill”或者包含“lane”的账户
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "address": "mill lane" } }
}'
match的变体(match_phrase),它会去匹配短语“mill lane”
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_phrase": { "address": "mill lane" } }
}'
组合查询返回包含“mill”和“lane”的所有的账户
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
返回的是地址中包含“mill”或者“lane”的所有的账户
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
它返回地址中既不包含“mill”,同时也不包含“lane”的所有的账户信息
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
40岁以上并且不生活在ID(daho)的人的账户
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}'
_analyze关键字查看分析的过程
curl 'localhost:9200/test/_analyze?analyzer=standard&pretty' -d "xiao ming"
创建mapping
curl -XPUT 'localhost:9200/test?pretty' -d '
{
"mappings": {
"employee": {
"properties": {
"username": {
"type": "text"
},
"password": {
"type": "keyword"
},
"describtion": {
"type": "text"
},
"birthday": {
"type": "date",
"format": "yyyy-MM-dd"
},
"email": {
"type": "text"
},
"ip_addr": {
"type": "ip"
}
}
}
}
}'
数据手动导入
curl -XPOST 'localhost:9200/test/employee/_bulk?pretty' --data-binary @test.db
test.db
{"index":{"_index":"test","_type":"employee","_id":1}}
{"username":"xiao ming","password":"password-111","describtion":"i'm xiaoming,call mingming","birthday":"2010-01-01","email":"123@qq.com","ip_addr":"192.168.0.1"}
{"index":{"_index":"test","_type":"employee","_id":2}}
{"username":"xiao hong","password":"password-222","describtion":"i'm xiao hong,call mingming","birthday":"2010-02-02","email":"222@qq.com","ip_addr":"192.168.0.2"}
{"index":{"_index":"test","_type":"employee","_id":3}}
{"username":"小红","password":"password-333","describtion":"i'm 小红,call 红红","birthday":"2010-03-03","email":"333@163.com","ip_addr":"192.168.0.3"}