ElasticsearchTemplate是Spring对ES的java api进行的封装,主要用来对索引的创建、删除等操作。同时ElasticsearchTemplate也是对一种补充。
1、ElasticsearchTemplate源码分析
ElasticsearchTemplate类实现了ElasticsearchOperations接口和ApplicationContextAware接口;ApplicationContextAware接口是让bean能够获取到Spring容器。
public class ElasticsearchTemplate implements ElasticsearchOperations, ApplicationContextAware
1.1、ElasticsearchOperations接口
ElasticsearchOperations接口部分代码截图如下:
ElasticsearchOperations接口中常用方法如下:
(1)createIndex()方法:创建索引,返回值布尔类型;
(2)putMapping()方法:创建Mapping映射,返回值布尔类型;
(3)getMapping()方法:得到Mapping映射,返回值是一个Map;
(4)deleteIndex()方法:删除索引,返回值布尔类型。
1.2、ElasticSearchTemplate源码
2、应用
2.1、基本配置
1、application.yml配置
spring:
data:
elasticsearch:
cluster-name: elasticsearch #es集群名称
cluster-nodes: 192.168.2.10:9300 #es节点信息
2、pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- lang3包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<!-- lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.22</version>
</dependency>
</dependencies>
2.2、配置实体类
@Document(indexName = "item",type = "docs",shards = 1,replicas = 0)
public class Item {
@Id
private Long id;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String title;
@Field(type=FieldType.Keyword)
private String category;
@Field(type=FieldType.Keyword)
private String brand;
@Field(type=FieldType.Double)
private Double price;
@Field(index = false,type = FieldType.Keyword)
private String images;
}
2.3、创建索引
【java创建索引代码】
@RunWith(SpringRunner.class)
@SpringBootTest(classes=EsdemoApplication.class)
public class IndexTest {
@Autowired
private ElasticsearchTemplate esTemplate;
/**
* 创建索引
*/
@Test
public void createIndex(){
// 创建索引,会根据Item类的@Document注解信息来创建
esTemplate.createIndex(Item.class);
// 配置映射,会根据Item类中的@Id、@Field等字段来自动完成映射
esTemplate.putMapping(Item.class);
}
}
【查询返回结果】
{
"item": {
"aliases": {},
"mappings": {
"docs": {
"properties": {
"brand": {
"type": "keyword"
},
"category": {
"type": "keyword"
},
"images": {
"type": "keyword",
"index": false
},
"price": {
"type": "double"
},
"title": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
},
"settings": {
"index": {
"refresh_interval": "1s",
"number_of_shards": "1",
"provided_name": "item",
"creation_date": "1542786915167",
"store": {
"type": "fs"
},
"number_of_replicas": "0",
"uuid": "KyJpv2q8RYWjfh4HxnPZ_A",
"version": {
"created": "6020499"
}
}
}
}
}
【删除索引】
@RunWith(SpringRunner.class)
@SpringBootTest(classes=EsdemoApplication.class)
public class IndexTest {
@Autowired
private ElasticsearchTemplate esTemplate;
/**
* 删除索引
* 因为在Item实体类上indexName = "item"定义索引名称为item
*/
@Test
public void deleteIndex(){
//删除索引
boolean result = esTemplate.deleteIndex("item");
}
}