ES常见操作
-
手动mapping
PUT /product
{
"settings": {
"analysis": {
"analyzer": {
"ik": {
"tokenizer": "ik_max_word"
}
}
}
},
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"title": {
"type": "text",
"analyzer": "ik_max_word",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"content": {
"type": "text",
"analyzer": "ik_max_word"
},
"module": {
"type": "text"
},
"role": {
"type": "text"
},
"top_flag": {
"type": "integer"
},
"hits": {
"type": "long"
},
"urls": {
"type": "text"
},
"order_num": {
"type": "integer"
},
"create_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"update_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
-
批量插入 注意json不能换行
POST /_bulk
{ "index": { "_index": "product", "_id": "1" }}
{ "name" : "xiaomi phone", "desc" : "shouji zhong de zhandouji", "date": "2021-06-01", "price" : 3999, "tags": [ "xingjiabi", "fashao", "buka" ]}
{ "create": { "_index": "product", "_id": "2" }}
{ "name" : "xiaomi nfc phone", "desc" : "zhichi quangongneng nfc,shouji zhong de jianjiji", "date": "2021-06-02", "price" : 4999, "tags": [ "xingjiabi", "fashao", "gongjiaoka" ]}
{ "create": { "_index": "product", "_id": "3" }}
{ "name" : "nfc phone", "desc" : "shouji zhong de hongzhaji", "date": "2021-06-03", "price" : 2999, "tags": [ "xingjiabi", "fashao", "menjinka" ]}
-
删除索引
DELETE product
-
单条插入
PUT /product/_doc/2
{
"name" : "xiaomi phone",
"desc" : "shouji zhong de zhandouji",
"date": "2021-06-01",
"price" : 3999,
"tags": [ "xingjiabi", "fashao", "buka" ]
}
GET product/_mapping
GET product/_setting
GET product/_search
GET product/_doc/1
GET /product/_search?q=name:xiaomi
## 全文检索
GET /product/_search
{
"query": {
"match": {
"name": "xiao nfc"
}
}
}
## 多字段全文检索
GET /product/_search
{
"query":{
"multi_match":{
"query":"行动客户",
"fields":["title","content"]
}
}
}
## 查看ik_smart分词结果
POST _analyze
{
"analyzer": "ik_smart",
"text": "特工红海特别探索"
}
## 查看ik_max_word分词结果
POST _analyze
{
"analyzer": "ik_max_word",
"text": "活动什么时候关闭"
}
-
删除索引 & 删除数据
## 删除索引
DELETE product
## 按条件删除数据
POST product/_delete_by_query
{
"query": {
"match_all": {
}
}
}
## 批量更新
POST /product/_update_by_query
{
"script": {
"inline": "ctx._source.order_num = ctx._source.id"
},
"query": {
"match": {
"top_flag": 1
}
}
}
## 单条更新
PUT /product/_doc/1
{ "count": 5 }
## 仅返回所需字段
GET /product/_search
{
"_source": {
"includes": ["id","top_flag","order_num"]
},
"size": 20,
"from": 0,
"query": {
"bool": {
"must": [{
"match": {
"top_flag": "1"
}
}]
}
}
}
-
ES 按前缀批量删除索引(设置超时时间,默认为30s)
curl -XDELETE 'http://localhost:9200/plume_log_run_202206160*?master_timeout=5m'