电商网站商品管理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了电商网站商品管理相关的知识,希望对你有一定的参考价值。
1、document数据格式面向文档的搜索分析引擎
(1)应用系统的数据结构都是面向对象的
(2)对象数据存储到数据库中,只能拆解,变为多张表,查询的时候还要还原为对象格式
(3)ES是面向文档的,与面向对象的数据结构一样,基于文档的数据结构,es可以提供复杂的索引、全文检索、分析聚合等功能
{ "email":"[email protected]", "first_name":"san", "last_name":"zhang", "info"{ "bio":"curison and modest", "age":30, "interests":["bike","climb"] }, "join_date":"2017-01-01" }
2、电商网站商品管理案例背景介绍
(1) 对商品信息进行CRUD(增删改查)
(2)执行简单的结构化查询
(3)可以执行简单的全文检索,以及复杂的phrase(短语)检索
(4)对于全文检索的结构,可以进行高亮显示
(5)对数据进行简单的聚合分析
3、简单的集群管理
(1)快速检查集群的健康状况
get _cat/health?v
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1525610669 20:44:29 elasticsearch green 1 1 0 0 0 0 0 0 - 100.0%
如何快速了解集群的健康状况?green、yellow、red?
green:每个索引的primary shared和replica shared都是active状态的
yellow:每个索引的primary shared都是active的,部分replica shared不是active
red:不是索引的primary shared都是active的,部分索引数据丢失的
(2)快速查看集群中有哪些索引?
get _cat/indices?v
(3)简单的索引操作
创建索引:PUT /test_index?pretty
删除索引:delete /test_index?pretty
4、商品的CRUD操作
(1)新增商品:新建文档、建立索引
PUT /index/type/id
PUT /ecommerce/product/1 { "name":"gaolujie yagao", "desc":"gaoxiao meibai", "price":30, "producer":"gaolujie producer", "tags":["meibai","fangzhu"] } PUT /ecommerce/product/2 { "name":"jiajieshi yagao", "desc":"gaoxiao 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"] }
返回结果:
{ "_index": "ecommerce", "_type": "product", "_id": "3", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1 }
ES会自动建立index和type,不需要提前创建,而且ES默认会对document每个field都建立倒排索引,让其可以被搜索
(2)查询商品,检索文档
GET /index/type/id
GET /ecommerce/product/2
(3)修改商品,替换文档
PUT /ecommerce/product/1 { "name":"jiaqiangban gaolujie yagao", "desc":"gaoxiao meibai", "price":30, "producer":"gaolujie producer" "tags":["meibai","fangzhu"] }
替换方式有一个不好的地方,必须带上所有的field,才能去进行信息修改,否则会丢失数据
(4)修改商品,更新文档
POST /ecommerce/product/1/_update { "doc":{ "name":"jiaqiangban gaolujie yagao" } }
(5)删除文档
delete /ecommerce/product/1?pretty
二、ES的查询语法
1、query string search
GET /ecommerce/product/_search GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
{ "took": 54, 耗费的毫秒数 "timed_out": false, 是否超时 "_shards": { "total": 5, 数据被拆成5个分片 "successful": 5, "skipped": 0, "failed": 0 }, hits": { "total": 3, 查询结果的数量,3个document "max_score": 1,search的相关度,越相关,就越匹配,分数就越高 "hits":包含了搜索的document的详细数据
2、query DSl 特定领域的语言
GET /ecommerce/product/_search { "query":{"match_all": {}} }
查询名称包含yagao的商品,同时按照价格降序排序
GET /ecommerce/product/_search { "query":{"match": {"name": "yagao"}} , "sort": [ {"price": "desc"} ] }
分页查询
GET /ecommerce/product/_search { "query":{"match_all": {}}, "from": 1, "size":1 }
指定要查询商品的名称和价格就可以了
GET /ecommerce/product/_search { "query":{"match_all": {}}, "_source": ["name","price"] }
query filter
搜索商品名称包含牙膏,而且售价大于25元的商品
GET /ecommerce/product/_search { "query": { "bool": { "must": [ {"match": { "name": "yagao" }} ],"filter": {"range": { "price": { "gte": 30 } }} } } }
full-text search
PUT /ecommerce/product/4 { "name":"special yagao", "desc":"special meibai", "price":50, "producer":"special yagao producer", "tags":["meibai"] }
GET /ecommerce/product/_search { "query": {"match": { "producer": "yagao producer" }} }
phrase search
跟全文索引相对应,全文索引会将输入的搜索串拆解开来,去倒排索引里面一一匹配,只要匹配上任意一个拆解后的单词,就可以作为结果返回,phrase search要求输入的搜索串,必须在指定的字段文本中,完全包含一模一样的,才算匹配
GET /ecommerce/product/_search { "query": {"match_phrase": { "producer": "yagao producer" }} }
highlight search
GET /ecommerce/product/_search { "query": {"match": { "producer": "producer" }}, "highlight": {"fields": { "producer": {} }} }
三、使用restful api做简单的聚合计算
计算每个tag下的商品数量
GET /ecommerce/product/_search { "aggs": { "group_by_tags": { "terms": { "field": "tags" } } } }
将文本field的fieldata属性设置为TRUE
put /ecommerce/_mapping/product { "properties":{ "tags":{ "type":"text", "fielddata":true } } }
GET /ecommerce/product/_search { "size":0, "aggs": { "group_by_tags": { "terms": { "field": "tags" } } } }
对名称中包含yagao的商品,计算每个tag下的商品数量
GET /ecommerce/product/_search { "size":0, "query": {"match": { "name": "yagao" }}, "aggs": { "group_by_tags": { "terms": { "field": "tags" } } } }
先分组,在算每组的平均值,计算每个tag下的商品的平均价格
GET /ecommerce/product/_search { "size":0, "aggs": { "group_by_tags": { "terms": { "field": "tags" }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } }
计算每个tag下的商品的平均价格,并降序排序
GET /ecommerce/product/_search { "size":0, "aggs": { "group_by_tags": { "terms": { "field": "tags", "order": { "avg_price": "desc" } }, "aggs": { "avg_price": { "avg": { "field": "price" } } } } } }
按照指定的价格范围区间进行分组,然后在每组内再安装tag进行分组,最后在计算每组的平均价格
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" } } } } } } } }
以上是关于电商网站商品管理的主要内容,如果未能解决你的问题,请参考以下文章