转自:https://blog.csdn.net/u013089490/article/details/84323762

1、注解@Document

1.1、@Document源码

  1. @Persistent
  2. @Inherited
  3. @Retention(RetentionPolicy.RUNTIME)
  4. @Target({ElementType.TYPE})
  5. public @interface Document {
  6. //索引库名称
  7. String indexName();
  8. //类型
  9. String type() default "";
  10. //
  11. boolean useServerConfiguration() default false;
  12. //默认分片数5
  13. short shards() default 5;
  14. //默认副本数1
  15. short replicas() default 1;
  16. //刷新间隔
  17. String refreshInterval() default "1s";
  18. //索引文件存储类型
  19. String indexStoreType() default "fs";
  20. //是否创建索引
  21. boolean createIndex() default true;
  22. }

1.2、@Document注解使用

@Document注解作用在类上,标记实体类为文档对象,常用属性如下:
(1)indexName:对应索引库名称;
(2)type:对应在索引库中的类型;
(3)shards:分片数
(4)replicas:副本数;

2、注解@Field

2.1、@Field源码

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @Target({ElementType.FIELD})
  3. @Documented
  4. @Inherited
  5. public @interface Field {
  6. //自动检测属性类型
  7. FieldType type() default FieldType.Auto;
  8. //
  9. boolean index() default true;
  10. //时间类型的字段格式化
  11. DateFormat format() default DateFormat.none;
  12. //
  13. String pattern() default "";
  14. //默认情况下不存储
  15. boolean store() default false;
  16. //
  17. boolean fielddata() default false;
  18. //指定字段使用搜索时的分词
  19. String searchAnalyzer() default "";
  20. //
  21. String analyzer() default "";
  22. //如果某个字段需要被忽略
  23. String[] ignoreFields() default {};
  24. //
  25. boolean includeInParent() default false;
  26. }

2.2、枚举类FieldType

【FieldType源码】

  1. public enum FieldType {
  2. Text,
  3. Integer,
  4. Long,
  5. Date,
  6. Float,
  7. Double,
  8. Boolean,
  9. Object,
  10. Auto,
  11. Nested,
  12. Ip,
  13. Attachment,
  14. Keyword;
  15. private FieldType() {
  16. }
  17. }

2.3、@Field注解使用

@Field作用在成员变量,标记为文档的字段,并制定映射属性;
(1)@Id:作用在成员变量,标记一个字段为id主键;一般id字段或是域不需要存储也不需要分词;
(2)type:字段的类型,取值是枚举,FieldType;
(3)index:是否索引,布尔值类型,默认是true;
(4)store:是否存储,布尔值类型,默认值是false;
(5)analyzer:分词器名称
@Field(type = FieldType.Keyword)和 @Field(type = FieldType.Text)区别
在早期elasticsearch5.x之前的版本存储字符串只有string字段;但是在elasticsearch5.x之后的版本存储了Keyword和Text,都是存储字符串的。FieldType.Keyword存储字符串数据时,不会建立索引;而FieldType.Text在存储字符串数据的时候,会自动建立索引,也会占用部分空间资源。
【 @Field(store = true)】
其实不管我们将store值设置为true或false,elasticsearch都会将该字段存储到Field域中;但是他们的区别是什么?
(1)store = false时,默认设置;那么给字段只存储在”_source”的Field域中;
(2)store = true时,该字段的value会存储在一个跟_source平级的独立Field域中;同时也会存储在_source中,所以有两份拷贝。
那么我们在什么样的业务场景下使用store field功能?
(1)_source field在索引的mapping 中disable了。这种情况下,如果不将某个field定义成store=true,那些将无法在返回的查询结果中看到这个field。
(2)_source的内容非常大。这时候如果我们想要在返回的_source document中解释出某个field的值的话,开销会很大(当然你也可以定义source filtering将减少network overhead),比例某个document中保存的是一本书,所以document中可能有这些field: title, date, content。假如我们只是想查询书的title 跟date信息,而不需要解释整个_source(非常大),这个时候我们可以考虑将title, date这些field设置成store=true。
需要注意的是,看起来将field store可以减少查询的开销,但其实这样也会加大disk的访问频率。假如你将_source中的10个field都定义store,那么在你查询这些field的时候会将会有10次disk seek的操作。而返回_source只有一次disk seek的操作。所以这个也是我们在定义的时候需要blance的。

3、实体类代码

  1. @Document(indexName = "item",type = "docs",shards = 1,replicas = 0)
  2. public class Item {
  3. @Id
  4. private Long id;
  5. @Field(type = FieldType.Text,analyzer = "ik_max_word")
  6. private String title;
  7. @Field(type=FieldType.Keyword)
  8. private String category;
  9. @Field(type=FieldType.Keyword)
  10. private String brand;
  11. @Field(type=FieldType.Double)
  12. private Double price;
  13. @Field(index = false,type = FieldType.Keyword)
  14. private String images;
  15. }

【注意】(1)@Field(index=true)表示是否索引,如果是索引表示该字段(或者叫域)能能够搜索。
(2)@Field(analyzer=”ik_max_word”,searchAnalyzer=”ik_max_word”)表示是否分词,如果是分词就会按照分词的单词搜索,如果不是分词就按照整体搜索。
(3)@Field(store=true)是否存储,也就是页面上显示。