ElasticSearch7.10-搜索和查询

发布于:2025-02-11 ⋅ 阅读:(50) ⋅ 点赞:(0)

文章目录

核心概念

1.节点

CleanShot 2024-12-16 at 17.31.40@2x

2.角色

CleanShot 2024-12-16 at 17.34.57@2x

3.分片

1.分为主分片和副本分片

CleanShot 2024-12-17 at 16.24.53@2x

2.两个分片的作用

主分片可读可写

副本分片只可读

3.分片的规则

CleanShot 2024-12-17 at 16.27.55@2x

4.集群

CleanShot 2024-12-17 at 16.35.38@2x

5.索引和文档

一个索引就可以看成是一张表,一个文档就是一条数据

Mapping

1.介绍

1.基本概念

CleanShot 2024-12-17 at 16.51.33@2x

2.查看当前索引为Mapping
1.创建一个索引
PUT test_mapping
2.查看Mapping
GET test_mapping/_mapping

CleanShot 2024-12-17 at 16.57.48@2x

2.字段数据类型

1.官方文档

https://www.elastic.co/guide/en/elasticsearch/reference/7.10/mapping-types.html

CleanShot 2024-12-18 at 16.52.22@2x

2.介绍
1.数字类型

CleanShot 2024-12-18 at 16.53.44@2x

2.基本数据类型

CleanShot 2024-12-18 at 16.54.30@2x

3.Keywords类型

CleanShot 2024-12-18 at 16.54.44@2x

4.Dates类型

CleanShot 2024-12-18 at 16.56.29@2x

5.对象类型

CleanShot 2024-12-18 at 16.57.29@2x

6.空间数据类型

CleanShot 2024-12-18 at 16.58.09@2x

7.文档排名类型

CleanShot 2024-12-18 at 16.59.42@2x

8.文本搜索类型

CleanShot 2024-12-18 at 16.59.55@2x

3.两种映射类型

1.自动映射
1.例子:如果直接向索引中插入文档,就会自动创建映射类型

CleanShot 2024-12-18 at 17.08.52@2x

2.可以自动映射的类型

CleanShot 2024-12-18 at 17.12.07@2x

2.显式映射(生产环境)
1.案例
PUT test_mapping1
{
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "name": {
        "type": "text",
        "fields": {
          "name2": {
            "type": "keyword"
          }
        }
      },
      "age": {
        "type": "byte"
      }
    }
  }
}

4.映射参数介绍

1.index 是否建立倒排索引
1.定义

CleanShot 2024-12-18 at 17.36.21@2x

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

CleanShot 2024-12-18 at 17.42.37@2x

4.但是使用这个字段去查询发现无效,因为没有设置index参数
GET test_index/_search
{
  "query": {
    "match": {
      "title": "elastic"
    }
  }
}

CleanShot 2024-12-18 at 17.45.51@2x

2.analyzer 指定分词器
1.定义

CleanShot 2024-12-21 at 15.37.56@2x

2.注意

这个字段可以指定分词器就说明,字段可以被分词,一般为text

CleanShot 2024-12-21 at 15.38.37@2x

3.boost 评分权重

CleanShot 2024-12-21 at 15.41.35@2x

CleanShot 2024-12-21 at 15.40.50@2x

4.coerce 是否允许强制类型转换

CleanShot 2024-12-21 at 15.41.41@2x

5.doc_values 为了提升聚合以及排序效率

CleanShot 2024-12-21 at 15.42.41@2x

6.dynamic 控制是否可以动态添加新字段

CleanShot 2024-12-21 at 15.44.59@2x

7.enable 是否全局创建倒排索引

CleanShot 2024-12-21 at 15.48.07@2x

8.format 格式化

CleanShot 2024-12-21 at 15.50.11@2x

9.Ignore_above 超过长度将被忽略

5.Text类型和Keyword类型

1.Text类型一般是和match一起使用

2.Keyword类型
1.概述

CleanShot 2024-12-21 at 16.07.00@2x

2.语法

CleanShot 2024-12-21 at 16.10.23@2x

3.注意事项

CleanShot 2024-12-21 at 16.13.27@2x

搜索和查询

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": {}
  }
}

CleanShot 2024-12-21 at 17.16.06@2x

2.精准匹配 term

1.新增数据
PUT /test_index/_doc/2
{
  "title:": "xiao mi"
}
2.使用term去查询
1.代码
GET /test_index/_search
{
  "query": {
    "term": {
        "title": "xiao mi"
    }
  }
}

CleanShot 2024-12-21 at 17.27.52@2x

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字段就是取多少个字符

CleanShot 2024-12-24 at 15.14.51@2x

term一般就是查询keyword的

CleanShot 2024-12-24 at 15.19.54@2x

5.总结
1.match:分词查询
2.match_phrase:分词查询,但要求顺序相同
3.term:整体查询
4.keyword:整体作为索引

3.批量查询 terms

1.介绍

进行批量查询,只要元素在这个列表内,就可以被查询到,一般与keyword字段一起用

2.应用

CleanShot 2024-12-24 at 15.31.26@2x

4.范围查询 range

1.介绍

range就是范围查询,gt是大于,lt是小于,加上e就是大于等于和小于等于

2.案例
1.大于等于10小于等于20

CleanShot 2024-12-24 at 15.33.40@2x

2.也可以对日期类型进行操作

CleanShot 2024-12-24 at 15.35.38@2x

3.可以使用now参数对于天进行操作

CleanShot 2024-12-24 at 15.36.17@2x

4.使用time_zone参数应对时区问题,加八个小时,实际上是对查询的日期减八个小时,变成了4月15号

CleanShot 2024-12-24 at 15.42.54@2x

5.过滤器 filter

CleanShot 2024-12-24 at 15.49.44@2x

6.组合查询 bool query

1.定义

CleanShot 2024-12-24 at 15.48.50@2x

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
    }
  }
}
7.嵌套查询

CleanShot 2024-12-25 at 15.15.00@2x


网站公告

今日签到

点亮在社区的每一天
去签到