索引的操作
Template索引操作
創(chuàng)建索引和映射
創(chuàng)建索引
ElasticsearchTemplate中提供了創(chuàng)建索引的API:
可以根據(jù)類的信息自動(dòng)生成,也可以手動(dòng)指定indexName和Settings
映射
映射相關(guān)的API:
可以根據(jù)類的字節(jié)碼信息(注解配置)來生成映射,或者手動(dòng)編寫映射
?
我們這里采用類的字節(jié)碼信息創(chuàng)建索引并映射:
@RunWith(SpringRunner.class) @SpringBootTest(classes = ItcastElasticsearchApplication.class) public class IndexTest {@Autowiredprivate ElasticsearchTemplate elasticsearchTemplate;@Testpublic void testCreate(){// 創(chuàng)建索引,會(huì)根據(jù)Item類的@Document注解信息來創(chuàng)建elasticsearchTemplate.createIndex(Item.class);// 配置映射,會(huì)根據(jù)Item類中的id、Field等字段來自動(dòng)完成映射elasticsearchTemplate.putMapping(Item.class);} }結(jié)果:
GET /item {"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": "1525405022589","store": {"type": "fs"},"number_of_replicas": "0","uuid": "4sE9SAw3Sqq1aAPz5F6OEg","version": {"created": "6020499"}}}} }刪除索引
刪除索引的API:
可以根據(jù)類名或索引名刪除。
示例:
@Test public void deleteIndex() {elasticsearchTemplate.deleteIndex("heima"); }?
總結(jié)
- 上一篇: 搭建elasticsearch测试工程
- 下一篇: 新增和更新