admin管理员组文章数量:1663678
Elasticsearch
一、概述
1、Elasticsearch是什么?
Elaticsearch简称为ES,是一个开源的可扩展的分布式的全文检索引擎,它可以近乎实时的存储、检索数据。本身扩展性很好,可扩展到上百台服务器,处理PB级别的数据。ES使用Java开发并使用Lucene作为其核心来实现索引和搜索的功能,但是它通过简单的RestfulAPI和javaAPI来隐藏Lucene的复杂性,从而让全文搜索变得简单。
2、Elasticsearch和Mysql的对比
二、Elasticsearch倒排索引
Term(单词):一段文本经过分析器分析以后就会输出一串单词,这一个一个的就叫做Term(直译为:单词)
Term Dictionary(单词字典):顾名思义,它里面维护的是Term,可以理解为Term的集合
Term Index(单词索引):为了更快的找到某个单词,我们为单词建立索引
Posting List(倒排列表):倒排列表记录了出现过某个单词的所有文档的文档列表及单词在该文档中出现的位置信息,每条记录称为一个倒排项(Posting)。
什么是倒排索引?
在倒排索引中,通过Term索引可以找到Term在Term Dictionary中的位置,进而找到Posting List,有了倒排列表就可以根据ID找到文档了。实际上是Term(单词)到文档id的映射关系
三、分词器(analyzer)
1Elasticsearch默认提供的常见分词器
要切分的语句:Set the shape to semi-transparent by calling set_trans(5)
standard analyzer - 是Elasticsearch中的默认分词器。标准分词器,处理英语语法的分词器。切分后的key_words:set, the, shape, to, semi, transparent, by, calling, set_trans, 5。这种分词器也是Elasticsearch中默认的分词器。切分过程中不会忽略停止词(如:the、a、an等)。会进行单词的大小写转换、过滤连接符(-)或括号等常见符号。
GET _analyze
{
"text": "Set the shape to semi-transparent by calling set_trans(5)",
"analyzer": "standard"
}
simple analyzer - 简单分词器。切分后的key_words:set, the, shape, to, semi, transparent, by, calling, set, trans。就是将数据切分成一个个的单词。使用较少,经常会破坏英语语法。
GET _analyze
{
"text": "Set the shape to semi-transparent by calling set_trans(5)",
"analyzer": "simple"
}
whitespace analyzer - 空白符分词器。切分后的key_words:Set, the, shape, to, semi-transparent, by, calling, set_trans(5)。就是根据空白符号切分数据。如:空格、制表符等。使用较少,经常会破坏英语语法。
GET _analyze
{
"text": "Set the shape to semi-transparent by calling set_trans(5)",
"analyzer": "whitespace"
}
language analyzer - 语言分词器,如英语分词器(english)等。切分后的key_words:set, shape, semi, transpar, call, set_tran, 5。根据英语语法分词,会忽略停止词、转换大小写、单复数转换、时态转换等,应用分词器分词功能类似standard analyzer。
GET _analyze
{
"text": "Set the shape to semi-transparent by calling set_trans(5)",
"analyzer": "english"
}
Elasticsearch中提供的常用分词器都是英语相关的分词器,对中文的分词都是一字一词。所以可以安装中文分词器插件IK分词器
2中文分词器(IK分词器)
IK分词器提供了两种analyzer,分别是ik_max_word(常用)和ik_smart。
ik_max_word: 会将文本做最细粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,中华人民,中华,华人,人民共和国,人民,人,民,共和国,共和,国,国歌”,会穷尽各种可能的组合;
ik_smart: 会做最粗粒度的拆分,比如会将“中华人民共和国国歌”拆分为“中华人民共和国,国歌”。
四、Elasticsearch中的mapping问题
Mapping在Elasticsearch中是非常重要的一个概念。决定了一个index中的field使用什么数据格式存储,使用什么分词器解析,是否有子字段等。
为什么要学习Mapping?
如果没有mapping所有text类型属性默认都使用standard分词器。所以如果希望使用IK分词就必须配置自定义mapping。
1mapping核心数据类型
Elasticsearch中的数据类型有很多,在这里只介绍常用的数据类型。
只有text类型才能被分词。其他类型不允许。
文本(字符串):text
整数:byte、short、integer、long
浮点型:float、double
布尔类型:boolean
日期类型:date
数组类型:array {a:[]}
对象类型:object {a:{}}
不分词的字符串(关键字): keyword
2dynamic mapping对字段的类型分配
true or false -> boolean
123 -> long
123.123 -> double
2018-01-01 -> date
hello world -> text
[] -> array
{} -> object
在上述的自动mapping字段类型分配的时候,只有text类型的字段需要分词器。默认分词器是standard分词器。
3 创建索引时指定mapping
语法:
PUT 索引名称
{
"mappings":{
"类型名称":{
"properties":{
"字段名":{
"type":类型,
["analyzer":字段的分词器,]
["fields":{
"子字段名称":{
"type":类型,
"ignore_above":长度限制
}
}]
}
}
}
}
}
如:
PUT /test_index
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
},
"mappings": {
"test_type":{
"properties": {
"author_id" : {
"type": "byte",
"index": false
},
"title" : {
"type": "text",
"analyzer": "ik_max_word",
"fields": {
"keyword" : {
"type": "keyword",
"ignore_above": 256
}
}
},
"content" : {
"type": "text",
"analyzer": "ik_max_word"
},
"post_date" : {
"type": "date"
}
}
}
}
}
“index” - 是否可以作为搜索索引。可选值:true | false
“analyzer” - 指定分词器。
“type” - 指定字段类型
3查看索引mapping
可以通过命令查看已有index的mapping具体信息,语法如下:
GET 索引名/_mapping
五、Search 搜索
1查询所有数据
GET 索引名/_search
{
"query" : { "match_all" : {} }
}
2 match search
全文检索。要求查询条件拆分后的任意词条与具体数据匹配就算搜索结果。
GET 索引名/_search
{
"query": {
"match": {
"字段名": "搜索条件"
}
}
}
3 phrase search
短语检索。要求查询条件必须和具体数据完全匹配才算搜索结果。其特征是:
1.对搜索条件进行拆词
2.把拆词当作一个整体,整体去索引(索引是存储内容被拆词后的结果)中匹配,必须严格匹配(存储内容拆词后是:比如北京,大兴,朝阳,条件拆词是:北京,朝阳。这种情况是不能被查询的,因为北京和朝阳之前还有大兴。)才能查询到
GET 索引名/_search
{
"query": {
"match_phrase": {
"字段名": "搜索条件"
}
}
}
4 range
范围比较搜索
GET 索引名/类型名/_search
{
"query" : {
"range" : {
"字段名" : {
"gt" : 搜索条件1,
"lte" : 搜索条件2
}
}
}
}
5 排序
GET 索引名/类型名/_search
{
"query": {
[搜索条件]
},
"sort": [
{
"字段名1": {
"order": "asc"
}
},
{
"字段名2": {
"order": "desc"
}
}
]
}
6 分页
DSL分页也是使用from和size实现的。
GET 索引名称/_search
{
"query":{
"match_all":{}
},
"from": 起始下标,
"size": 查询记录数
}
六、常见注意点
1、Elasticsearch返回数据条数问题
表现情况:total数返回不正确,当超过某个数值(10000)之后一直是这个数
解决方案:1.评估数据量,通过配置修改total的限制。
2.代码层面加上searchSourceBuilder.trackTotalHits(true) 返回真实命中数。
2、Elasticsearch准实时的问题
问题描述:前端提交数据,立马跳入结果页,发现数据获取不到,刷新之后才能出数据。
问题原因:
ES数据写入后,默认1s后才会被搜索到(refresh_interval为1)
解决方案:
1.代码层面强制刷新
UpdateRequest updateRequest = new UpdateRequest(esIndex, id.toString());
updateRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
2.修改配置 refresh_interval
本文标签: 海量搜索引擎Elasticsearch
版权声明:本文标题:Elasticsearch海量搜索引擎 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1730007083a1218920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论