基本概念
Elasticsearch也是基于Lucene的全文检索库,本质也是存储数据,很多概念与MySQL类似的。
对比(MySQL)关系
索引(indices)----------------------Databases 数据库
类型(type)-------------------------Table 数据表
文档(Document)---------------------Row 行
字段(Field)------------------------Columns 列
映射配置(mappings)字段的数据类型、属性、是否索引、是否存储等特性
ES常用命令
1.创建索引
curl -XPUT http://localhost:9200/music/ #music为索引名称
2.查看全部索引
curl -XGET http://localhost:9200/_cat/indices?v
3.插入文档
curl -H "Content-Type:application/json" -XPUT http://localhost:9200/music/songs/1 -d '{"name":"Deck the Halls","year":1885,"lyrics":"Fa la la la la"}'
4.查看文档
curl -XGET http://localhost:9200/music/songs/1
5.更新文档
ID相同则更新,不同则新增
6.删除文档
curl -XDELETE http://localhost:9200/music/songs/1
7.查看索引下全部记录
curl http://localhost:9200/music/songs/_search
8.删除索引
curl -XDELETE http://localhost:9200/music
9.删除全部索引
curl -XDELETE http://localhost:9200/_all 或者 curl -XDELETE http://localhost:9200/*
IK分词器下载地址
https://github.com/medcl/elasticsearch-analysis-ik/releases
- 下载后解压重命名文件夹为 analysis-ik
- 将该文件夹放入Elasticsearch的plugins目录,重启Elasticsearch
使用kibana常用命令
概念:Elasticsearch采用Rest风格API,因此其API就是一次http请求,你可以用任何工具发起http请求。kibana的控制台,对http请求进行简化。
创建索引的请求格式:
请求方式:PUT
请求路径:/索引库名
请求参数:json格式
----------------------------A.索引操作----------------------------
1.创建索引
#创建--某个test索引
PUT test/
{
"settings":{ 可以不设置默认5和1
"index":{
"number_of_shards":5, 是数据分片数,默认为5,有时候设置为3
"number_of_replicas":1 是数据备份数,如果只有一台机器,设置为0
}
}
}
#删除--某个索引
DELETE test/
#修改--副本数量设置
PUT test/_settings
{
"number_of_replicas":2
}
#查--某个索引
GET test/
#查--所有索引信息
GET _cat/indices?v
#查--test的设置信息
GET test/_settings
#查--所有的设置信息
GET _all/_settings
2.创建映射
索引有了,接下来肯定是添加数据。但是,在添加数据之前必须定义映射。
什么是映射?
映射是定义文档的过程,文档包含哪些字段,这些字段是否保存,是否索引,是否分词等。
映射说明:
PUT /索引库名/_mapping/类型名称(默认_doc类型)
{
"properties": {
"title": { 字段名
"type": "text",
字段类型,text、long、short、date、integer、object、keyword:不可分词
基本数据类型:long、interger、short、byte、double、float、half_float
浮点数的高精度类型:scaled_float等.
"index": true, 是否索引,默认为true
"store": true, 是否存储,默认为false
"analyzer": " ik_max_word" 分词器 ik_max_word和ik_smar
},
}
}
映射示例:
/*******************新增时间类型映射************************/
PUT test/_mapping/goods
{
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
/*******************新增时间类型映射************************/
PUT test1/_mapping/_doc
{
"properties": {
"TimeFormat": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss"
}
}
}
3.查看映射
GET /test/_mapping
type字段类型介绍
String类型,又分两种:
text:可分词,不可参与聚合
keyword:不可分词,数据会作为完整字段进行匹配,可以参与聚合
Numerical:数值类型,分两类
基本数据类型:
long、interger、short、byte、double、float、half_float
浮点数的高精度类型:
scaled_float
需要指定一个精度因子,比如10或100。
elasticsearch会把真实值乘以这个因子后存储,取出时再还原。
Date:日期类型
elasticsearch可以对日期格式化为字符串存储,
但是建议我们存储为毫秒值,存储为long,节省空间。
----------------------------B.数据操作----------------------------
1.增加数据
注:修改必须指定id,id对应文档 存在/不存在,则修改/新增。
如下: id=1存在,则为修改"price":8888.00
//指定id为1
PUT /test/goods/1
{
"title":"华为手机",
"images":"http://image.baidu.com/165422.jpg",
"type": "text",
"price":8888.00
}
#2.使用_builk 批量添加文档
PUT student //假设一张表,含有name和age字段
{
"mappings" : {
"properties" : {
"name" : {
"type" : "keyword"
},
"age" : {
"type" : "integer"
}
}
}
}