elasticsearch教程
注意:
1、elasticsearch的rest默认端口为9200,客户端的默认端口为9300,也就是,java编程连接的端口为9300
命令模式:
curl -<REST Verb> <Node>:<Port>/<Index>/<Type><ID>
返回字段说明:
- took —— Elasticsearch执行这个搜索的耗时,以毫秒为单位
- timed_out —— 指明这个搜索是否超时
- _shards —— 指出多少个分片被搜索了,同时也指出了成功/失败的被搜索的shards的数量
- hits —— 搜索结果
- hits.total —— 能够匹配我们查询标准的文档的总数目
- hits.hits —— 真正的搜索结果数据(默认只显示前10个文档)
- _score和max_score —— 现在先忽略这些字段
1、检查集群健康
curl 'localhost:9200/_cat/health?v'
2、获得节集群中的节点列表
curl 'localhost:9200/_cat/nodes?v'
3、列出所有的索引
curl 'localhost:9200/_cat/indices?v'
4、创建一个索引customer
curl -XPUT 'localhost:9200/customer?pretty'
说明:索引index相当于一个库,pretty附加到调用的尾部,使其以美观的形式打印出JSON响应(如果有的话),不添加pretty也会打印结果,只是不美观
5、在customer索引下创建一个文档external(添加一个记录)
curl -XPUT '192.168.60.128:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
说明:文档type相当于一个表
curl -XPOST 'localhost:9200/customer/external?pretty' -d '
{
"name": "Jane Doe"
}'
注意,在上面的情形中,由于我们没有指定一个ID,我们使用的是POST而不是PUT
6、数据查询
查询一个id=1的数据
curl -XGET '192.168.60.128:9200/customer/external/1?pretty'
查询external下所有的数据
curl -XGET '192.168.60.128:9200/customer/external/_search?pretty'
查询customer下所有的数据
curl -XGET '192.168.60.128:9200/customer/_search?pretty'
查询所有数据
curl -XGET '192.168.60.128:9200/_search?pretty'
7、删除一个索引
curl -XDELETE '192.168.60.128 :9200/customer?pretty'
8、修改文档(数据)
curl -XPUT '192.168.60.128:9200/customer/external/1?pretty' -d '
{
"name": "Tom"
}'
curl -XPOST '192.168.60.128 :9200/customer/external/1/_update?pretty' -d '
{
"doc": { "name": "Jane Doe", "age": 20 }
}'
curl -XPOST '192.168.60.128 :9200/customer/external/1/_update?pretty' -d '
{
"script" : "ctx._source.age += 5"
}'
9、删除文档(数据)
curl -XDELETE '192.168.60.128 :9200/customer/external/1?pretty'
删除符合某个查询条件的多个文档
curl -XDELETE 'localhost:9200/customer/external/_query?pretty' -d '
{
"query": { "match": { "name": "John" } }
}'
10、批处理
添加两个文档(ID 1 - John Doe and ID 2 - Jane Doe)
curl -XPOST '192.168.60.128 :9200/customer/external/_bulk?pretty' -d '
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
更新第一个文档(ID为1),然后删除第二个文档(ID为2):
curl -XPOST '192.168.60.128 :9200/customer/external/_bulk?pretty' -d '
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
11、导入json文件(导入数据)
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary @accounts.json
accounts.json为一个存储着json数据的文件
12、请求体条件搜索
curl '192.168.60.128 :9200/bank/_search?q=*&pretty'
使用请求体方法的等价搜索是:
curl -XPOST '192.168.60.128 :9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} }
}'
match(equal)条件查询,相当于select account_number,balance from bank.account where gender='M' order by balance desc limit 10,10
curl -XPOST '192.168.60.128 :9200/bank/account/_search?pretty' -d '
{
"_source": ["account_number", "balance"],
"query": { "match": {"gender":"M"} },
"sort": { "balance": { "order": "desc" } }
"from": 10,
"size": 10
}'
match_phrase(like)条件查询,相当于select * from bank.account where address like ‘%mill lane%’
注意:字符串匹配不分大小写
curl -XPOST '192.168.60.128 :9200/bank/account/_search?pretty' -d '
{
"query": { "match_phrase": { "address": "mill lane" } }
}'
must(and)条件查询,相当于select * from bank.account where address like "%mill%" and address like "%lane%"
返回包含“mill”和“lane”的所有的账户
curl -XPOST '192.168.60.128 :9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
should(or)条件查询,相当于select * from bank.account where address like "%mill%" or address like "%lane%"
返回地址中包含“mill”或者“lane”的所有的账户
curl -XPOST '192.168.60.128 :9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
must_not(not in)条件查询,相当于select * from bank.accout where id not in(select id from bank.account where address like "%mill%" or address like "lane")
返回地址中既不包含“mill”,同时也不包含“lane”的所有的账户信息
curl -XPOST '192.168.60.128 :9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
返回40岁以上并且不生活在ID(daho)的人的账户
curl -XPOST '192.168.60.128 :9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}'
过滤的查询,其返回值是越在20000到30000之间(闭区间)的账户
curl -XPOST '192.168.60.128 :9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": {
"match_all": {}
},
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
聚合,group by
' -d '
{
"properties": {
"state": {
"type": "text",
"fielddata": true
}
}
}'
统计每个state有多少条记录
将size设置成0,这样就可以只看到聚合结果了,而不会显示命中的结果
curl -XPOST '192.168.60.128 :9200/bank/_search?pretty=true ' -d '
{
"size":0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
}
}
}
}'
计算了每个州的账户的平均余额(还是按照账户数量倒序排序的前10个州)
curl -XPOST '192.168.60.128 :9200/bank/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}'
使用年龄段(20-29,30-39,40-49)分组,然后在用性别分组,然后为每一个年龄段的每一个性别计算平均账户余额
curl -XPUT "http://192.168.60.128:9200/bank/_mapping/account" -d'
{
"properties": {
"gender": {
"type": "text",
"fielddata": true
}
}
}'
curl -XPOST 'localhost: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"
}
}
}
}
}
}
}
}
curl -XPOST '192.168.60.128 :9200/bank/account/_bulk?pretty' --data-binary @accounts.json
curl '192.168.60.128 :9200/_cat/indices?v'
13、IK中文分词器使用
curl -XPUT http://192.168.60.128:9200/ik
curl -XPOST http://192.168.60.128:9200/ik/fulltext/_mapping -d'{ "fulltext": { "_all": { "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "term_vector": "no", "store": "false" }, "properties": { "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "include_in_all": "true", "boost": 8 } } }}'
curl -XPOST http://192.168.60.128:9200/ik/fulltext/1 -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}'
curl -XPOST http://192.168.60.128:9200/ik/fulltext/2 -d'
{"content":"公安部:各地校车将享最高路权"}'
curl -XPOST http://192.168.60.128:9200/ik/fulltext/3 -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}'
curl -XPOST http://192.168.60.128:9200/ik/fulltext/4 -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}'
curl -XPOST http://192.168.60.128:9200/ik/fulltext/_search -d'{ "query" : { "match" : { "content" : "中国" }}, "highlight" : { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "content" : {} } }}'