PUT product_db
{
"mappings": {
"properties": {
"id": {
"type": "long"
},
"name": {
"type": "text",
"analyzer": "ik_max_word"
},
"keywords": {
"type": "text",
"analyzer": "ik_max_word"
},
"subTitle": {
"type": "text",
"analyzer": "ik_max_word"
},
"salecount": {
"type": "long"
},
"putawayDate": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"price": {
"type": "double"
},
"promotionPrice": {
"type": "keyword"
},
"originalPrice": {
"type": "keyword"
},
"pic": {
"type": "keyword"
},
"sale": {
"type": "long"
},
"hasStock": {
"type": "boolean"
},
"brandId": {
"type": "long"
},
"brandName": {
"type": "keyword"
},
"brandImg": {
"type": "keyword"
},
"categoryId": {
"type": "long"
},
"categoryName": {
"type": "keyword"
},
"attrs": {
"type": "nested",
"properties": {
"attrId": {
"type": "long"
},
"attrName": {
"type": "keyword"
},
"attrValue": {
"type": "keyword"
}
}
}
}
}
}
上方语句表示新建一个名称为product_db的索引
可能碰到的问题:
org.elasticsearch.index.mapper.MapperParsingException: analyzer [ik_max_word] not found for field [name]
原因是:当前安装的elasticsearch没有安装ik分词器
解决办法:
- 进入 elasticsearch 安装目录下的 bin\ 目录,在此目录打开命令行窗口,输入下面代码安装IK分词器插件
elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/vX.X.X/elasticsearch-analysis-ik-X.X.X.zip
注意:上方指令中的X.X.X表示的是当前安装的elasticsearch的版本号
PUT /product_db/_doc/1
{
"id": "26",
"name": "小米 11 手机",
"keywords": "小米手机",
"subTitle": "AI智慧全面屏 6GB +64GB 亮黑色 全网通版 移动联通电信4G手机 双卡双待双卡双待",
"price": "3999",
"promotionPrice": "2999",
"originalPrice": "5999",
"pic": "http://macro‐oss.oss‐cn‐shenzhen.aliyuncs.com/mall/images/20180615/xiaomi.jpg",
"sale": 999,
"hasStock": true,
"salecount": 999,
"putawayDate": "2021-04-01",
"brandId": 6,
"brandName": "小米",
"brandImg": "http://macro‐oss.oss‐cn‐shenzhen.aliyuncs.com/mall/images/20190129/1e34aef2a409119018a4c6258e39ecfb_222_222.png",
"categoryId": 19,
"categoryName": "手机通讯",
"attrs": [
{
"attrId": 1,
"attrName": "cpu",
"attrValue": "2核"
},
{
"attrId": 2,
"attrName": "颜色",
"attrValue": "黑色"
}
]
}
上方语句表明在product_db索引下新增一个序号为1的数据文档
POST /product_db/_update/1
{
"doc":{
"name": "小米12pro hahha"
}
}
上方的指令表示:将序号为1的文档中属性名为name的属性修改成对应的字符串,相当于:
update product_db set name="小米12pro hahha" where _id=1
GET product_db
上方指令表示查询product_db索引的具体属性
GET product_db/_search
{
"query": {
"match_all": {}
}
}
上方指令表示查询product_db索引下的所有数据,相当于:
select * from product_db
GET product_db/_search
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}
上方指令表示从序号10的文档开始向后查询10条数据,相当于:
select * from product_db where _id >=10 limit 10
- 注意:如果size没有设置的话,默认是10
GET product_db/_search
{
"query": { "match_all": {} },
"sort": { "price": { "order": "desc" } }
}
上方指令表示查询出的数据以price字段值倒序,相当于
select * from product_db order by price desc limit 10
- 注意:如果price不是数字类型的话,可能无法进行排序
GET product_db/_search
{
"query": { "match_all": {} },
"_source": ["id", "name", "price"]
}
上方指令表示查询所有数据中的id、name、price三个字段,相当于:
select id, name, price from product_db
GET product_db/_search
{
"query": { "match": { "price": 3999 } }
}
上方指令表示只查询price是3999的数据,相当于:
select * from product_db where price=3999
只要不是数字类型,使用match都是模糊查询
GET product_db/_search
{
"query": { "match": { "name": "小米" } }
}
GET product_db/_search
{
"query": { "match": { "name": "小米 华为" } }
}
上方两条指令分别相当于:
select * from product_db where name like '%小米%'
select * from product_db where name like '%小米' or name like '%华为%'
- 注意:后面一个指令的小米只是以小米为后缀的模糊查询,而不是任意包含小米字段的模糊查询
GET product_db/_search
{
"query": {
"bool": {
"must": [
{ "match": { "name": "小米" } },
{ "match": { "name": "华为" } }
],
"should": [
{ "match": { "name": "苹果" } },
{ "match": { "name": "三星" } }
],
"must_not": [
{ "match": { "price": 2999 } }
]
}
}
}
上方指令是一个与、或、非组合查询语句,相当于:
select * from product_db where name like '%小米%华为%' or name like '%苹果%三星%' and price != 2999
GET product_db/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"price": {
"gte": 2000,
"lte": 5000
}
}
}
}
}
}
上方指令相当于:
select * from product_db where price between 2000 and 5000
相当于SQL中的聚集函数,比如分组、求和、求平均数之类的
GET product_db/_search
{
"size": 0,
"aggs": {
"group_by_brandId": {
"terms": {
"field": "brandId"
}
}
}
}
上方指令相当于:
SELECT brandId, COUNT(*) FROM product_db GROUP BY brandId ORDER BY COUNT(*) DESC LIMIT 10;
GET product_db/_search
{
"size": 0,
"aggs": {
"group_by_brandId": {
"terms": {
"field": "brandId"
},
"aggs": {
"average_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
上方指令相当于:
SELECT brandId, COUNT(*), AVG(price) FROM product_db GROUP BY brandId ORDER BY COUNT(*) DESC LIMIT 10;
- 注意:如果price不是数字类型的话,无法求平均值
GET product_db/_search
{
"size": 0,
"aggs": {
"group_by_salecount": {
"range": {
"field": "salecount",
"ranges": [
{
"from": 2000,
"to": 3000
},
{
"from": 3000,
"to": 4000
},
{
"from": 4000,
"to": 5000
}
]
},
"aggs": {
"group_by_brandId": {
"terms": {
"field": "brandId"
},
"aggs": {
"average_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}
上方指令表示统计销售数量在三个销售数量区间内,各个品牌的平均价格
- 注意:如果price不是数字类型的话,无法求平均值
因篇幅问题不能全部显示,请点此查看更多更全内容