es
概念
Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL类似的。
对比关系:
索引(indices)--------------------------------Databases 数据库
类型(type)-----------------------------Table 数据表
文档(Document)----------------Row 行
字段(Field)-------------------Columns 列
详细说明:
概念 | 说明 |
---|---|
索引库(indices) | indices是index的复数,代表许多的索引, |
类型(type) | 类型是模拟mysql中的table概念,一个索引库下可以有不同类型的索引,比如商品索引,订单索引,其数据格式不同。不过这会导致索引库混乱,因此未来版本中会移除这个概念 |
文档(document) | 存入索引库原始的数据。比如每一条商品信息,就是一个文档 |
字段(field) | 文档中的属性 |
映射配置(mappings) | 字段的数据类型、属性、是否索引、是否存储等特性 |
-
索引集(Indices,index的复数):逻辑上的完整索引
-
分片(shard):数据拆分后的各个部分
-
副本(replica):每个分片的复制
要注意的是:Elasticsearch本身就是分布式的,因此即便你只有一个节点,Elasticsearch默认也会对你的数据进行分片和副本操作,当你向集群添加新数据时,数据也会在新加入的节点中进行平衡。
es安装
-
安装elastic search
-
解压elasticsearch-6.5.4.zip
-
打开config/elasticsearch.yml,在文件末尾加上以下配置:(为了让kibanna访问到)
http.cors.enabled: true http.cors.allow-origin: "*"
-
启动bin目录中的 elasticsearch.bat
-
验证 访问 http://localhost:9200/ ,有如下效果:
{ "name" : "Dals5Z1", "cluster_name" : "elasticsearch", "cluster_uuid" : "ohNc9vzoRua-ItxlDa8aXw", "version" : { "number" : "6.5.4", "build_flavor" : "default", "build_type" : "zip", "build_hash" : "d2ef93d", "build_date" : "2018-12-17T21:17:40.758843Z", "build_snapshot" : false, "lucene_version" : "7.5.0", "minimum_wire_compatibility_version" : "5.6.0", "minimum_index_compatibility_version" : "5.0.0" }, "tagline" : "You Know, for Search" }
-
-
安装kibana
-
解压kibana-6.5.4-windows-x86_64.zip
-
进入bin,运行kibana.bat
-
验证,访问 http://localhost:5601/
-
选择左侧的DevTools菜单,即可进入控制台页面
-
执行查询条件,即可访问
-
-
安装Ik分词器
-
解压elasticsearch-analysis-ik-6.5.4.zip
-
将文件copy到es的plugins下
-
验证
在控制下输入
POST _analyze { "analyzer": "ik_max_word", "text": "我是中国人" }
运行得到结果:
{ "tokens" : [ { "token" : "我", "start_offset" : 0, "end_offset" : 1, "type" : "CN_CHAR", "position" : 0 }, { "token" : "是", "start_offset" : 1, "end_offset" : 2, "type" : "CN_CHAR", "position" : 1 }, { "token" : "中国人", "start_offset" : 2, "end_offset" : 5, "type" : "CN_WORD", "position" : 2 }, { "token" : "中国", "start_offset" : 2, "end_offset" : 4, "type" : "CN_WORD", "position" : 3 }, { "token" : "国人", "start_offset" : 3, "end_offset" : 5, "type" : "CN_WORD", "position" : 4 } ] }
-
-
安装拼音分词器
-
解压elasticsearch-analysis-pinyin-6.5.4.zip
-
将文件copy到es的plugins下
-
玩一玩es
关于索引
创建索引
语法
Elasticsearch采用Rest风格API,因此其API就是一次http请求,你可以用任何工具发起http请求
创建索引的请求格式:
-
请求方式:PUT
-
请求路径:/索引库名(小写)
-
请求参数:json格式:
{ "settings": { "number_of_shards": 3, "number_of_replicas": 2 } }
-
settings:索引库的设置
-
number_of_shards:分片数量
-
number_of_replicas:副本数量
-
测试
-
postman测试
路径:http://127.0.0.1:9200/laobai
请求方式:PUT
请求体:
数据格式:json
-
Kibana测试
PUT /laobai2 { "settings": { "number_of_shards": 3, "number_of_replicas": 2 } }
结果:
{ "acknowledged" : true, "shards_acknowledged" : true, "index" : "laobai2" }
-
查看索引设置
Get请求可以帮我们查看索引信息,格式: GET 索引库名: GET laobai
或者 GET * 查询所有配置
HEAD /索引库名 查看当前索引是否存在:HEAD /laobai
删除索引
Delete请求可以帮我们删除索引信息,格式DELETE /索引库名 : DELETE /laobai
创建映射字段
请求方式依然是PUT
PUT /索引库名/_mapping/类型名称 { "properties": { "字段名": { "type": "类型", "index": true, "store": true, "analyzer": "分词器" } } }
-
类型名称:就是前面将的type的概念,类似于数据库中的不同表 字段名:任意填写 ,可以指定许多属性,例如:
-
type:类型,可以是text、long、short、date、integer、object等
-
index:是否索引,默认为true
-
store:是否存储,默认为false
-
analyzer:分词器,这里的
ik_max_word
即使用ik分词器
查看映射关系
语法:
GET /索引库名/_mapping
示例:
GET /laobai/_mapping
字段属性详解
type
Elasticsearch中支持的数据类型非常丰富:
我们说几个关键的:
-
String类型,又分两种:
-
text:可分词,不可参与聚合
-
keyword:不可分词,数据会作为完整字段进行匹配,可以参与聚合
-
-
Numerical:数值类型,分两类
-
基本数据类型:long、interger、short、byte、double、float、half_float
-
浮点数的高精度类型:scaled_float
-
需要指定一个精度因子,比如10或100。elasticsearch会把真实值乘以这个因子后存储,取出时再还原。
-
-
-
Date:日期类型
elasticsearch可以对日期格式化为字符串存储,但是建议我们存储为毫秒值,存储为long,节省空间。
index
index影响字段的索引情况。
-
true:字段会被索引,则可以用来进行搜索。默认值就是true
-
false:字段不会被索引,不能用来搜索
index的默认值就是true,也就是说你不进行任何配置,所有字段都会被索引。
但是有些字段是我们不希望被索引的,比如商品的图片信息