1 集群管理
启动集群
./bin/elasticsearch -d
查看集群健康状态
curl '192.168.43.30:9200/_cat/health?v
关闭集群
jps
kill -9 4670
2 索引
创建索引
curl -XPUT '192.168.43.30:9200/weimr?pretty' -H 'Content-Type: application/json' -d'
{"settings" : {"number_of_shards" : 3,"number_of_replicas" : 1},
"mappings" : {
"doc" : {
"properties" : {
"about" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},
"age" : {"type" : "long"},
"first_name" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},
"interests" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},
"last_name" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}}
}
}
}
}'
查看所有索引
curl 192.168.43.30:9200/_cat/indices?v
所有索引库(index)和索引表(type)
curl 192.168.43.30:9200/_search?pretty=true
查看索引映射信息
curl http://192.168.43.30:9200/weimr/doc/_mapping?pretty=true
插入记录
curl -XPUT 'http://192.168.43.30:9200/weimr/doc/1' -H 'Content-Type: application/json' -d '
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'
curl -XPUT http://192.168.43.30:9200/weimr/doc/2 -H 'Content-Type: application/json' -d '
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}'
curl -XPUT http://192.168.43.30:9200/weimr/doc/3 -H 'Content-Type: application/json' -d '
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}'
查看某个索引和记录
curl 192.168.43.30:9200/weimr/_search?pretty
查询某个索引类型记录数
curl 192.168.43.30:9200/weimr/doc/_count?pretty
查询所有记录
curl 192.168.43.30:9200/weimr/doc/_search?pretty=true
多条件分页精确查询
curl '192.168.43.30:9200/weimr/doc/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"filter":[
{"term": {"first_name.keyword": "Jane"}},
{"range": {"age":{ "gte": 32 }}}
]
}
},
"sort": { "_id": "asc"},
"from": 0,
"size": 2
}'
备注:因为语法解析的缘故,first_name.keyword表示不解析情况下匹配
多条件分页匹配查询
curl '192.168.43.30:9200/weimr/doc/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must":[
{"match":{"about": "like"}},
{"range": {"age":{ "gte": 32 }}}
]
}
},
"sort": { "_id": "asc"},
"from": 0,
"size": 2
}'
全文检索
curl -XGET 192.168.43.30:9200/weimr/doc/_search?pretty -H 'Content-Type: application/json' -d '
{
"query": {
"multi_match": {
"query":"rock"
}
},
"sort": { "_id": "asc"},
"from": 0,
"size": 2
}'
分组统计
curl '192.168.43.30:9200/weimr/doc/_search?pretty' -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"app_code_count": {
"terms": {
"field": "last_name.keyword",
"order" : {
"_count" : "asc"
}
},
"aggs": {
"num_id_count": {
"terms": {
"field": "last_name.keyword"
}
}
}
}
}
}'
删除文档(记录)
curl -XDELETE 192.168.43.30:9200/weimr/doc/1
清空数据
curl -XPOST 192.168.43.30:9200/weimr/doc/_delete_by_query?pretty -H 'Content-Type: application/json' -d '
{
"query": {
"match_all": {}
}
}'
删除索引库
curl -XDELETE 'http://192.168.43.30:9200/weimr?pretty=true'
批量导入数据
data.json
{"index": { "_id": 1 }}
{"first_name":"John","last_name":"Smith","age":25,"about":"I love to go rock climbing","interests": [ "sports", "music" ]}
{"index": { "_id": 2 }}
{"first_name":"Jane","last_name":"Smith","age":32,"about":"I like to collect rock albums","interests": [ "music" ]}
{"index": { "_id": 3 }}
{"first_name":"Douglas","last_name":"Fir","age":35,"about":"I like to build cabinets","interests": [ "forestry" ]}
curl -XPOST '192.168.43.30:9200/weimr/doc/_bulk' -H 'Content-Type: application/json' --data-binary @data.json
更新文档(记录)
curl -XPUT 192.168.43.30:9200/weimr/doc/1 -H 'Content-Type: application/json' -d '
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 26,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}'