一基础概念
node | 节点,部署es程序的服务器 |
cluster | 集群多个节点 |
index | 索引,相当于数据库的名字 |
shard | 索引分片,默认5 |
replicas | 索引副本 |
type | 类型 |
document | 文档数据库的row |
其实我们可以理解为
关系型 | ES |
---|---|
database | index |
table | type |
document文档3元素(其实就是数据库里面的字段)
_index | 文档索引(相当于id) |
_type | 数据类型(相当于varchar) |
_ID | 文档唯一ID |
ES请求方式
GET | 查询 |
POST | 增\改\查 |
PUT | 增\改 |
DELETE | 删 |
ES数据类型
一级分类 | 二级分类 | 具体类型 |
---|---|---|
核心类型 | 字符串类型 | text keyword |
核心类型 | 整数型 | integer long short byte |
核心类型 | ||
核心类型 | ||
核心类型 | ||
核心类型 | ||
核心类型 | ||
核心类型 |
分词器分类
标准分词器查询 | |
IK | 中文分词器(自定义) |
delete sc_001 删除索引
创建索引(只能用PUT,在6.X版本中一个index(库)只能创建1个type(表))-----其实就相当于是创建了数据库
put 索引名称 /可以省略代表根目录
put sc_001
{
"settings":{
"number_of_shards":3 #分片数目默认5,
"number_of_replicas":2 #副本数量,根据集群数量添加
},
"mappings":{ #对table的设置
"user":{ #创建user表
"properties":{ #设置字段
"id":{ #id字段
"type":"long" #属性
},
"name":{
"type":"text"
},
"age":{
"type":"long"
}
}
}
}
}
查询索引库
GET sc_003
#以下为结果
{
"sc_001": {
"aliases": {},
"mappings": {
"user": {
"properties": {
"age": {
"type": "long"
},
"id": {
"type": "long"
},
"name": {
"type": "text"
}
}
}
},
"settings": {
"index": {
"creation_date": "1574304267283",
"number_of_shards": "4",
"number_of_replicas": "1",
"uuid": "wbiEpQzORT6yfXzJ3ah3gQ",
"version": {
"created": "6020499"
},
"provided_name": "sc_001"
}
}
}
}
管理文档(数据)
PUT /索引名称/类型名称/文档ID(POST可以不指定)
{
"字段名称":"字段值"
...
}
# 我们新增2条数据分别使用POST和GET 如果没有user的话会自动的创建
POST sc_001/user
{
"id":1,
"name":"zhangsan",
"age":40
}
PUT sc_001/user/1
{
"id":1,
"name":"LISI",
"age":31
}
修改数据-----实质是删除原来的数据后新增
PUT sc_001/user/1
{
"id":1,
"name":"LI22",
"age":32
}
GET sc_001/_search
查询
# 查看全部索引
GET _cat/indices
# 查看全部
GET _search
查询结果分析
took | 请求耗费时间,毫秒为单位 |
_shards | 查询的分片总数 |
timed_out | 查询是否超时 |
hits | 查询结果集 |
score分值越大搜索排名越靠前
数据查询的请求体
过滤查询:不进行打分,用于精准查询,比如查性别为"男" 不是范围性
filter要和bool联合使用,term指明子句的类型
query常用子句
match 匹配度查询 全文检索
term 过滤查询\匹配查询 精准查询 与filter组合
range 过滤查询\匹配度查询 范围查询 参数get 大于等于 lte小于等于
组合查询
bool子句
must 必须符合
must_not 不符合什么条件
should 满足条件会增加分值
filter 使用过滤模式
分页参数
from :分页开始位置
size:页大小
POST sc_003/user/_search
{
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 30,
"lte": 50
}
}
}
}
}
}
#根据条件查询数据
POST sc_003/user/_search
{
"from": 1,
"size": 1,
"query": {
"bool": {
"filter": {
"range": {
"age": {
"gte": 40,
"lte": 45
}
}
}
}
}
}
IK分词器
#粗分词
POST _analyze
{
"analyzer":"ik_smart",
"text": " 中华人名共和国合同法"
}
#细分词
POST _analyze
{
"analyzer":"ik_max_word",
"text": " 中华人名共和国合同法"
}