1.1. 基本概念
程序源码论坛-1024,网址 www.cx1314.cn 仅分享最流行最优质的IT资源! 不同于其他论坛平台,这里只有精品、稀有资源,已泛滥、已过时、垃圾资源不录入! Java,前端,python,人工智能,大数据,云计算...持续更新资源-最新完整且均不加密、、、
活动线报,宅男福利,最新大片...
程序员的新大陆-更新最快的IT资源社区!开发者必备平台! 欢迎访问:www.cx1314.cn 百度搜索-> 程序源码论坛
Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL类似的。
对比关系:
索引(indices)----------------------Databases 数据库 类型(type)--------------------------Table 数据表 文档(Document)----------------------Row 行 字段(Field)-------------------------Columns 列
要注意的是:Elasticsearch本身就是分布式的,因此即便你只有一个节点,Elasticsearch默认也会对你的数据进行分片和副本操作,当你向集群添加新数据时,数据也会在新加入的节点中进行平衡。
1.2. 索引操作(indeces)
1.2.1. 查询索引
查看es中有哪些索引库:
GET /_cat/indices?v
es 中会默认存在一个名为.kibana和.kibana_task_manager的索引
表头的含义
字段名 | 含义说明 |
---|---|
health | green(集群完整) yellow(单点正常、集群不完整) red(单点不正常) |
status | 是否能使用 |
index | 索引名 |
uuid | 索引统一编号 |
pri | 主节点几个 |
rep | 从节点几个 |
docs.count | 文档数 |
docs.deleted | 文档被删了多少 |
store.size | 整体占空间大小 |
pri.store.size | 主节点占 |
1.2.2. 创建索引
PUT /索引名
参数可选:指定分片及副本,默认分片为3,副本为2。
{ "settings": { "number_of_shards": 3, "number_of_replicas": 2 } }
演示:说明索引创建成功
再次查询,可以看到刚刚创建的索引:
1.2.3. 查看索引具体信息
GET /索引名
或者,我们可以使用*来查询所有索引具体信息
1.2.4. 删除索引
DELETE /索引库名
演示:
查看atguigu:
1.3. 映射配置(_mapping)
索引有了,接下来肯定是添加数据。但是,在添加数据之前必须定义映射。
什么是映射?
映射是定义文档的过程,文档包含哪些字段,这些字段是否保存,是否索引,是否分词等
只有配置清楚,Elasticsearch才会帮我们进行索引库的创建(不一定)
1.3.1. 创建映射字段
PUT /索引库名/_mapping/类型名称 { "properties": { "字段名": { "type": "类型", "index": true, "store": true, "analyzer": "分词器" } } }
类型名称:就是前面将的type的概念,类似于数据库中的不同表
字段名:类似于列名,properties下可以指定许多字段。
每个字段可以有很多属性。例如:
-
type:类型,可以是text、long、short、date、integer、object等
-
index:是否索引,默认为true
-
store:是否存储,默认为false
-
analyzer:分词器,这里使用ik分词器:
ik_max_word
或者ik_smart
示例
发起请求:
PUT atguigu/_mapping/goods { "properties": { "title": { "type": "text", "analyzer": "ik_max_word" }, "images": { "type": "keyword", "index": "false" }, "price": { "type": "long" } } }
响应结果:
{ "acknowledged": true }
1.3.2. 查看映射关系
语法:
GET /索引库名/_mapping
示例:
GET /atguigu/_mapping
响应:
{ "atguigu" : { "mappings" : { "goods" : { "properties" : { "images" : { "type" : "keyword", "index" : false }, "price" : { "type" : "long" }, "title" : { "type" : "text", "analyzer" : "ik_max_word" } } } } } }
type:字段类型。String(text keyword) Numeric(long integer float double) date boolean
index:是否创建索引
analyzer:分词器(ik_max_word)
1.4. 新增文档(document)
有了索引、类型和映射,就可以对文档做增删改查操作了。
1.4.1. 基本玩法
如果我们想要自己新增的时候指定id,可以这么做:
POST /索引库名/类型/id值 { ... }
演示:
查询得到两条数据:小米手机的id是我们指定的id
-
_source
:源文档信息,所有的数据都在里面。 -
_id
:这条文档的唯一标示,与文档自己的id字段没有关联
1.4.2. 智能判断
事实上Elasticsearch非常智能,你不需要给索引库设置任何mapping映射,它也可以根据你输入的数据来判断类型,动态添加数据映射。
测试一下:
POST /atguigu/goods/2 { "title":"小米手机", "images":"http://image.jd.com/12479122.jpg", "price":2899, "stock": 200, "saleable":true, "attr": { "category": "手机", "brand": "小米" } }
我们额外添加了stock库存,saleable是否上架,attr其他属性几个字段。
来看结果:GET /atguigu/_search
{ "took" : 7, "timed_out" : false, "_shards" : { "total" : 2, "successful" : 2, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 1.0, "hits" : [ { "_index" : "atguigu", "_type" : "goods", "_id" : "1", "_score" : 1.0, "_source" : { "title" : "华为手机", "images" : "http://image.jd.com/12479122.jpg", "price" : 4288 } }, { "_index" : "atguigu", "_type" : "goods", "_id" : "2", "_score" : 1.0, "_source" : { "title" : "小米手机", "images" : "http://image.jd.com/12479122.jpg", "price" : 2899, "stock" : 200, "saleable" : true, "attr" : { "category" : "手机", "brand" : "小米" } } } ] } }
再看下索引库的映射关系: GET /atguigu/_mapping
{ "atguigu" : { "mappings" : { "goods" : { "properties" : { "attr" : { "properties" : { "brand" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } }, "category" : { "type" : "text", "fields" : { "keyword" : { "type" : "keyword", "ignore_above" : 256 } } } } }, "images" : { "type" : "keyword", "index" : false }, "price" : { "type" : "long" }, "saleable" : { "type" : "boolean" }, "stock" : { "type" : "long" }, "title" : { "type" : "text", "analyzer" : "ik_max_word" } } } } } }
stock,saleable,attr都被成功映射了。
如果是字符串类型的数据,会添加两种类型:text + keyword。如上例中的category 和 brand
1.5. 删除数据
删除使用DELETE请求,同样,需要根据id进行删除:
语法
DELETE /索引库名/类型名/id值
示例:
DELETE /atguigu/goods/3
结果:
{ "_index" : "atguigu", "_type" : "goods", "_id" : "3", "_version" : 2, "result" : "deleted", "_shards" : { "total" : 4, "successful" : 1, "failed" : 0 }, "_seq_no" : 1, "_primary_term" : 1 }