文章目录
- 核心概念
- Mapping
- 搜索和查询
核心概念
1.节点
2.角色
3.分片
1.分为主分片和副本分片
2.两个分片的作用
主分片可读可写
副本分片只可读
3.分片的规则
4.集群
5.索引和文档
一个索引就可以看成是一张表,一个文档就是一条数据
Mapping
1.介绍
1.基本概念
2.查看当前索引为Mapping
1.创建一个索引
PUT test_mapping
2.查看Mapping
GET test_mapping/_mapping
2.字段数据类型
1.官方文档
https://www.elastic.co/guide/en/elasticsearch/reference/7.10/mapping-types.html
2.介绍
1.数字类型
2.基本数据类型
3.Keywords类型
4.Dates类型
5.对象类型
6.空间数据类型
7.文档排名类型
8.文本搜索类型
3.两种映射类型
1.自动映射
1.例子:如果直接向索引中插入文档,就会自动创建映射类型
2.可以自动映射的类型
2.显式映射(生产环境)
1.案例
PUT test_mapping1
{
"mappings": {
"properties": {
"title": {
"type": "text"
},
"name": {
"type": "text",
"fields": {
"name2": {
"type": "keyword"
}
}
},
"age": {
"type": "byte"
}
}
}
}
4.映射参数介绍
1.index 是否建立倒排索引
1.定义
2.创建一个index为false的案例
1.创建test_index索引,其中的title字段的index为false
PUT test_index
{
"mappings": {
"properties": {
"title": {
"type": "text",
"index": false
}
}
}
}
2.插入一条文档
PUT test_index/_doc/1
{
"title":"elastic"
}
3.查询索引,发现确实有数据
GET test_index/_search
4.但是使用这个字段去查询发现无效,因为没有设置index参数
GET test_index/_search
{
"query": {
"match": {
"title": "elastic"
}
}
}
2.analyzer 指定分词器
1.定义
2.注意
这个字段可以指定分词器就说明,字段可以被分词,一般为text
3.boost 评分权重
4.coerce 是否允许强制类型转换
5.doc_values 为了提升聚合以及排序效率
6.dynamic 控制是否可以动态添加新字段
7.enable 是否全局创建倒排索引
8.format 格式化
9.Ignore_above 超过长度将被忽略
5.Text类型和Keyword类型
1.Text类型一般是和match一起使用
2.Keyword类型
1.概述
2.语法
3.注意事项
搜索和查询
1.ES查询上下文、评分、排除或者包括字段
1.数据
1.创建索引
PUT test_index
{
"mappings": {
"properties": {
"title": {
"type": "text"
},
"description": {
"type": "text"
},
"price": {
"type": "double"
}
}
}
}
2.添加数据
PUT /test_index/_doc/1
{
"title": "text",
"description": "description",
"price": 1.2
}
2.查询上下文
GET /test_index/_search
{
"took" : 921, 当前请求消耗的毫秒
"timed_out" : false, 是否超时
"_shards" : { 当前请求的分片
"total" : 1, 一共一个
"successful" : 1, 成功了一个
"skipped" : 0, 跳过0个
"failed" : 0 失败0个
},
"hits" : { 查询的结果
"total" : {
"value" : 1, 一共一条结果
"relation" : "eq" 查询的关系是等于
},
"max_score" : 1.0, 最大相关度评分
"hits" : [
{
"_index" : "test_index", 索引
"_type" : "_doc", 类型固定是_doc
"_id" : "1", 文档id
"_score" : 1.0, 文档分数
"_source" : { 源数据
"title" : "text",
"description" : "description",
"price" : 1.2
}
}
]
}
}
3.相关度评分
如果不是自己设置了排序字段,会自动按照相关度评分排序
4.排除或者包括字段
GET /test_index/_search
{
"_source": {
"includes": "title",
"excludes": "price"
},
"query": {
"match_all": {}
}
}
2.精准匹配 term
1.新增数据
PUT /test_index/_doc/2
{
"title:": "xiao mi"
}
2.使用term去查询
1.代码
GET /test_index/_search
{
"query": {
"term": {
"title": "xiao mi"
}
}
}
2.结果发现查询不到
原因是xiao mi默认会使用分词器被分成xiao 和 mi,但是使用term查询时就相当于使用xiao mi去查询,根本没有
所以一般term都是跟keyword类型一起使用的
3.使用match和match_phrase的区别
1.match(查得到)
GET /test_index/_search
{
"query": {
"match": {
"title:": "mi xiao"
}
}
}
2.match_phrase(查不到)
GET /test_index/_search
{
"query": {
"match_phrase": {
"title:": "mi xiao"
}
}
}
3.对比
首先xiao mi会被分词为xiao和mi
match查询时会将自己的mi和xiao分词,然后任意一个人匹配到即可查询成功
match_phrase查询时会将自己的分词为mi和xiao,但是需要与被查询字段的分词顺序一致并且包含match_phrase的全部分词才可以,所以查不到
4.关于keyword类型
keyword类型指的是将这个字段的数据整体作为一个索引,可以整体查询,ignore_above字段就是取多少个字符
term一般就是查询keyword的
5.总结
1.match:分词查询
2.match_phrase:分词查询,但要求顺序相同
3.term:整体查询
4.keyword:整体作为索引
3.批量查询 terms
1.介绍
进行批量查询,只要元素在这个列表内,就可以被查询到,一般与keyword字段一起用
2.应用
4.范围查询 range
1.介绍
range就是范围查询,gt是大于,lt是小于,加上e就是大于等于和小于等于
2.案例
1.大于等于10小于等于20
2.也可以对日期类型进行操作
3.可以使用now参数对于天进行操作
4.使用time_zone参数应对时区问题,加八个小时,实际上是对查询的日期减八个小时,变成了4月15号
5.过滤器 filter
6.组合查询 bool query
1.定义
2.must 子句
GET /test_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "xiao mi"
}
},
{
"match": {
"desc": "shouji zhong"
}
}
]
}
}
}
3.filter 子句
GET /test_index/_search
{
"query": {
"bool": {
"filter": [
{
"match": {
"name": "xiao mi"
}
},
{
"match": {
"desc": "shouji zhong"
}
}
]
}
}
}
4.must_not 子句
GET /test_index/_search
{
"query": {
"bool": {
"must_not": [
{
"match": {
"name": "xiao mi"
}
},
{
"match": {
"desc": "shouji zhong"
}
}
]
}
}
}
5.should 子句(相当于or)
GET /test_index/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "xiao mi"
}
},
{
"match": {
"desc": "shouji zhong"
}
}
]
}
}
}
6.组合查询
1.简单组合
GET /test_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "xiao mi"
}
},
{
"match": {
"desc": "shouji zhong"
}
}
],
"filter": [
{
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
]
}
}
}
2.关于should的问题
如果在组合查询中有了其他的条件,那么should就可以一个都不满足,除非指定参数
GET /test_index/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "xiao mi"
}
},
{
"match": {
"desc": "shouji zhong"
}
}
],
"filter": [
{
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
],
"minimum_should_match": 1
}
}
}