一、管理性命令
1、查看集群的节点
GET /_cat/nodes?v
2、查看集群健康状况
GET /_cat/health
3、查看集群中的库(index)
GET /_cat/indices?v
二、库(index)操作
1、查看某个具体库
GET /_cat/indices/xxx
2、手动建库
PUT /库名
{
"mappings": {
"表名":{
"properties":{
"属性名1":{
"type":"keyword"
},
"属性名2":{
"type":"text"
},
"属性名3":{
"type":"integer"
},
"属性名4":{
"type":"date"
}
}
}
}
}
2、自动建库
# 如果使用put则必须添加id号,使用post不添加id号会自动创建一个id号
POST /库名/表名/id号
{
"属性名": 属性值,
"属性名": 属性值,
"属性值名": [
属性值,
属性值
]
}
3、删库
DELETE /库名
4、判断库是否存在
#404不存在 200存在
HEAD /库名
三、表(type)操作
1、查看库下表的信息
# (6.0版本,默认只有1个,7.0版本没有type)
# 查看某个库中所有type信息,包含mapping和setting
GET /库名
#查看某个index的某个type
GET /test2/_mapping/table2
#只查看mapping信息
GET /库名/_mapping
#只查看setting信息
GET /库名/_setting
2、建表操作同建库
四、数据基本操作
1、增
#自动生成id,无规则的
POST /db1/persons
{
"name":"jack",
"age": 30,
"gender":"male"
}
#手动指定ID
POST /db1/persons/1
{
"name":"jack1",
"age": 30,
"gender":"male"
}
2、改
#如果id已经存在,就更新,不存在就新增
#默认全量更新
POST /db1/persons/1
{
"name":"jack1",
"age": 40
}
#增量更新
POST /db1/persons/1/_update
{
"age": 41
}
#只有post可以增量更新
3、查询
# 全表查询
GET /库名/表名/_search
4、判断id是否存在
HEAD /库名/表名/id
5、删除某个id
DELETE /db1/persons/2
五、分词、子属性
注意:格式是keyword不能切词
# 默认英文按照空格切分
GET _analyze
{
"text":"I am a programmer"
}
#默认切词器切中文,按字切
GET _analyze
{
"text":"我是程序员"
}
#指定分词器,使用IK分词器的ik_smart切词法,
#特点:切的词的总字数=文本总字数
GET _analyze
{
"analyzer": "ik_smart",
"text":"我是程序员"
}
#使用IK分词器的ik_max_word切词法,
特点:切的词的总字数>=文本总字数
GET _analyze
{
"analyzer": "ik_max_word",
"text":"我是程序员"
}
子属性
text类型的字段,如果将来需要聚合,一定需要为其设置一个子属性,子属性的类型必须是keyword类型!
例如:
name是text属性
name.keyword是keyword类型
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
六、批量导入
#导入数据:
#_bulk代表批量写
#格式 : {"action": {metadata}}\n {data}
# action: insert:插入,update:更新,delete:删除, index(upsert): 存在就更新,不存在就插入
#metadata 指定当前向哪个index,哪个type,哪个id进行写,
# 格式:"_index":库名,"_type":表名,"_id":id号
#所有的数据插入同一个库,表中
POST /test/emps/_bulk
{"index":{"_id":"1"}}
{"empid":1001,"age":20,"balance":2000,"name":"李三","gender":"男","hobby":"吃饭睡觉"}
{"index":{"_id":"2"}}
或者:
#可以指定每个数据插入的不同的库,表中
POST /_bulk
{"index":{"_index":"test2","_type":"t2","_id":"1"}}
{"name":"lisi","age":30}
{"index":{"_index":"test","_type":"t1","_id":"1"}}
{"name":"wangwu","age":22}
七、查询
两种方式:
1、url的方式
GET /test/emps/_search?q=*
2、DSL语法
GET /test/emps/_search
{
"query": {
"match_all": {}
}
}
常用参数
关键字 | 含义 | 在SQL中 |
---|---|---|
query | 查询 | select |
bool | 多个组合条件 | selext xxx from xxx where age=20 and gender=male |
filter | 一个过滤条件 | where |
term | 精确匹配 | = |
match | 全文检索,会分词 (分词器ik) | |
must | 在过滤条件中使用,代表必须包含 | |
fuzzy | 模糊音匹配 | dick 联想到 nick pick |
from | 从哪一条开始取 | |
size | 取多少条 | limit |
_source | 只选择某些字段 | select 字段 |
match_phrase | 短语匹配,将输入的查询内容整个作为整体进行查询,不切词 | |
multi_match | 一次匹配多个字段 | |
must_not | 不包含 | ! |
should | 最好也符合… 不符合也行,符合更好。一旦满足should会加分 | |
range | 匹配某个范围 | between and |
filter | 过滤,留下指定的 | where |
八、聚合
语法:
GET /库名/表名/_search
{
【"query": {
...
}, 】
"aggs": {
"聚合后的名称": {
"terms": {
"field": "聚合的字段:必须要是keyword类型",
"size": 10
}
}
#子聚合
【,"aggs": {
"聚合后的名称": {
"terms": {
"field": "聚合的字段:必须要是keyword类型",
"size": 10
}
}
}】
}
【#跟聚合
,"aggs": {
"聚合后的名称": {
"terms": {
"field": "聚合的字段:必须要是keyword类型",
"size": 10
}
}
}】
}
九、索引
建库时添加索引
PUT 库名
{
"aliases": {
"别名": {},
"别名": {}
},
"mappings": {
"表名":{
"properties": {
"属性名":{
"type": "long"
},
"属性名":{
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
}
2、建库后添加索引
POST /_aliases
{
"actions": [
{
"add": {
"index": "库名",
"alias": "别名"
}
}
]
}
3、查询添加的别名
#查询别名: 查看ES中已经起了哪些别名
GET /_cat/aliases?v
4、给查询结果起别名(只能使用精确匹配term),建立一个子集视图
POST /_aliases
{
"actions": [
{
"add": {
"index": "库名",
"alias": "别名",
"filter": {
"terms": {
"属性名": [
"属性值1",
"属性值2"
]
}
}
}
}
]
}
十、模板
PUT _template/库名
{
"index_patterns": ["xxx*"], #创建xxx开头的库会自动匹配该模板
"aliases" : {
"{index}-xxx": {}, #别名1,将index作为变量,假设index名为class,则别名为calss-xxx
"xxx-query":{} #别名2
},
"mappings": {
"表名": {
"properties": {
"属性名": {
"type": "keyword"
},
"属性值": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
}