赞
踩
import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; @Document(indexName = "stu", type = "_doc") @Data public class Stu { private Long stuId; @Field(store = true) private String name; @Field(store = true) private Integer age; }
@Autowired
private ElasticsearchTemplate esTemplate;
@Test
public void createIndexStu() {
Stu stu = new Stu();
stu.setStuId(1001L);
stu.setName("bat man");
stu.setAge(18);
IndexQuery indexQuery = new IndexQueryBuilder().withObject(stu).build();
esTemplate.index(indexQuery);
}
访问Elasticsearch管理页面可以看到索引和数据都成功生成!
@Test
public void deleteIndexStu() {
esTemplate.deleteIndex(Stu.class);
}
执行一下,可以看到node上的索引已被删除!
不建议使用 ElasticsearchTemplate 对索引进行管理(创建索引,更新映射,删除索引)
索引就像是数据库或者数据库中的表,我们平时是不会是通过java代码频繁的去创建修改删除数据库或者表的,我们只会针对数据做CRUD的操作。
在es中也是同理,我们尽量使用 ElasticsearchTemplate 对文档数据做CRUD的操作。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。