转自:https://blog.csdn.net/u013089490/article/details/84323886
ElasticsearchTemplate是Spring对ES的java api进行的封装,主要用来对索引的创建、删除等操作。同时ElasticsearchTemplate也是对一种补充。

1、ElasticsearchTemplate源码分析

  1. ElasticsearchTemplate类实现了ElasticsearchOperations接口和ApplicationContextAware接口;ApplicationContextAware接口是让bean能够获取到Spring容器。
  1. public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware

1.1、ElasticsearchOperations接口

ElasticsearchOperations接口部分代码截图如下:
Spring Data Elasticsearch篇(2):ElasticsearchTemplate索引操作 - 图1
ElasticsearchOperations接口中常用方法如下:
(1)createIndex()方法:创建索引,返回值布尔类型;
(2)putMapping()方法:创建Mapping映射,返回值布尔类型;
(3)getMapping()方法:得到Mapping映射,返回值是一个Map;
(4)deleteIndex()方法:删除索引,返回值布尔类型。

1.2、ElasticSearchTemplate源码


2、应用

2.1、基本配置

1、application.yml配置

  1. spring:
  2. data:
  3. elasticsearch:
  4. cluster-name: elasticsearch #es集群名称
  5. cluster-nodes: 192.168.2.10:9300 #es节点信息

2、pom文件

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-test</artifactId>
  9. <scope>test</scope>
  10. </dependency>
  11. <!-- lang3包-->
  12. <dependency>
  13. <groupId>org.apache.commons</groupId>
  14. <artifactId>commons-lang3</artifactId>
  15. <version>3.7</version>
  16. </dependency>
  17. <!-- lombok-->
  18. <dependency>
  19. <groupId>org.projectlombok</groupId>
  20. <artifactId>lombok</artifactId>
  21. <version>1.16.22</version>
  22. </dependency>
  23. </dependencies>

2.2、配置实体类

  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. }

2.3、创建索引

【java创建索引代码】

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest(classes=EsdemoApplication.class)
  3. public class IndexTest {
  4. @Autowired
  5. private ElasticsearchTemplate esTemplate;
  6. /**
  7. * 创建索引
  8. */
  9. @Test
  10. public void createIndex(){
  11. // 创建索引,会根据Item类的@Document注解信息来创建
  12. esTemplate.createIndex(Item.class);
  13. // 配置映射,会根据Item类中的@Id、@Field等字段来自动完成映射
  14. esTemplate.putMapping(Item.class);
  15. }
  16. }

【查询返回结果】

  1. {
  2. "item": {
  3. "aliases": {},
  4. "mappings": {
  5. "docs": {
  6. "properties": {
  7. "brand": {
  8. "type": "keyword"
  9. },
  10. "category": {
  11. "type": "keyword"
  12. },
  13. "images": {
  14. "type": "keyword",
  15. "index": false
  16. },
  17. "price": {
  18. "type": "double"
  19. },
  20. "title": {
  21. "type": "text",
  22. "analyzer": "ik_max_word"
  23. }
  24. }
  25. }
  26. },
  27. "settings": {
  28. "index": {
  29. "refresh_interval": "1s",
  30. "number_of_shards": "1",
  31. "provided_name": "item",
  32. "creation_date": "1542786915167",
  33. "store": {
  34. "type": "fs"
  35. },
  36. "number_of_replicas": "0",
  37. "uuid": "KyJpv2q8RYWjfh4HxnPZ_A",
  38. "version": {
  39. "created": "6020499"
  40. }
  41. }
  42. }
  43. }
  44. }

2.4、删除索引

【java删除索引代码】

  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest(classes=EsdemoApplication.class)
  3. public class IndexTest {
  4. @Autowired
  5. private ElasticsearchTemplate esTemplate;
  6. /**
  7. * 删除索引
  8. * 因为在Item实体类上indexName = "item"定义索引名称为item
  9. */
  10. @Test
  11. public void deleteIndex(){
  12. //删除索引
  13. boolean result = esTemplate.deleteIndex("item");
  14. }
  15. }