Elasticsearch命令执行增删改查

POST /_analyze
{
"analyzer": "ik_smart",
"text": "郭艾伦简直泰裤辣哦!学习java当程序员真好。"
}
-- 创建索引库:PUT/索引库名 (index是否需要倒排索引,排序或查找使用,默认开启)
PUT /heima
{
"mappings": {
"properties": {
"info": {
"type": "text",
"analyzer": "ik_smart"
},
"email": {
"type": "keyword",
"index": false
},
"name": {
"type": "object",
"properties": {
"firstName": {
"type": "keyword"
}
}
}
}
}
}
-- 查询索引库:GET/索引库名
GET /heima
-- 删除索引库:DELETE/索引库名
DELETE /heima
-- 添加字段:PUT/索引库名/_mapping
PUT /heima/_mapping
{
"properties": {
"age":{
"type": "byte"
}
}
}
-- 新增文档: POST /索引库名/_doc/文档id {json文档}
POST /heima/_doc/1
{
"info": "黑马程序员Java讲师",
"email": "zy@itcast.cn",
"name": {
"firstName": "云",
"lastName": "赵"
}
}
-- 查询文档: GET /索引库名/_doc/文档id
GET /heima/_doc/3
-- 删除文档:DELETE /索引库名/_doc/文档id
DELETE /heima/_doc/1
-- 全量修改文档:(有的话,先删,再写;没有就新增;类似map.put)
-- PUT /索引库名/_doc/文档id{json文档}
PUT /heima/_doc/2
{
"info": "黑马程序员Java讲师",
"email": "zs@itcast.cn",
"name": {
"firstName": "四",
"lastName": "赵"
}
}
-- 局部修改文档(增量修改):POST /索引库名/_update/文档id {"doc":{字段}}
POST /heima/_update/1
{
"doc": {
"email": "zhaoyun@itcast.cn"
}
}
---- 文档批处理
-- 批量新增
POST /_bulk
{"index":{"_index":"heima","_id":"3"}}
{"info":"黑马程序员C++讲师","email":"ww@itcast.cn","name":{"firstName":"五","lastName":"王"}}
{"index":{"_index":"heima","_id":"4"}}
-- 批量删除
POST /_bulk
{"delete":{"_index":"heima","_id":"3"}}
-- 批量更新
POST /_bulk
{"update":{"_index":"heima","_id":"3"}}
{"doc":{"email":"wangwu@itcast.cn"}}
{"update":{"_index":"heima","_id":"4"}}
GET /items
GET /items/_doc/896020
GET /items/_count
PUT /items
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"name": {
"type": "text",
"analyzer": "ik_smart"
},
"price": {
"type": "integer"
},
"image": {
"type": "keyword",
"index": false
},
"category": {
"type": "keyword"
},
"brand": {
"type": "keyword"
},
"sold": {
"type": "integer"
},
"commentCount": {
"type": "integer",
"index": false
},
"isAD": {
"type": "boolean"
},
"updateTime": {
"type": "date"
}
}
}
}
-- 查询所有文档
GET /items/_search
{
"query": {
"match_all": {}
}
}
-- 全文检索查询
GET /items/_search
{
"query": {
"match": {
"name": "华为荣耀"
}
}
}
GET /items/_search
{
"query": {
"multi_match": {
"query": "华为",
"fields": ["name","brand"]
}
}
}
-- 精确查询(不分词)
GET /items/_search
{
"query": {
"term": {
"brand.keyword": {
"value": "韦沃"
}
}
}
}
-- 范围查询
GET /items/_search
{
"query": {
"range": {
"price": {
"gte": 500000,
"lte": 1000000
}
}
}
}
GET /items/_search
{
"query": {
"ids": {
"values": ["1861099","1861100"]
}
}
}
-- 复合查询
GET /items/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "智能手机"
}
}
],
"filter": [
{
"term": {
"brand.keyword": "华为"
}
},
{
"range": {
"price": {
"gte": 90000,
"lte": 159900
}
}
}
]
}
}
}
-- 排序查询
GET /items/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"sold": "desc"
},
{
"price": "asc"
}
]
}
-- 排序分页查询
GET /items/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"sold": "desc"
},
{
"price": "asc"
}
],
"from": 3,
"size": 3
}
-- 高亮显示,默认就是em标签
GET /items/_search
{
"query": {
"match": {
"name": "脱脂牛奶"
}
},
"highlight": {
"fields": {
"name": {
"pre_tags": "",
"post_tags": ""
}
}
}
}
-- 聚合-分组聚合(桶聚合)
GET /items/_search
{
"size": 0,
"aggs": {
"category_aggs": {
"terms": {
"field": "category.keyword",
"size": 10
}
},
"brand_aggs": {
"terms": {
"field": "brand.keyword",
"size": 10
}
}
}
}
-- 带条件聚合
GET /items/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"category.keyword": {
"value": "手机"
}
}
},
{
"range": {
"price": {
"gt": 300000
}
}
}
]
}
},
"size": 0,
"aggs": {
"brand_aggs": {
"terms": {
"field": "brand.keyword",
"size": 10
}
}
}
}
-- 度量聚合(stats:同时计算max、min、avg、sum、count)
GET /items/_search
{
"size": 0,
"query": {
"match": {
"category": "手机"
}
},
"aggs": {
"cate_aggs": {
"terms": {
"field": "brand.keyword",
"size": 20
},
"aggs": {
"price_stats_aggs": {
"stats": {
"field": "price"
}
}
}
}
}
}

浙公网安备 33010602011771号