最近其实有点忙,在主攻Django项目和go语言项目,所以只能利用下班晚上和周末总结一些知识点,因为最近的公司的项目没有用上Elasticsearch,所以总结一下,方便以后复习,以及以后面试会用上,不然过段时间,这些知识点都遗忘了。
目录
4.Elasticsearch映射(Mapping)和索引(Index)设置
5.Elasticsearch查询DSL(Domain Specific Language)
6. Elasticsearch聚合(Aggregations)操作
7.Elasticsearch高亮(Highlighting)
10. Elasticsearch安全与权限(Security and Authorization)
11. Elasticsearch监控与日志(Monitoring and Logging)
12.Elasticsearch集群管理(Cluster Management)
13.Elasticsearch索引别名(index Alias)
14.Elasticsearch索引末班与模式(Index Templates and Patterns)
15. Elasticsearch搜索操作(Search Operations)
16.Elasticsearch批量操作(Bulk Operations)
17.Elasticsearch搜索结果解析(Search Result Parsing)
18.Elasticsearch插件生态系统(Plugins Ecosystem)
1.Elasticsearch 的定义
Elasticsearch 是一个分布式、RESTful 风格的搜索与数据分析引擎。它可以用于全文搜索、结构化搜索以及分析;Elasticsearch 可以被用于构建实时搜索引擎,如电商网站的产品搜索功能。
2. Elasticsearch数据类型概览
| 数据类型分类 | 数据类型 | 描述 | 示例 |
|---|---|---|---|
| 核心数据类型 | string (text/keyword) | text 用于全文搜索,keyword 用于精确匹配。 |
"content": {"type": "text"} |
| numeric | 包括 byte, short, integer, long 等。 |
"age": {"type": "integer"} |
|
| date | 日期类型,用于存储日期和时间。 | "date": {"type": "date"} |
|
| boolean | 布尔类型,用于存储真值或假值。 | `"is_published": {"type": "boolean"} | |
| binary | 二进制数据类型。 | "file": {"type": "binary"} |
|
| 范围数据类型 | range | 包括 integer_range, float_range, date_range 等。 |
"age_range": {"type": "integer_range"} |
| 复杂数据类型 | object | 用于存储结构化数据,如 JSON 对象。 | "user": {"type": "object"} |
| nested | 嵌套对象,允许对内嵌数据进行索引和搜索。 | "comments": {"type": "nested"} |
|
| 地理数据类型 | geo_point | 存储地理位置的经纬度。 | "location": {"type": "geo_point"} |
| geo_shape | 存储地理形状,如多边形。 | "shape": {"type": "geo_shape"} |
|
| 专门数据类型 | ip | 用于存储 IP 地址。 | "ip": {"type": "ip"} |
| token_count | 用于统计字段中的词项数量。 | "word_count": {"type": "token_count"} |
|
| completion | 用于自动完成功能的字段。 | "suggest": {"type": "completion"} |
3.Elasticsearch的分析器(Analyzers)和分词器(Tokenizer)
| 组件 | 描述 | 示例 |
|---|---|---|
| 分析器(Analyzer) | 用于将文本转换为可搜索的项(tokens)。包括字符过滤器、分词器和词元过滤器。 | "analyzer": {"my_custom_analyzer": {"type": "standard"}} |
| 分词器(Tokenizer) | 将文本分解成单个或多个词条(terms)。 | "tokenizer": {"my_custom_tokenizer": {"type": "whitespace"}} |
| 词元过滤器(Token Filter) | 对分词器生成的词条进行转换或修改。 | "filter": {"my_custom_filter": {"type": "lowercase"}} |
| 标准分析器(Standard Analyzer) | 默认分析器,适用于大多数文本。 | "analyzer": {"default": {"type": "standard"}} |
| 简单分析器(Simple Analyzer) | 基于词元的边界分词,不使用词元过滤器。 | "analyzer": {"my_simple_analyzer": {"type": "simple"}} |
| 停用词过滤(Stop Token Filter) | 移除常见的停用词,如“and”、“the”等。 | "filter": {"my_stop_filter": {"type": "stop", "stopwords": ["and", "the"]}} |
分析器在索引文档时用于文本字段的分析,Tokenizer 用于将文本分解成词条,而 Token Filter 用于进一步处理这些词条,例如小写化、同义词处理或删除停用词。
示例:
假设我们有一个博客平台,需要对用户提交的帖子内容进行索引,以便于后续的搜索。我们可能会定义一个自定义分析器,如下所示:
PUT /my_blog_index
{
"settings": {
"analysis": {
"analyzer": {
"my_custom_analyzer": {
"type": "standard",
"stopwords": ["a", "an", "the"] // 自定义停用词列表
}
}
}
},
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "my_custom_analyzer"
}
}
}
}
在这个示例中,我们创建了一个名为 my_blog_index 的新索引,并定义了一个自定义的标准分析器 my_custom_analyzer,它使用了一个自定义的停用词列表。然后,我们将这个分析器应用到了 content 字段上。
4.Elasticsearch映射(Mapping)和索引(Index)设置
| 概念 | 描述 | 示例 |
|---|---|---|
| 映射(Mapping) | 定义文档中每个字段的数据类型和相关的属性,如是否可搜索、是否存储等。 | "mappings": {"properties": {"title": {"type": "text"}}} |
| 索引(Index) | 存储数据的结构,类似于关系型数据库中的“表”。 | "settings": {"index": {"number_of_shards": 3}}} |
| 分片(Shards) | 一个索引可以被分成多个分片以分布数据和提高查询性能。 | 如上例 |
| 副本(Replicas) | 每个分片可以有多个副本以提供高可用性和读取性能。 | "settings": {"index": {"number_of_replicas": 2}}} |
| 全文搜索(Full-Text Search) | 利用 text 类型字段进行全文搜索。 |
"query": {"match": {"content": "Elasticsearch"}} |
| 精确搜索(Exact Search) | 利用 keyword 类型字段进行精确匹配搜索。 |
"query": {"term": {"category.keyword": "finance"}} |
| 多字段(Multi-fields) | 一个字段可以定义为多个类型的字段,以支持不同的搜索需求。 | "properties": {"title": {"type": "text", "fields": {"raw": {"type": "keyword"}}}} |
示例:
假设我们正在设计一个新闻文章的索引,并且我们希望文章的标题能够支持全文搜索,同时支持基于标题的精确匹配搜索,如下所示:
PUT /news_index
{
"settings": {
"index": {
"number_of_shards": 3,
"number_of_replicas": 2
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "standard",
"fields": {
"raw": {
"type": "keyword"
}
}
},
"content": {
"type": "text"
},
"publication_date": {
"type": "date"
},
"category": {
"type": "keyword"
}
}
}
}
在这个示例中,我们创建了一个名为 news_index 的新索引,并设置了3个主分片和2个副本。文章的 title 字段被定义为 text 类型以支持全文搜索,并且通过 fields 定义了一个名为 raw 的子字段,它是 keyword 类型,用于支持精确匹配搜索。content 字段同样被定义为 text 类型,而 publication_date 和 category 分别被定义为 date 和 keyword 类型。
5.Elasticsearch查询DSL(Domain Specific Language)
| 查询类型 | 描述 | 示例 |
|---|---|---|
| Match Query | 用于在 text 类型字段上执行全文搜索。 |
"query": {"match": {"content": "Elasticsearch"}} |
| Term Query | 用于在 keyword 类型字段上执行精确值匹配。 |
"query": {"term": {"category.keyword": "finance"}} |
| Range Query | 用于查找数值或日期类型字段在指定范围内的文档。 | "query": {"range": {"age": {"gte": 18}}} |
| Bool Query | 组合多个查询,支持 must, should, must_not, filter。 |
"query": {"bool": {"must": [{"match": {"content": "Elasticsearch"}}]}} |
| Prefix Query | 用于查找以特定前缀开始的 text 或 keyword 类型字段的文档。 |
"query": {"prefix": {"title.keyword": "fin"}} |
| Wildcard Query | 支持使用通配符(* 和 ?)进行搜索。 | "query": {"wildcard": {"user.keyword": "j*"}} |
| Fuzzy Query | 允许搜索拼写错误的词条。 | "query": {"fuzzy": {"title.keyword": "elasticsearche"}} |
| Regexp Query | 使用正则表达式进行搜索。 | "query": {"regexp": {"title.keyword": "elast.*"}} |
| Aggregations | 用于执行数据分析和统计。 | "aggs": {"avg_price": {"avg": {"field": "price"}}} |
示例:
假设我们想要在新闻文章索引中搜索有关 "Elasticsearch" 的文章,并且希望结果按照发布日期排序,同时获取这些文章的平均阅读量:
GET /news_index/_search
{
"query": {
"match": {
"content": "Elasticsearch"
}
},
"sort": [
{
"publication_date": {
"order": "desc"
}
}
],
"aggs": {
"average_article_views": {
"avg": {
"field": "article_views"
}
}
}
}

最低0.47元/天 解锁文章
16万+

被折叠的 条评论
为什么被折叠?



