假如:有一个电商网站,需要为其基于ES构建一个后台系统,提供一下功能:
1.对商品信息进行CRUD(增删改查)操作
添加格式:
PUT /index/type/id
{
"json数据"
}
1.1新增商品,新增文档,建立索引
PUT /ecommerce/product/1 PUT/索引/类型/id
{
"name" : "gaolujie yagao", 商品名称
"desc" : "gaoxiao meibai", 商品功效
"price" : 30, 商品价格
"producer" : "gaolujie producer", 商品介绍
"tags": [ "meibai", "fangzhu" ] 标签
}
PUT /ecommerce/product/2
{
"name" : "jiajieshi yagao",
"desc" : "youxiao fangzhu",
"price" : 25,
"producer" : "jiajieshi producer",
"tags": [ "fangzhu" ]
}
PUT /ecommerce/product/3
{
"name" : "zhonghua yagao",
"desc" : "caoben zhiwu",
"price" : 40,
"producer" : "zhonghua producer",
"tags": [ "qingxin" ]
}
PUT /ecommerce/product/4
{
"name" : "special yagao",
"desc" : "special meibai",
"price" : 40,
"producer" : "special producer",
"tags": [ "meibai" ]
}
1.2删除
DELETE /ecommerce/product/1 DELEtE/索引/类型/id 如果要全部删除( DELEtE/索引//类型)
1.3修改
修改分两种方式:一种时覆盖,另一种则为修改
修改1
PUT /ecommerce/product/1
{
"name":"JiaJiangBan GaoLuJieYaGao",
"desc":"GaoXiaoMeiBai",
"price":30,
"producer":"GaoLuJieYaGaoProducer",
"tags":["MeiBai","FangZhu"]
}
以上的这种修改时覆盖,不推荐
修改2
POST /ecommerce/product/1/_update
{
"doc": {
"name":"JiaQiangBanGaoLuJieYaGao"
}
}
以上是修改指定的Field
1.4查询
1.4.1查看集群有多少索引
GET /_cat/indices?v
1.4.2查看1条数据
GET /ecommerce/product/1 释:GET /索引/类型/id
1.4.3查看全部数据(两种方式)
GET /ecommerce/product/_search
或:
GET /ecommerce/product/_search
{
"query"{"match_all":{}}
}
1.4.4查询数据+排序
搜索商品名称中包含yagao的商品,而且时按照售价降序排序 (两种方式)
GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
或:
GET /ecommerce/product/_search
{
"query": {
"match": {
"name": "yagao"
}
},
"sort": [
{ "price": "desc"}
]
}
1.4.5查询数据+分页
分页查询商品,总共3条商品,假设每页就显示一条商品,现在直接显示第2页,所以就查出来第2个商品
GET /ecommerce/product/_search
{
"query": {"match_all": {}},
"from": 1, 从0开始查
"size":1 查几条数据
}
1.4.6查询制定的字段
只查找商品的名称和价格
GET /ecommerce/product/_search
{
"query": {
"match_all": {}
},
"_source": ["name","price"]
}
1.4.7查询范围值,数据过滤
搜索商品名称包含yagao,而且售价大于25元的商品
GET /ecommerce/product/_search
{
"query": {
"bool": { 可以封装多个查询条件
"must": { 必须要匹配的条件
"match": { 条件
"name": "yagao" 条件值
}
},
"filter": { 范围筛选
"range": {
"price":{"gt":25} 价格大于25的商品
}
}
}
}
}
1.4.8全文检索(按规定的关键字查询)
producer这个字段会先被拆解,建立倒排索引
GET /ecommerce/product/_search
{
"query": {
"match": {
"producer": "关键字"
}
}
}
1.4.9短语搜索
GET /ecommerce/product/_search
{
"query": {
"match_phrase": {
"producer": "指定的短语" 释:查询的数据必须和指定短语一致,否则查不出
}
}
}
全文检索和短语搜索总结
全文检索会将输入的搜索串拆解开来,去倒排索引里面一一匹配,只要能匹配任意一个拆解后的单词,就可以作为结果返回
phrase search,要求输入的搜索串,必须在指定的字段文本中,完全包含一模一样,才可以算匹配,才能作为结果返回
1.4.10高亮搜索 (也就是根据关键字进行查询,查出来的关键字,为高亮形式)
GET /ecommerce/product/_search
{
"query": {
"match": {
"producer": "关键字"
}
},
"highlight": { 释:高亮的关键字
"fields": {
"producer": {}
}
}
}
2.对数据进行简单的聚合分析
2.1首先将文本的field的fielddata的属性设置为true 否则很有可能报错
PUT /ecommerce/_mapping/product
{
"properties": {
"tags":{
"type": "text",
"fielddata": true
}
}
}
2.2第一个分析需求:计算每个tag下的商品数量
GET /ecommerce/product/_search
{
"size": 0,
"aggs": {
"geoup_by_tags": {
"terms": {
"field": "tags"
}
}
}
}
2.3第二个聚合分析的需求,对名称中包含yagao的商品,计算每个tag下面的商品数量
GET /ecommerce/product/_search
{
"size": 0,
"query": {
"match": {
"name": "yagao"
}
},
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
}
}
}
}
2.4第三个聚合分析的需求,先分组,在算每组的平局值,计算每个tag下面的商品平局价格
GET /ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
2.5第四个分析需求:计算每个tag下的商品的平局价格,并且按照平均价格降序排序
GET /ecommerce/product/_search
{
"size":0,
"aggs": {
"group_by_tags": {
"terms": {
"field": "tags",
"order": {
"avg_price": "desc"
}
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
2.6.数据分析需求: 按照指定的价格范围区间进行分组,然后在每组内在按照teg进行分组,最后在计算每组的平均价格
GET /ecommerce/product/_search
{
"size": 0,
"aggs": {
"group_by_price": {
"range": {
"field": "price",
"ranges": [
{
"from": 0,
"to": 20
},
{
"from": 20,
"to": 40
},
{
"from": 40,
"to": 60
}
]
}
, "aggs": {
"group_by_tags": {
"terms": {
"field": "tags"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
}
}