- Elasticsearch 教程
- Elasticsearch - 主页
- Elasticsearch - 基本概念
- Elasticsearch - 安装
- Elasticsearch - 填充
- 版本之间的迁移
- Elasticsearch - API 约定
- Elasticsearch - 文档 API
- Elasticsearch - 搜索 API
- Elasticsearch - 聚合
- Elasticsearch - 索引 API
- Elasticsearch - CAT API
- Elasticsearch - 集群 API
- Elasticsearch - 查询 DSL
- Elasticsearch - 映射
- Elasticsearch - 分析
- Elasticsearch - 模块
- Elasticsearch - 索引模块
- Elasticsearch - 摄取节点
- Elasticsearch - 管理索引生命周期
- Elasticsearch - SQL 访问
- Elasticsearch - 监控
- Elasticsearch - 汇总数据
- Elasticsearch - 冻结索引
- Elasticsearch - 测试
- Elasticsearch - Kibana 仪表板
- Elasticsearch - 按字段过滤
- Elasticsearch - 数据表
- Elasticsearch - 区域地图
- Elasticsearch - 饼图
- Elasticsearch - 面积图和条形图
- Elasticsearch - 时间序列
- Elasticsearch - 标签云
- Elasticsearch - 热图
- Elasticsearch - 画布
- Elasticsearch - 日志 UI
- Elasticsearch 有用资源
- Elasticsearch - 快速指南
- Elasticsearch - 有用的资源
- Elasticsearch - 讨论
Elasticsearch - 分析
当在搜索操作期间处理查询时,分析模块会分析任何索引中的内容。该模块由分析器、分词器、分词过滤器和字符过滤器组成。如果未定义分析器,则默认情况下内置分析器、令牌、过滤器和分词器将注册到分析模块。
在以下示例中,我们使用标准分析器,在未指定其他分析器时使用该分析器。它将根据语法分析句子并生成句子中使用的单词。
POST _analyze
{
"analyzer": "standard",
"text": "Today's weather is beautiful"
}
运行上面的代码,我们得到如下所示的响应 -
{
"tokens" : [
{
"token" : "today's",
"start_offset" : 0,
"end_offset" : 7,
"type" : "",
"position" : 0
},
{
"token" : "weather",
"start_offset" : 8,
"end_offset" : 15,
"type" : "",
"position" : 1
},
{
"token" : "is",
"start_offset" : 16,
"end_offset" : 18,
"type" : "",
"position" : 2
},
{
"token" : "beautiful",
"start_offset" : 19,
"end_offset" : 28,
"type" : "",
"position" : 3
}
]
}
配置标准分析器
我们可以为标准分析仪配置各种参数,以满足我们的定制要求。
在以下示例中,我们将标准分析器配置为 max_token_length 为 5。
为此,我们首先使用具有 max_length_token 参数的分析器创建一个索引。
PUT index_4_analysis
{
"settings": {
"analysis": {
"analyzer": {
"my_english_analyzer": {
"type": "standard",
"max_token_length": 5,
"stopwords": "_english_"
}
}
}
}
}
接下来我们应用带有文本的分析器,如下所示。请注意,令牌不会出现,因为它的开头有两个空格,结尾有两个空格。对于“is”这个词来说,它的开头和结尾都有一个空格。把它们全部加起来,就变成了 4 个带空格的字母,但这并不能使它成为一个单词。至少在开头或结尾应该有一个非空格字符,以使其成为要计数的单词。
POST index_4_analysis/_analyze
{
"analyzer": "my_english_analyzer",
"text": "Today's weather is beautiful"
}
运行上面的代码,我们得到如下所示的响应 -
{
"tokens" : [
{
"token" : "today",
"start_offset" : 0,
"end_offset" : 5,
"type" : "",
"position" : 0
},
{
"token" : "s",
"start_offset" : 6,
"end_offset" : 7,
"type" : "",
"position" : 1
},
{
"token" : "weath",
"start_offset" : 8,
"end_offset" : 13,
"type" : "",
"position" : 2
},
{
"token" : "er",
"start_offset" : 13,
"end_offset" : 15,
"type" : "",
"position" : 3
},
{
"token" : "beaut",
"start_offset" : 19,
"end_offset" : 24,
"type" : "",
"position" : 5
},
{
"token" : "iful",
"start_offset" : 24,
"end_offset" : 28,
"type" : "",
"position" : 6
}
]
}
各种分析器的列表及其描述如下表所示 -
| 序列号 | 分析仪及描述 |
|---|---|
| 1 |
标准分析仪(标准) 可以为此分析器设置停用词和 max_token_length 设置。默认情况下,停用词列表为空,max_token_length 为 255。 |
| 2 |
简单分析仪(简单) 该分析器由小写分词器组成。 |
| 3 |
空白分析器(空白) 该分析器由空白分词器组成。 |
| 4 |
停止分析仪(停止) 可以配置 stopwords 和 stopwords_path。默认情况下,停用词初始化为英文停用词,stopwords_path 包含带有停用词的文本文件的路径。 |
分词器
分词器用于从 Elasticsearch 中的文本生成标记。通过考虑空格或其他标点符号,可以将文本分解为标记。Elasticsearch 有大量内置分词器,可在自定义分析器中使用。
分词器的一个示例,每当遇到非字母的字符时,它会将文本分解为术语,但它也会将所有术语小写,如下所示 -
POST _analyze
{
"tokenizer": "lowercase",
"text": "It Was a Beautiful Weather 5 Days ago."
}
运行上面的代码,我们得到如下所示的响应 -
{
"tokens" : [
{
"token" : "it",
"start_offset" : 0,
"end_offset" : 2,
"type" : "word",
"position" : 0
},
{
"token" : "was",
"start_offset" : 3,
"end_offset" : 6,
"type" : "word",
"position" : 1
},
{
"token" : "a",
"start_offset" : 7,
"end_offset" : 8,
"type" : "word",
"position" : 2
},
{
"token" : "beautiful",
"start_offset" : 9,
"end_offset" : 18,
"type" : "word",
"position" : 3
},
{
"token" : "weather",
"start_offset" : 19,
"end_offset" : 26,
"type" : "word",
"position" : 4
},
{
"token" : "days",
"start_offset" : 29,
"end_offset" : 33,
"type" : "word",
"position" : 5
},
{
"token" : "ago",
"start_offset" : 34,
"end_offset" : 37,
"type" : "word",
"position" : 6
}
]
}
分词器列表及其描述如下表所示 -
| 序列号 | 分词器和描述 |
|---|---|
| 1 |
标准分词器(标准) 这是基于基于语法的分词器构建的,并且可以为此分词器配置 max_token_length 。 |
| 2 |
边缘 NGram 分词器 (edgeNGram) 可以为此分词器设置 min_gram、max_gram、token_chars 等设置。 |
| 3 |
关键词分词器(关键词) 这会生成整个输入作为输出,并且可以为此设置 buffer_size。 |
| 4 |
字母分词器(字母) 这会捕获整个单词,直到遇到非字母为止。 |