1. 首页
  2. elasticsearch教程

11-十一、Elasticsearch 教程: 填充数据

上一章节我们已经规划好了要填充的用户、索引和 JSON 数据,本章节我们就把它们添加到 Elasticsearch 中吧。

本章节中,我们将添加一些索引、映射和数据到 Elasticsearch 中。为后面的章节添加一些测试用例

创建索引


PUT http://localhost:9200/user_admin?pretty

请求正文

可以在请求正文中包含特定于索引的设置,但现在,默认设置为空

响应


{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "user_admin" }

意思是创建索引成功

创建映射和添加数据

Elasticsearch 会根据请求正文中提供的数据自动创建映射

这里,我们使用批量添加功能在此索引中添加多个 JSON 对象


POST http://localhost:9200/user_admin/_bulk?pretty

请求正文


{"index":{"_index":"user_admin", "_type":"user", "_id":"1"} } {"nickname":"站长","description":"创业是的天赋是天生的,而我偏偏是后生的","street":"东四十条","city":"Beijing","state":"Beijing","zip":"100007","location":[116.432727,39.937732],"money":5201314,"tags":["PHP", "Python"],"vitality":"9.0"} {"index":{"_index":"user_admin", "_type":"user", "_id":"2"}} {"nickname":"雅少","description":"虚怀若谷","street":"四川大学","city":"Chengdu","state":"Sichuan","zip":"610044","location":[104.094537,30.640174],"money":68023,"tags":["Python", "HTML"],"vitality":"7.8"} {"index":{"_index":"user_admin", "_type":"user", "_id":"3"}} {"nickname":"歌者","description":"程序设计也是设计,研发新菜也是研发","street":"五道口","city":"Beijing","state":"Beijing","zip":"100083","location":[116.346346,39.999333],"money":71128,"tags":["Java", "Scala"],"vitality":"6.9"}

响应内容


{ "took": 1001, "errors": false, "items": [ { "index": { "_index": "user_admin", "_type": "user", "_id": "1", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1, "status": 201 } }, { "index": { "_index": "user_admin", "_type": "user", "_id": "2", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1, "status": 201 } }, { "index": { "_index": "user_admin", "_type": "user", "_id": "3", "_version": 1, "result": "created", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 0, "_primary_term": 1, "status": 201 } } ] }

_bulk 模式 JSON 串有逆天的要求:每个json串不能换行,只能放一行,同时一个json串和一个json串之间,必须有一个换行,最后要以一个新行结尾

否则就会报错:json_e_o_f_exception

添加另一个索引

创建索引


POST http://localhost:9200/user?pretty

请求正文

可以在请求正文中包含特定于索引的设置,但现在,默认设置为空

响应


{ "acknowledged": true, "shards_acknowledged": true, "index": "user" }

创建映射和添加数据


POST http://localhost:9200/user/_bulk?pretty

请求正文


{"index":{"_index":"user", "_type":"user", "_id":"1"} } {"nickname":"question", "description":"问题少年也是少年","street":"张江高科技园区","city":"Shanghai","state":"Shanghai","zip":"201204","location":[121.60632,31.199305],"money":13648,"tags":["VUE", "HTML"],"vitality":"8.8"} {"index":{"_index":"user", "_type":"user", "_id":"2"} } {"nickname":"枫晚","description":"停车坐爰枫林晚","street":"苏州大学","city":"Suzhou","state":"Jiangsu","zip":"215006","location":[120.65426,31.30797],"money":10235,"tags":["Java", "Android"],"vitality":"3.5"}

响应内容


{"index":{"_index":"user", "_type":"user", "_id":"1"} } {"nickname":"question", "description":"问题少年也是少年","street":"张江高科技园区","city":"Shanghai","state":"Shanghai","zip":"201204","location":[121.60632,31.199305],"money":13648,"tags":["VUE", "HTML"],"vitality":"8.8"} {"index":{"_index":"user", "_type":"user", "_id":"2"} } {"nickname":"枫晚","description":"停车坐爰枫林晚","street":"苏州大学","city":"Suzhou","state":"Jiangsu","zip":"215006","location":[120.65426,31.30797],"money":10235,"tags":["Java", "Android"],"vitality":"3.5"}

希望读者能够给小编留言,也可以点击[此处扫下面二维码关注微信公众号](https://www.ycbbs.vip/?p=28 "此处扫下面二维码关注微信公众号")

看完两件小事

如果你觉得这篇文章对你挺有启发,我想请你帮我两个小忙:

  1. 关注我们的 GitHub 博客,让我们成为长期关系
  2. 把这篇文章分享给你的朋友 / 交流群,让更多的人看到,一起进步,一起成长!
  3. 关注公众号 「方志朋」,公众号后台回复「666」 免费领取我精心整理的进阶资源教程
  4. JS中文网,Javascriptc中文网是中国领先的新一代开发者社区和专业的技术媒体,一个帮助开发者成长的社区,是给开发者用的 Hacker News,技术文章由为你筛选出最优质的干货,其中包括:Android、iOS、前端、后端等方面的内容。目前已经覆盖和服务了超过 300 万开发者,你每天都可以在这里找到技术世界的头条内容。

    本文著作权归作者所有,如若转载,请注明出处

    转载请注明:文章转载自「 Java极客技术学习 」https://www.javajike.com

    标题:11-十一、Elasticsearch 教程: 填充数据

    链接:https://www.javajike.com/article/1254.html

« 12-十二、Elasticsearch 教程: API 约定
10-十、Elasticsearch 教程: 准备数据»

相关推荐

QR code