这部分文档主要包含:html
- 倒排索引
- Analyzer分词
作个类比,看书时,咱们看到了哪一个章节,根据章节标题去目录中检索具体的内容。可是当咱们回忆起一些只言片语,一些句子,一些情节时,去定位它出现的章节和页码,就便相似于倒排索引了。正则表达式
在es中,正排索引就是文档id和文档内容的关联,而倒排索引就是文档内容和文档id的关联。json
下面是三个文档bash
文档id | 内容 |
---|---|
1 | study elasticsearch |
2 | learn elasticsearch |
3 | elasticsearch method |
通过倒排索引后elasticsearch
Term(单词) | Count | DocumentID:Position |
---|---|---|
elasticsearch | 3 | 1:1,2:4,3:0 |
study | 1 | 1:0 |
learn | 1 | 2:0 |
method | 1 | 3:1 |
(Term,一段文本通过分析后会输出一串单词,一个一个的叫作Term)ide
以上就是一个简单的正排索引和倒排索引的例子post
一个例子-以单词 elasticsearch
为例性能
文档id | 内容 |
---|---|
1 | study elasticsearch |
2 | learn elasticsearch |
3 | elasticsearch method |
elasticsearch的Posting List
以下测试
Doc Id | TF | Position | Offset |
---|---|---|---|
1 | 1 | 1 | <6,19> |
2 | 1 | 1 | <6,19> |
3 | 1 | 0 | <0,13> |
Analysis
(文本分析) - 是把全文的转换为一系列单词(term/token)的过程,也叫分词Analysis
是经过 Analyzer
来实现的
input:
Elasticsearch Server
output:
elasticsearch
server
# 仔细看会发现,通过分词器处理后,Elasticsearch被转换成了elasticsearch,大写字母自动转换成了小写
复制代码
GET /_analyze
{
"analyzer": "standard",
"text":"Mastering Elasticsearch,elasticsearch in Action"
}
复制代码
POST my_index/_analyze
{
"field": "title",
"text": "Mastering Elasticsearch"
}
复制代码
POST /_analyze
{
"tokenizer": "standard",
"filter": ["lowercase"],
"text": "Mastering Elastricsearch"
}
复制代码
名称 | 做用 |
---|---|
Standard Analyzer | 默认分词器,按词切分,小写处理 |
Simple Analyzer | 按照非字母切分(符号被过滤),小写处理 |
Stop Analyzer | 小写处理,停用词过滤(a/the/is) |
Whitespace Analyzer | 按照空格切分,不转小写 |
Keyword Analyzer | 部分词,直接将输入当作输出 |
Patter Analyzer | 正则表达式,默认\W+(非字符分割) |
Language | 提供了30多种常见语言的分词器 |
Customer Analyzer | 自定义分词器 |
\W+
,非字符的符号进行分隔一组分析特定语言文本的分析器,支持如下类型:
arabic
,armenian
,basque
,bengali
,brazilian
,bulgarian
,catalan
,cjk
,czech
,danish
,dutch
,english
,finnish
,french
,galician
,german
,greek
,hindi
,hungarian
,indonesian
,irish
,italian
,latvian
,lithuanian
,norwegian
,persian
,portuguese
,romanian
,russian
,sorani
,spanish
,swedish
,turkish
,thai
.ui
GET _analyze
{
"analyzer": "icu_analyzer",
"text": "这事的确肯定不下来"
}
复制代码
分词结果
{
"tokens" : [
{
"token" : "这",
"start_offset" : 0,
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0
},
{
"token" : "事",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "的确",
"start_offset" : 2,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "肯定",
"start_offset" : 4,
"end_offset" : 6,
"type" : "<IDEOGRAPHIC>",
"position" : 3
},
{
"token" : "不下来",
"start_offset" : 6,
"end_offset" : 9,
"type" : "<IDEOGRAPHIC>",
"position" : 4
}
]
}
复制代码
本篇中接触了es的倒排索引结构和各类analyzer分词器,并基本介绍了各分词器的基本使用,较为简单。
以为不错,请点个赞吧