基本概念:
1. cluster 和 node
本质是分布式数据库。单个实例就是一个node,多个node构成一个cluster
2. Index
es会对索引所有的字段,处理后写入一个反向索引,类似于数据库。
查看当前所有节点的所有index
curl -X GET 'http://localhost:9200/_cat/indices?v'
3.Document
index里的单条记录称为document
例子如下:
{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}
4. type
index 下的分组称为type,多个type之间应该有相似的schema
查看某个index下的所有type
curl 'localhost:9200/_mapping?pretty=true'
基本操作
0. 修改用户密码
curl -X POST ‘http://localhost:9200/_xpack/security/user/jacknich/_password’ -d '
{
"password" : "123456"
}'
1. 添加index
curl -X PUT 'localhost:9200/weather'
2. 删除index
curl -X DELETE 'localhost:9200/weather'
3.添加数据
put
curl -X PUT 'localhost:9200/accounts/person/1' -d '
{
"user": "张三",
"title": "工程师",
"desc": "数据库管理"
}'
上面的1表示此条数据id,如果不指定id,则需要使用post
post
curl -X POST 'localhost:9200/accounts/person' -d '
{
"user": "李四",
"title": "工程师",
"desc": "系统管理"
}'
4. 查看数据
curl 'localhost:9200/accounts/person/1?pretty=true'
结果样例:
{
"_index" : "accounts",
"_type" : "person",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"user" : "张三",
"title" : "工程师",
"desc" : "数据库管理"
}
}
5. 修改记录
curl -X PUT 'localhost:9200/accounts/person/1' -d '
{
"user" : "张三",
"title" : "工程师",
"desc" : "数据库管理,软件开发"
}'
{
"_index":"accounts",
"_type":"person",
"_id":"1",
"_version":2,
"result":"updated",
"_shards":{"total":2,"successful":1,"failed":0},
"created":false
}
6. 删除记录
curl -X DELETE 'localhost:9200/accounts/person/1'
7. 查询所有记录
curl 'localhost:9200/accounts/person/_search'
8. 全文搜索
普通搜索,desc字段里包含“软件”关键字
curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "软件" }}
}'curl 'localhost:9200/accounts/person/_search'
指定记录条数
curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "管理" }},
"size": 1
}'
9. 逻辑运算
desc包含“软件” or “系统”
curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "软件 系统" }}
}'
desc包含“软件” and “系统”
curl 'localhost:9200/accounts/person/_search' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "desc": "软件" } },
{ "match": { "desc": "系统" } }
]
}
}
}'