Elasticsearch生成mapping的方式上有多种方式,我们可以把mapping做成配置文件,也可以用spring-data-elasticsearch基于注解生成。
在基于注解生成这种方式上spring-data的注解还是不错的,但是如果想深度定制化一些参数spring-data却是不支持的,比如针对分词的string类型字段的fielddata加载设置。
又如果项目中不想引入spring但又想使用基于注解方式生成mapping,这时spring-data就不行了,这里有另一种选择:elasticsearch-mapper
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 = "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 = @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 = @Fielddata(format = FielddataFormat.disabled)
)
private String description;
/*数值型*/
@Field(type = FieldType.Integer)
private int openDays;
}