大数据|Elasticsearch-mapper 基于注解方式生成mapping(2.0以上)
Elasticsearch生成mapping的方式上有多种方式,我们可以把mapping做成配置文件,也可以用spring-data-elasticsearch基于注解生成。
在基于注解生成这种方式上spring-data的注解还是不错的,但是如果想深度定制化一些参数spring-data却是不支持的,比如针对分词的string类型字段的fielddata加载设置。
又如果项目中不想引入spring但又想使用基于注解方式生成mapping,这时spring-data就不行了,这里有另一种选择:elasticsearch-mapper
【大数据|Elasticsearch-mapper 基于注解方式生成mapping(2.0以上)】git 地址:http://git.oschina.net/music_code_m/elasticsearch-mapper
elasticsearch-mapper支持绝大部分数据类型和相关参数的设置,使用是请参考官网对各种数据类型和相关参数:ES2.x官网mapping设置
下面是使用示例:
@Document(type = "book", _timestamp = true, _ttl = @TTL(enabled = true, _default = "5m"))
public class Book {
/*ID,只能是Long或者String类型*/
@Id
private Long id;
/*数值类型*/
@Field(type = FieldType.Double, ignoreMalformed = true)
private Double price;
/*数值类型*/
@Field(type = FieldType.Integer)
private Integer pageCount;
/*未分词String型*/
@Field(type = FieldType.String, index = FieldIndex.not_analyzed)
private String isnNo;
/*bool型*/
@Field(type = FieldType.Boolean, nullValue = "https://www.it610.com/article/false")
private Boolean isValid;
/*日期类型*/
@Field(type = FieldType.Date, format = DateFormat.basic_time_no_millis)
private Date publishDate;
/*分词String类型,并设置fielddata加载限制(当然也可不设置用默认)*/
@Field(
type = FieldType.String,
index = FieldIndex.analyzed,
analyzer = "ik_max_word",
searchAnalyzer = "ik_smart",
termVector = TermVector.with_positions_offsets,
fielddata = https://www.it610.com/article/@Fielddata(
format = FielddataFormat.paged_bytes,
frequency = @FielddataFrequencyFilter(
enable = true,
min = 0.001,
max = 1.2,
minSegmentSize = 500
),
loading = FielddataLoading.eager_global_ordinals
))
private String author;
/*multi field 类型(用于多字段搜索)*/
@MultiField(
mainField = @Field(type = FieldType.String, index = FieldIndex.analyzed, analyzer ="ik_max_word", searchAnalyzer = "ik_smart"),
otherFields = {
@MultiNestedField(dotSuffix = "pinyin", nestedField = @Field(
type = FieldType.String,
index = FieldIndex.analyzed,
analyzer = "lc_index",
searchAnalyzer = "lc_search")
),
@MultiNestedField(dotSuffix = "english", nestedField = @Field(
type = FieldType.String,
index = FieldIndex.analyzed,
analyzer = "standard")
)
}
)
private String title;
/*Completion Context Suggester配置(如果不配置CompletionContext则是Completion Suggester)*/
@CompletionField(analyzer = "ik", payloads = true, context = {
@CompletionContext(name = "bookType", type = CompletionContextType.category, defaultVal = {"algorithm"}),
@CompletionContext(name = "bookColor", type = CompletionContextType.category, defaultVal = {"red"})
})
private String suggestContextField;
/*二进制类型*/
@Field(type = FieldType.Binary)
private byte[] pdf;
/*内嵌类型*/
@NestedObject(clazz = SalesArea.class)
private SalesArea salesArea;
}
public class SalesArea {
/*未分词String*/
@Field(type = FieldType.String, index = FieldIndex.not_analyzed)
private String localtionName;
/*分词String且禁用fielddata*/
@Field(
type = FieldType.String,
index = FieldIndex.analyzed,
analyzer = "ik_max_word",
fielddata = https://www.it610.com/article/@Fielddata(format = FielddataFormat.disabled)
)
private String description;
/*数值型*/
@Field(type = FieldType.Integer)
private int openDays;
}
转载于:https://www.cnblogs.com/chennanlcy/p/6591786.html
推荐阅读
- 我要做大厨
- Docker应用:容器间通信与Mariadb数据库主从复制
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量
- 知识
- 三十年后的广场舞大爷
- 奔向你的城市
- 村里的故事|村里的故事 --赵大头
- 期刊|期刊 | 国内核心期刊之(北大核心)
- 华为旁!大社区、地铁新盘,佳兆业城市广场五期!
- 汇讲-勇于突破