# Elasticsearch面向对象封装之注解分析
## 前情提要
前一段时间封装过关系型数据库,发现出奇的好用,最近想尝试对Elasticsearch做一个面向对象封装。与ES这种文档存储系统不同,多数关系型数据库类型属性单一、结果扁平。而ES存储的文档结构立体,层次通常深度大于1,还包含了索引、评分系统的的建立,因而他的文档数据收集确显得比较复杂。但是幸好ES给了一系列默认的配置,即使我们不定义任何结构也能实现普通关系型数据库那样的存储和查询功能,因为json格式的数据天生就包含了存储结构,但是要凸显ES的性能和扩展开发,需要对存储结构做一个更精细的定义及收集,特别是如果要支持自动建立索引这种功能,对Mapping的支持要详细而且完整。前篇Sqlite的封装已经说过,自定义注解是悄无声息收集存储结构数据的完美姿势,当然无注解也会有默认配置,下面我根据通用套路去定义一些注解。
## 实体类注解
### @EsDoc({...})
1. 定义 Index、Type,为空的话,实体类中元字段 ```_index```、```_type```要定义
2. 扩展定义 动态Index生成规则、动态Type生成规则
### @EsDynamicMapping({...})
1. 动态定义Mapping属性模板
2. 主要属性
- 动态参数
* ```date_detection```:```true/false```,日期检测
* ```dynamic_date_formats```:日期格式定义,例如 ```["MM/dd/yyyy"]```
* ```numeric_detection```:```true/false```,数字检测
- 动态模板
* ```dynamic_templates```,存json对象数组或json对象数组对应的文件路径。例如:
```java
[
{
"named_analyzers": {
"match_mapping_type": "string",
"match": "*",
"mapping": {
"type": "text",
"analyzer": "{name}"
}
}
},
{
"no_doc_values": {
"match_mapping_type":"*",
"mapping": {
"type": "{dynamic_type}",
"doc_values": false
}
}
}
]
```
### @EsMappingFile(...)
1. 定义 value ,Mapping的json文件路径,自定义Mapping方式之一
2. 直接读取Mapping结构构建实体类对应的doc存储结构
3. 适用于复制ES库已有的映射结构导入 , 简化开发
## 实体类属性注解
### @EsFieldsJson(...)
1. 定义 value , Mapping 中对应 每个property 的 定义 Json 字符串,自定义Mapping方式之一
2. 适用于专业ES开发人士,可以对每个doc节点精确定义属性,达到更好的效果
### @EsFields({...})
1. 自定义Mapping方式之一,粒度细化定义
2. 定义 type , 默认 string
- 简单域类型
* 字符串: ```string```, ```text```, ```keyword```
* 整数 : ```byte```, ```short```, ```integer```, ```long```, ```alf_float```,```scaled_float```
* 浮点数: ```float```, ```double```
* 布尔型: ```boolean```
* 日期: ```date```
- 复杂域类型
* 数组:```array```, 普通数据数组
* 对象:```object```, json格式对象
* 对象数组:```nested```, json对象数组
- 其他数据类型
* 经纬度点类型:```geo_point```
* 地理形状类型:```geo_shape```
* IP数据类型:```ip```, 用于IPv4和IPv6地址
* 完成数据类型:```completion``` 提供自动完成的建议
* 令牌计数数据类型:```token_count``` 计算字符串中的令牌数量
* mapper-murmur3:```murmur3``` 在索引时计算值的哈希值并将它们存储在索引中
* 渗滤器类型:```percolator```, 接受来自```query-dsl```的查询
* [```join``` 数据类型](https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html):```join```, 为相同索引内的文档定义父/子关系
3. 额外属性定义
- ```index``` ,定义索引类型 , 空则不定义,默认 ```analyzed```
* ```analyzed``` 首先分析字符串,然后索引它。换句话说,以全文索引这个域。```@字符串类型专用```
* ```not_analyzed``` 索引这个域,所以它能够被搜索,但索引的是精确值。不会对它进行分析。```@字符串类型专用```
* ```no``` 不索引这个域。这个域不会被搜索到。```@字符串类型专用```
* ```true/false``` 是否编入索引
- ```analyzer``` ,分析器语言类型 , 空则不定义,默认 ```standard```
* 内含的还有```whitespace``` 、 ```simple``` 、 ```english```
* 自定义请参考API
- ```format``` , 一般用于日期用字符串格式存储,定义日期格式
- ```normalizer``` , 空则不定义,跟随elasticsearch默认值
- ```boost``` , 相关性查询权重设置,默认```1.0```
- ```coerce``` , 设置为```true```强制将数字型数据转化为对应类型,而避免出现字符串或定义为整形文档存储为浮点型
- ```copy_to``` ,定义的组字段名, 将多个字段的值复制到组字段中,然后可以将其作为单个字段进行查询
- ```doc_values``` , 默认```true```,false则查询的结果集中没有该字段
- ```dynamic``` , 默认```true```,多用于```object```类型字段
* ```true```:新检测到的字段被添加到映射中。(默认)
* ```false```:新检测的字段被忽略。这些字段不会被编入索引,因此不会被搜索到,但仍会出现在```_source```返回的匹配字段中。这些字段不会添加到映射中,必须明确添加新字段。
* ```strict```:如果检测到新字段,则抛出异常并拒绝该文档。必须将新字段明确添加到映射中。
- ```enabled``` , 默认```true```,用于开启关闭某些字段的索引建立,```false```则不做查询字段
- ```eager_global_ordinals``` ,提高聚合查询速度,聚合字段可以设置为```true```
- ```fielddata``` , 空则不定义,跟随elasticsearch默认值
- ```ignore_above``` , 属性值为整数,针对字符类型数据,长度比设定值长的将不被索引和存储
- ```ignore_malformed``` , 定义为```true```的话,就算类型不对也不影响存储,默认为```false```
- ```index_options``` , 被编入索引的项目定义
* ```docs```:只有文档编号被索引。
* ```freqs```:文件编号和术语频率被编入索引。
* ```positions```:文档编号,术语频率和术语位置(或顺序)被编入索引。
* ```offsets```:文档编号,术语频率,位置以及开始和结束字符偏移(将术语映射回原始字符串)进行索引。
- ```fields``` , 多领域定义,详情查看API
- ```norms``` , 用于开启或禁用规范,用于查询评分,详情见API
- ```null_value``` , 当传入```null```时候,用该属性的值替代```null```存储
- ```position_increment_gap``` , 值为数字,定义匹配间隙,越大越模糊
- ```search_analyzer``` , 定义搜索时的分析器,用于覆盖默认建立索引的分析器,通常不设定
- ```similarity``` , 定义评分算法
* ```BM25```:```Okapi BM25```算法。请参阅[]可插入相似性算法。](https://www.elastic.co/guide/en/elasticsearch/guide/master/pluggable-similarites.html)
* ```classic```:```TF/IDF```算法。请参阅[Lucene的实用评分函数](https://www.elastic.co/guide/en/elasticsearch/guide/master/practical-scoring-function.html)。
* ```boolean```:一种简单的布尔相似性,在不需要全文排名时使用,分数只应基于查询条件是否匹配而使用。布尔相似度给出了一个等于他们的查询提升的分数。
- ```store``` ,```true/false``` 通常查询是对编入的索引进行查询,不必从```_source```字段中提取这些字段可以设置为 ```false```。
- ```term_vector``` , 定义术语,用于特定文档搜索
没有合适的资源?快使用搜索试试~ 我知道了~
结构化数据存储系统的面向对象封装.zip
共715个文件
js:199个
md:137个
java:121个
需积分: 5 0 下载量 142 浏览量
2024-01-05
22:33:42
上传
评论
收藏 4.57MB ZIP 举报
温馨提示
结构化数据存储系统的面向对象封装
资源推荐
资源详情
资源评论
收起资源包目录
结构化数据存储系统的面向对象封装.zip (715个子文件)
database-oop-mds2index.html.bat 3KB
elasticsearch-oop-mds2index.html.bat 2KB
postgresql-oop-mds2index.html.bat 2KB
sqlserver-oop-mds2index.html.bat 2KB
arangodb-oop-mds2index.html.bat 2KB
mongodb-oop-mds2index.html.bat 2KB
oracle-oop-mds2index.html.bat 2KB
access-oop-mds2index.html.bat 2KB
sqlite-oop-mds2index.html.bat 2KB
redis-oop-mds2index.html.bat 2KB
mysql-oop-mds2index.html.bat 2KB
db2-oop-mds2index.html.bat 2KB
xml-oop-mds2index.html.bat 2KB
.bithoundrc 1KB
categories_list 723B
CNAME 20B
config 33B
bootstrap.min.css 141KB
editor_ie7.css 37KB
editor_iequirks.css 36KB
editor_ie8.css 36KB
editor_ie.css 35KB
editor_gecko.css 34KB
editor.css 34KB
skin.min.css 31KB
skin.ie7.min.css 31KB
skin.classic.min.css 30KB
default.css 19KB
dialog_ie7.css 17KB
dialog_ie8.css 17KB
dialog_iequirks.css 16KB
dialog_ie.css 16KB
dialog_opera.css 16KB
dialog.css 15KB
jquery.fancybox.css 5KB
jquery.fancybox-buttons.css 2KB
contents.css 2KB
content.min.css 1KB
toolbar.css 1KB
content.inline.min.css 1022B
jquery.fancybox-thumbs.css 735B
test.db 8KB
test.db 8KB
ubuntu-c-webfont.eot 37KB
junge-regular-webfont.eot 26KB
bitter-regular-webfont.eot 25KB
tinymce-small.eot 8KB
tinymce.eot 8KB
Gemfile 257B
fancybox_loading@2x.gif 14KB
fancybox_loading.gif 6KB
loader.gif 5KB
loader.gif 3KB
object.gif 152B
anchor.gif 53B
blank.gif 43B
trans.gif 43B
spacer.gif 43B
.gitignore 651B
index.html 14KB
index.html 14KB
index.html 14KB
index.html 14KB
index.html 14KB
index.html 14KB
index.html 14KB
index.html 14KB
index.html 14KB
index.html 14KB
index.html 14KB
index.html 13KB
index.html 13KB
index.html 13KB
index.html 13KB
index.html 13KB
index.html 13KB
index.html 13KB
index.html 13KB
index.html 13KB
index.html 13KB
index.html 13KB
index.html 13KB
scripts.html 7KB
directory.html 6KB
post.html 5KB
guide.html 4KB
home.html 2KB
analytics.html 2KB
header.html 2KB
scripts.html 2KB
footer.html 1KB
disqus.html 1KB
blog.html 1KB
overlay.html 1KB
page.html 911B
index.html 779B
libraries.html 752B
index.html 729B
gitment.html 648B
meta.html 626B
共 715 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8
资源评论
张呱呱_
- 粉丝: 1107
- 资源: 135
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功