@SpringBootTest
@RunWith(SpringRunner.class)
public class ItemTest {
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Autowired
private ItemRepository itemRepository;
/**
* Class对象创建索引
*/
@Test
public void testDemo() {
this.elasticsearchTemplate.createIndex(Item.class); //创建索引,根据@Document注解创建
this.elasticsearchTemplate.putMapping(Item.class);
//映射索引,根据@id和@field来映射
}
/**
* String形式创建索引库
*/
@Test
public void testDemo1() {
Settings setting = Settings.builder().put("number_of_replicas", 0).put("number_of_shards", 5).build();
this.elasticsearchTemplate.createIndex("love", setting);
}
/**
* 测试删除索引库,
* [Class<T>] /[String indexName]
*/
@Test
public void deleteDemo() {
// this.elasticsearchTemplate.deleteIndex(Item.class);
this.elasticsearchTemplate.deleteIndex("love");
}
/**
* 使用Repository来新增文档
*/
@Test
public void repositoryAddIndexName() {
Item item = new Item();
item.setId(88888L);
item.setBrand("小米");
item.setCategory("手机");
item.setTitle("小米手机");
item.setImages("http://image.leyou.com/123456.jpg");
item.setPrice(1999d);
itemRepository.save(item);
}
@Test
public void repositoryAddListIndexName() {
ArrayList<Item> list = new ArrayList<>();
list.add(new Item(2L, "坚果手机R1", " 手机", "锤子", 3699.00, "http://image.leyou.com/123.jpg"));
list.add(new Item(3L, "华为META10", " 手机", "华为", 4499.00, "http://image.l
ElasticSearch基本操作
最新推荐文章于 2024-06-16 11:36:07 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)