【云知易】Elasticsearch服务 入门 01 快速开始使用ES
前言
对于新上手的用户,可能还不太清楚如何使用Elasticsearch服务,今天就为大家科普下使用Elasticsearch服务的具体过程,在此之前,先讲下使用Elasticsearch服务搜索数据的基本流程,如下所示:
第一步:新建集群
第二步:接入集群
第三步:定义数据
第四步:加载数据建立索引
第五步:搜索数据
第六步:删除集群
为了方便大家了解使用过程,我们以如下业务场景来模拟用户的需求场景和使用场景。
场景描述
某服装品牌在电商网站上经营业务,主要销售适合春夏秋冬四季穿戴的男装和女装。为了更好地了解商品的销售状况,该店铺开始使用Elasticsearch服务来进行商品月销量查询、商品价格查询、商品名称查询等,通过查询结果来充分了解目前的经营状况,进而更准确地进行决策和管理。
店铺提供的商品数据(JSON格式,文件名为“products.json”)部分内容如下所示:
[{"_id":"1","price":"100","productID":"XHDK-A-1293-#fJ3","time":"2017-09-01","level":"2","productName":"2017秋装新款文艺衬衫女装","productType1":"象牙白","productType2":"M","monthlySales":"328","commentTime":"2017-09-04","commentNum":"715","commentConetnt":"衣服真的超级好看"}, {"_id":"2","price":"205","productID":"KDKE-B-9947-#kL5","time":"2017-09-01","level":"2","productName":"2017秋装新款文艺针织毛衫女装"","productType1":"玫瑰红","productType2":"L","monthlySales":"584","commentTime":"2017-09-05","commentNum":"406","commentConetnt":"卖家包装精致"}, {"_id":"3","price":"300","productID":"JODL-X-1937-#pV7","time":"2017-09-01","level":"2","productName":"2017秋装新款文艺T恤男装","productType1":"午夜黑","productType2":"XL","monthlySales":"1245","commentTime":"2017-09-03","commentNum":"502","commentConetnt":"衣服质量不错啊"}, {"_id":"4","price":"310","productID":"QQPX-R-3956-#aD8","time":"2017-09-02","level":"2","productName":"2017秋装新款文艺风衣外套女装","productType1":"宝石蓝","productType2":"L","monthlySales":"411","commentTime":"2017-09-05","commentNum":"436","commentConetnt":"关注店家很久了"}, {"_id":"5","price":"150","productID":"ABEF-C-1820-#mC6","time":"2017-09-03","level":"2","productName":"2017秋装新款文艺牛仔裤女装","productType1":"海军蓝","productType2":"M","monthlySales":"1223","commentTime":"2017-09-06","commentNum":"1200","commentConetnt":"买到真的很惊喜"}, {"_id":"6","price":"200","productID":"BCQP-E-2365-#qE4","time":"2017-09-04","level":"2","productName":"2017秋装新款文艺休闲裤男装","productType1":"海军蓝","productType2":"L","monthlySales":"997","commentTime":"2017-09-10","commentNum":"301","commentConetnt":"打开包装,衣服没有异味"}, {"_id":"7","price":"250","productID":"EABE-D-1476-#oB1","time":"2017-09-10","level":"2","productName":"2017秋装新款文艺连衣裙女装","productType1":"青绿色","productType2":"S","monthlySales":"841","commentTime":"2017-09-15","commentNum":"299","commentConetnt":"很喜欢的衣服"}, {"_id":"8","price":"108","productID":"CDXK-F-1527-#pL2","time":"2017-09-11","level":"2","productName":"2017秋装新款文艺半身群女装","productType1":"棕色","productType2":"M","monthlySales":"85","commentTime":"2017-09-14","commentNum":"22","commentConetnt":"衣服买小了"}, {"_id":"9","price":"450","productID":"MMCE-H-4728-#nP9","time":"2017-09-11","level":"2","productName":"2017秋装新款文艺羽绒服女装","productType1":"白色","productType2":"M","monthlySales":"114","commentTime":"2017-09-14","commentNum":"22","commentConetnt":"衣服超厚,今年冬天好过了"}, {"_id":"10","price":"260","productID":"OCDA-G-2817-#bD3","time":"2017-09-12","level":"2","productName":"2017秋装新款文艺毛呢外套女装","productType1":"灰色","productType2":"L","monthlySales":"2004","commentTime":"2017-09-15","commentNum":"826","commentConetnt":"这样的价格很划算"}, {"_id":"11","price":"980","productID":"ZKDS-J-5490-#cW4","time":"2017-09-13","level":"2","productName":"2017秋装新款文艺棉服女装","productType1":"白色","productType2":"M","monthlySales":"112","commentTime":"2017-09-16","commentNum":"219","commentConetnt":"衣服穿着很舒服"}, {"_id":"12","price":"98","productID":"FKQB-I-2564-#dA5","time":"2017-09-15","level":"2","productName":"2017秋装新款文艺打底裤女装","productType1":"黑色","productType2":"M","monthlySales":"4345","commentTime":"2017-09-18","commentNum":"5473","commentConetnt":"这条裤子买了两条"}, {"_id":"13","price":"150","productID":"DMQY-K-6579-#eS6","time":"2017-09-21","level":"2","productName":"2017秋装新款文艺女鞋","productType1":"白色","productType2":"37","monthlySales":"2840","commentTime":"2017-09-25","commentNum":"5831","commentConetnt":"鞋子很好穿"}, {"_id":"14","price":"200","productID":"GKLW-l-2897-#wQ7","time":"2017-09-22","level":"2","productName":"2017秋装新款文艺男鞋","productType1":"白色","productType2":"39","monthlySales":"5879","commentTime":"2017-09-25","commentNum":"7200","commentConetnt":"鞋子很舒服"}, {"_id":"15","price":"300","productID":"HWEC-L-2531-#xP8","time":"2017-09-23","level":"2","productName":"2017秋装新款阔腿裤女装","productType1":"黑色","productType2":"M","monthlySales":"403","commentTime":"2017-09-26","commentNum":"607","commentConetnt":"款型很好看"}, {"_id":"16","price":"100","productID":"IQPD-M-3214-#yQ1","time":"2017-09-24","level":"2","productName":"2017秋装新款配雪纺衫","productType1":"乳白色","productType2":"M","monthlySales":"3045","commentTime":"2017-09-27","commentNum":"5021","commentConetnt":"卖家服务态度真好"}, {"_id":"17","price":"350","productID":"LPEC-N-4572-#zX2","time":"2017-09-25","level":"2","productName":"2017秋装新款皮衣女装","productType1":"黑色","productType2":"M","monthlySales":"239","commentTime":"2017-09-28","commentNum":"407","commentConetnt":"好评,价格实惠"}, {"_id":"18","price":"110","productID":"NQAB-O-3768-#sM3","time":"2017-09-26","level":"2","productName":"2017秋装新款内衣女装","productType1":"白色","productType2":"S","monthlySales":"6089","commentTime":"2017-09-29","commentNum":"7021","commentConetnt":"颜色很好,做工也不错"}, {"_id":"19","price":"210","productID":"HWNB-P-7879-#tN4","time":"2017-09-27","level":"2","productName":"2017秋装新款内衣男装","productType1":"白色","productType2":"L","monthlySales":"3201","commentTime":"2017-09-30","commentNum":"4059","commentConetnt":"我很喜欢,质量也不错"}, {"_id":"20","price":"230","productID":"JKHU-Q-8865-#uO5","time":"2017-09-29","level":"2","productName":"2017秋装新款文艺牛仔裤男装","productType1":"天蓝色","productType2":"M","monthlySales":"2056","commentTime":"2017-10-02","commentNum":"3842","commentConetnt":"衣服穿了几天了,洗过一次,好像没缩水"}]
新建集群
在使用Elasticsearch服务正式搜索数据之前,需要店铺使用者新建一个Elasticsearch集群,新建一个名称为“Sample-ESCluster”的集群。
1. 登录ES管理控制台。
2. 在“总览”或者“集群管理”页面,单击“创建搜索集群”,进入新建集群页面。
3. 在“基本信息”区域,输入“集群名称”,选择“可用区域”和“搜索引擎版本”。
4. 在“主机规格”区域,指定"节点数量"、“节点规格”、“存储类型”和“存储空间”。
5. 在“网络”区域,指定“虚拟私有云”、“子网”和“安全组”。
6. 单击“立即申请”,进入规格确认界面。
7. 规格确认完成后,勾选“我已经阅读并接受《公测免费使用服务条款协议》”,然后单击“提交申请”开始创建集群。
8. 系统将自动跳转到集群列表页面,您创建的集群将展现在集群列表中。且集群状态为“创建中”,耐心等待几分钟,创建成功后集群状态会变为“服务中”。
如果集群创建失败,请根据界面提示,重新创建集群。
集群新建完成后,通过集群列表,查看“Sample-ESCluster”的集群内网访问地址。如下图所示,集群内网访问地址包含集群IP和端口号,如集群IP为192.168.0.138,访问Elasticsearch需要使用端口9200,访问Kibana请使用端口5601。
接入集群
在集群创建完成后,需要申请一台弹性云主机接入集群,进行后续操作。
请访问此链接申请一台Windows操作系统的弹性云主机,http://www.huaweicloud.com/product/ecs.html。
新申请的弹性云主机需满足如下要求:
1. 操作系统为Windows。
2. 与上文创建的集群,在相同的可用区域(AZ)、相同网段、相同虚拟私有云(VPC)以及相同安全组。
3. 请按照界面指导,为此弹性云服务器申请弹性IP。
4. 按照弹性云主机的操作指导,为弹性云主机申请支持“密码方式登录Windows弹性云服务器”。
下面介绍如何使用新申请的弹性云主机访问集群。
1. 在本地使用mstsc和弹性IP远程登录此弹性云主机,或者在华为云弹性云主机的管理控制台,在此弹性云主机行中单击“远程登录”直接访问此弹性云主机。
2. 从互联网下载一个浏览器,只要浏览器满足这些版本:Chrome(36.0及以上版本)、FireFox(35.0及以上版本)、IE(11.0及以上版本,不支持IE9)。
3. 打开新安装的浏览器,直接在浏览器地址中输入Kibana的访问路径。即http://{Cluster Internal access address}。{Cluster Internal access address}为访问Elasticsearch集群的内网访问地址,包含集群IP和访问Kibana的端口号,其中,集群IP为集群内网访问地址,端口号为5601。如http://192.168.0.187:5601。
这样,您就通过弹性云主机,接入了Elasticsearch集群,您可以通过kibana进行后续操作。
定义数据
为了方便后续操作顺利进行,需要店铺使用者为“products.json”文件中的数据进行数据定义。
1. 上个步骤申请的弹性云主机中,打开浏览器,输入http://{Cluster Internal access address}打开Kibana。
{Cluster Internal access address}为访问Elasticsearch集群的内网访问地址,包含集群IP和访问Kibana的端口号,其中,集群IP为集群内网访问地址,端口号为5601。如http://192.168.0.187:5601。2. 在“Kibana Dev Tools”界面,单击“Get to work”,进入Console界面。
3. 在Console界面,如图所示。执行如下命令,创建索引“my_store”,并指定自定义映射来定义数据类型,设置无需对精确值字段进行分析。
PUT /my_store { "mappings": { "products": { "properties": { "productID": { "type": "text", "index": "not_**yzed" }, "_id": { "type": "integer", "index": "not_**yzed" }, "level": { "type": "text", "index": "not_**yzed" }, "productName": { "type": "text", "index": "not_**yzed" }, "price": { "type": "integer", "index": "not_**yzed" }, "productType1": { "type": "text", "index": "not_**yzed" }, "productType2": { "type": "text", "index": "not_**yzed" }, "monthlySales": { "type": "integer", "index": "not_**yzed" }, "commentTime": { "type": "date", "index": "not_**yzed" }, "commentNum": { "type": "integer", "index": "not_**yzed" }, "commentContent": { "type": "text", "index": "not_**yzed" }, "time": { "type": "date", "index": "not_**yzed" } } } } }
加载数据建立索引
在Kibana Console输入如下命令,为文档建立products类型的索引“my_store”。
POST /my_store/products/_bulk { "index": { "_id": 1 } } { "price": 100, "productID": "XHDK-A-1293-#fJ3", "time": "2017-09-01", "level": "A", "productName": "2017秋装新款文艺衬衫女装", "productType1": "象牙白", "productType2": "M", "monthlySales": 328, "commentTime": "2017-09-04", "commentNum": 715, "commentConetnt": "衣服真的超级好看" } { "index": { "_id": 2 } } { "price": 205, "productID": "KDKE-B-9947-#kL5", "time": "2017-09-01", "level": "A", "productName": "2017秋装新款文艺针织毛衫女装", "productType1": "玫瑰红", "productType2": "L", "monthlySales": 584, "commentTime": "2017-09-05", "commentNum": 406, "commentConetnt": "卖家包装精致" } { "index": { "_id": 3 } } { "price": 300, "productID": "JODL-X-1937-#pV7", "time": "2017-09-01", "level": "A", "productName": "2017秋装新款文艺T恤男装", "productType1": "午夜黑", "productType2": "XL", "monthlySales": "1245", "commentTime": "2017-09-03", "commentNum": 502, "commentConetnt": "衣服质量不错啊" } { "index": { "_id": 4 } } { "price": 310, "productID": "QQPX-R-3956-#aD8", "time": "2017-09-02", "level": "B", "productName": "2017秋装新款文艺风衣外套女装", "productType1": "宝石蓝", "productType2": "L", "monthlySales": 411, "commentTime": "2017-09-05", "commentNum": 436, "commentConetnt": "关注店家很久了" } { "index": { "_id": 5 } } { "price": 150, "productID": "ABEF-C-1820-#mC6", "time": "2017-09-03", "level": "B", "productName": "2017秋装新款文艺牛仔裤女装", "productType1": "海军蓝", "productType2":"M", "monthlySales":1223, "commentTime":"2017-09-06", "commentNum":1200, "commentConetnt":"买到真的很惊喜" } { "index": { "_id": 6 } } { "price": 200, "productID": "BCQP-E-2365-#qE4", "time": "2017-09-04", "level": "B", "productName": "2017秋装新款文艺休闲裤男装", "productType1": "海军蓝", "productType2": "L", "monthlySales": "997", "commentTime": "2017-09-10", "commentNum": "301", "commentConetnt": "打开包装,衣服没有异味" } { "index": { "_id": 7 } } { "price": 250, "productID": "EABE-D-1476-#oB1", "time": "2017-09-10", "level": "A", "productName": "2017秋装新款文艺连衣裙女装", "productType1": "青绿色", "productType2": "S", "monthlySales": 841, "commentTime": "2017-09-15", "commentNum": 299, "commentConetnt": "很喜欢的衣服" } { "index": { "_id": 8 } } { "price": 108, "productID": "CDXK-F-1527-#pL2", "time": "2017-09-11", "level": "A", "productName": "2017秋装新款文艺半身群女装", "productType1": "棕色", "productType2": "M", "monthlySales": 85, "commentTime": "2017-09-14", "commentNum": 22, "commentConetnt": "衣服买小了" } { "index": { "_id": 9 } } { "price": 450, "productID": "MMCE-H-4728-#nP9", "time": "2017-09-11", "level": "A", "productName": "2017秋装新款文艺羽绒服女装", "productType1": "白色", "productType2": "M", "monthlySales": 114, "commentTime": "2017-09-14", "commentNum": 22, "commentConetnt": "衣服超厚,今年冬天好过了" } { "index": { "_id": 10 } } { "price": 260, "productID": "OCDA-G-2817-#bD3", "time": "2017-09-12", "level": "B", "productName": "2017秋装新款文艺毛呢外套女装", "productType1": "灰色", "productType2": "L", "monthlySales": 2004, "commentTime": "2017-09-15", "commentNum": 826, "commentConetnt": "这样的价格很划算" } { "index": { "_id": 11 } } { "price": 980, "productID": "ZKDS-J-5490-#cW4", "time": "2017-09-13", "level": "B", "productName": "2017秋装新款文艺棉服女装", "productType1": "白色", "productType2": "M", "monthlySales": 112, "commentTime": "2017-09-16", "commentNum": 219, "commentConetnt": "衣服穿着很舒服" } { "index": { "_id": 12 } } { "price": 98, "productID": "FKQB-I-2564-#dA5", "time": "2017-09-15", "level": "B", "productName": "2017秋装新款文艺打底裤女装", "productType1": "黑色", "productType2": "M", "monthlySales": 4345, "commentTime": "2017-09-18", "commentNum": 5473, "commentConetnt": "这条裤子买了两条" } { "index": { "_id": 13 } } { "price": 150, "productID": "DMQY-K-6579-#eS6", "time": "2017-09-21", "level": "A", "productName": "2017秋装新款文艺女鞋", "productType1": "白色", "productType2": "37", "monthlySales": 2840, "commentTime": "2017-09-25", "commentNum": 5831, "commentConetnt": "鞋子很好穿" } { "index": { "_id": 14 } } { "price": 200, "productID": "GKLW-l-2897-#wQ7", "time": "2017-09-22", "level": "A", "productName": "2017秋装新款文艺男鞋", "productType1": "白色", "productType2": "39", "monthlySales": 5879, "commentTime": "2017-09-25", "commentNum": 7200, "commentConetnt": "鞋子很舒服" } { "index": { "_id": 15 } } { "price": 300, "productID": "HWEC-L-2531-#xP8", "time": "2017-09-23", "level": "A", "productName": "2017秋装新款阔腿裤女装", "productType1": "黑色", "productType2": "M", "monthlySales": 403, "commentTime": "2017-09-26", "commentNum": 607, "commentConetnt": "款型很好看" } { "index": { "_id": 16 } } { "price": 100, "productID": "IQPD-M-3214-#yQ1", "time": "2017-09-24", "level": "B", "productName": "2017秋装新款配雪纺衫", "productType1": "乳白色", "productType2": "M", "monthlySales": 3045, "commentTime": "2017-09-27", "commentNum": 5021, "commentConetnt": "卖家服务态度真好" } { "index": { "_id": 17 } } { "price": 350, "productID": "LPEC-N-4572-#zX2", "time": "2017-09-25", "level": "B", "productName": "2017秋装新款皮衣女装", "productType1": "黑色", "productType2": "M", "monthlySales": 239, "commentTime": "2017-09-28", "commentNum": 407, "commentConetnt": "好评,价格实惠" } { "index": { "_id": 18 } } { "price": 110, "productID": "NQAB-O-3768-#sM3", "time": "2017-09-26", "level": "B", "productName": "2017秋装新款内衣女装", "productType1": "白色", "productType2": "S", "monthlySales": 6089, "commentTime": "2017-09-29", "commentNum": 7021, "commentConetnt": "颜色很好,做工也不错" } { "index": { "_id": 19 } } { "price": 210, "productID": "HWNB-P-7879-#tN4", "time": "2017-09-27", "level": "B", "productName": "2017秋装新款内衣男装", "productType1": "白色", "productType2": "L", "monthlySales": 3201, "commentTime": "2017-09-30", "commentNum": 4059, "commentConetnt": "我很喜欢,质量也不错" } { "index": { "_id": 20 } } { "price": 230, "productID": "JKHU-Q-8865-#uO5", "time": "2017-09-29", "level": "C", "productName": "2017秋装新款文艺牛仔裤男装", "productType1": "天蓝色", "productType2": "M", "monthlySales": 2056, "commentTime": "2017-10-02", "commentNum": 3842, "commentConetnt": "衣服穿了几天了,洗过一次,好像没缩水" }
当返回结果信息中created字段值为true时,表示创建成功。
Ok,搜索数据前需要进行的操作已完成,现在可以使用Elasticsearch服务来进行想要的数据搜索了。结合前面的场景,以常用的操作来举例,店铺使用者可进行如下操作来搜索数据。
搜索数据
一、精确值查找
查找价格为20的商品信息,并且不进行评分。
GET /my_store/products/_search { "query" : { "constant_score" : { "filter" : { "term" : { "price" : 230 } } } } }
查找产品ID为“XHDK-A-1293-#fJ3”的商品信息。
GET /my_store/products/_search { "query" : { "constant_score" : { "filter" : { "term" : { "productID" : "XHDK-A-1293-#fJ3" } } } } }
查找月销量为584的商品信息。
GET /my_store/products/_search { "query" : { "constant_score" : { "filter" : { "term" : { "monthlySales" : 584 } } } } }
查找商品名称为“2017秋装新款文艺衬衫女装”的商品信息。
GET /my_store/products/_search { "query" : { "constant_score" : { "filter" : { "term" : { "productName" : "2017秋装新款文艺衬衫女装" } } } } }
二、组合过滤
查询价格非300,价格为230或产品ID为XHDK-A-1293-#fJ3”的商品信息:
GET /my_store/products/_search { "query" : { "constant_score" : { "filter" : { "bool" : { "should" : [ { "term" : {"price" : 230}}, {"term" : {"productID" : "XHDK-A-1293-#fJ3"} } ], "must_not" : { "term" : {"price" : 300} } } } } } }
三、多个精确值查找
查询价格为200或300的商品信息:
GET /my_store/products/_search { "query" : { "constant_score" : { "filter" : { "terms" : { "price" : [200, 300] } } } } }
删除集群
按照上述步骤对示例数据进行搜索,得到满意的结果后,想必大家已完全了解了Elasticsearch服务的使用流程和方法,还可以参考如下步骤,删除示例集群以及示例数据,避免造成资源浪费。
由于集群删除后,数据无法恢复,请谨慎操作。
1. 登录Elasticsearch管理控制台。在左侧菜单栏选择“集群管理”。
2. 进入集群管理页面,选中“Sample-ESCluster”集群所在行,在操作列选择“删除”。
3. 在弹出的确认对话框中,单击“确认”完成操作。
更多内容,请参见Elasticsearch服务帮助中心(http://support.huaweicloud.com/es_gls/index.html)。
- 点赞
- 收藏
- 关注作者
评论(0)