针对Elasticsearch 的简单使用,对于其增删改查以及相应的批处理进行简单的说明,对于初学者而言,希望有所帮助。
1、创建索引
curl -XPUT ‘172.17.1.48:9200/customer?pretty’
#索引库中插入数据
curl -XPUT ‘172.17.1.48:9200/customer/external/1?pretty’ -d ’
{
“name”: “John Doe”
}’
2、删除
#删除索引库
curl -XDELETE ‘172.17.1.48:9200/customer?pretty’
#一次删除符合条件的多个文档
curl -XDELETE ‘172.17.1.48:9200/customer/external/_query?pretty’ -d ’
{
“query”: { “match”: { “name”: “John” } }
}’
3、修改
#更新一个文档
curl -XPUT '172.17.1.48:9200/customer/external/1?pretty' -d '
{
"name": "ysl Doe"
}'
#es并不支持原地更新,它是删除一个旧的文档,重新生成索引新的文档
curl -XPOST '172.17.1.48:9200/customer/external/1/_update?pretty' -d '
{
"doc": { "name": "Jane Doe", "age": 20 }
}'
#年龄字段上加5
curl -XPOST '172.17.1.48:9200/customer/external/1/_update?pretty' -d '
{
"script" : "ctx._source.age += 5"
}'
4、查看
查询某条记录
curl -XGET '172.17.1.48:9200/customer/external/1?pretty'
查看集群状态
curl '172.17.1.48:9200/_cat/health?v'
获得集群节点列表
curl '172.17.1.48:9200/_cat/nodes?v'
查看所有的索引列表
curl '172.17.1.48:9200/_cat/indices?
#查询具体size的大小的数据,size:默认诶为10
curl -XPOST '172.17.1.48:9200/bank/_search/?pretty' -d '{"query":{"match_all":{}},"size":1}'
返回第10到20的数据记录
curl -XPOST '172.17.1.48:9200/bank/_search?pretty' -d '
{
“query”:{“match_all”:{},
“from”:10,
“size”:10
}’
做一次match_all并且以账户余额排序。最后返回前10个文档
curl -XPOST '172.17.1.48:9200/bank/_search?pretty' -d '
{
“query”:{“match_all”:{}},
“sort”:{“balance”:{“order”:”desc”}}
}’
做一次的查询,只返回部分的原始字段
curl -XPOST '172.17.1.48:9200/bank/_search?pretty' -d '
{
“query”:{“match_all”:{}},
“_source”:[“account_number”,”balance”]
}’
查询账户为20的数据记录
curl -XPOST ‘172.17.1.48:9200/bank/_search?pretty’ -d ’
{
“query”:{“account_number”:20}
}’
查询地址中包含mill的数据记录
curl -XPOST ‘172.17.1.48:9200/bank/_search?pretty’ -d ’
{
“query”:{“match”:{“address”:”mill”}}
}’
match的变体match_phrase,他会匹配短语”mill lane”
curl -XPOST ‘172.17.1.48:9200/bank/_search?pretty’ -d ’
{
“query”:{“match_phrase”:{“address”:”mill lane”}}
}’
boolean查询,布尔查询允许我们将较小额查询组合成为较大的查询
curl -XPOST ‘172.17.1.48:9200/bank/_search?pretty’ -d ’
{
“query”: {
“bool”: {
“must”: [
{ “match”: { “address”: “mill” } },
{ “match”: { “address”: “lane” } }
]
}
}
}’
个例子返回40岁以上并且不生活在ID(daho)的人的账户
curl -XPOST ‘172.17.1.48:9200/bank/_search?pretty’ -d ’
{
“query”: {
“bool”: {
“must”: [
{ “match”: { “age”: “40” } }
],
“must_not”: [
{ “match”: { “state”: “ID” } }
]
}
}
}’
执行过滤器
- 过滤器不会计算相关度的得分,所以它们在计算上更快一些
过滤器可以被缓存到内存中,这使得在重复的搜索查询上,其要比相应的查询快出许多。
curl -XPOST ‘172.17.1.48:9200/bank/_search?pretty’ -d ’
{
“query”: {
“filtered”: {
“query”: { “match_all”: {} },
“filter”: {
“range”: {
“balance”: {
“gte”: 20000,
“lte”: 30000
}
}
}
}
}
}’
执行聚合
按照state分组,按照州名的计数倒序排序:
curl -XPOST ‘172.17.1.48:9200/bank/_search?pretty’ -d ’
{
“size”: 0,
“aggs”: {
“group_by_state”: {
“terms”: {
“field”: “state”
}
}
}
}’
每个州的账户的平均余额(还是按照账户数量倒序排序的前10个州):
curl -XPOST ‘172.17.1.48:9200/bank/_search?pretty’ -d ’
{
“size”: 0,
“aggs”: {
“group_by_state”: {
“terms”: {
“field”: “state”
},
“aggs”: {
“average_balance”: {
“avg”: {
“field”: “balance”
}
}
}
}
}
}’
使用年龄段(20-29,30-39,40-49)分组,然后在用性别分组,然后为每一个年龄段的每一个性别计算平均账户余额:
curl -XPOST ‘172.17.1.48:9200/bank/_search?pretty’ -d ’
{
“size”: 0,
“aggs”: {
“group_by_age”: {
“range”: {
“field”: “age”,
“ranges”: [
{
“from”: 20,
“to”: 30
},
{
“from”: 30,
“to”: 40
},
{
“from”: 40,
“to”: 50
}
]
},
“aggs”: {
“group_by_gender”: {
“terms”: {
“field”: “gender”
},
“aggs”: {
“average_balance”: {
“avg”: {
“field”: “balance”
}
}
}
}
}
}
}
}’
5、批处理操作curl -XPOST '172.17.1.48:9200/customer/external/_bulk?pretty' -d ' {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}}
6、聚合操作
7载入数据
curl -XPOST ‘172.17.1.48:9200/bank/account/_bulk?pretty’ –data-binary @accounts.json