3.1-simple

优质
小牛编辑
127浏览
2023-12-01

插入

// 单条插入
User user=new User();
// 省略entity属性赋值... ...
elasticsearchTemplate.save(user);
// 批量插入
LinkedList<User> users= new LinkedList<User>();
// 省略entity属性赋值... ...
elasticsearchTemplate.save(users);
~

删除

// 使用主键删除
elasticsearchTemplate.delete(User.class, "tnL2ZWoBNfLC2FGsSCEb");
User user=new User();
user.setId("tnL2ZWoBNfLC2FGsSCEb");
elasticsearchTemplate.delete(user);
elasticsearchTemplate.delete(String indexName, String type, Serializable id)
// 使用主键批量删除
LinkedList  ids= new LinkedList();
ids.add(1);
ids.add(2);
elasticsearchTemplate.deleteByIds(User.class,ids);

修改

User user=new User();
user.setId("tnL2ZWoBNfLC2FGsSCEb");
elasticsearchTemplate.update(user);
elasticsearchTemplate.update("tnL2ZWoBNfLC2FGsSCEb",User.class);
//批量修改
LinkedList<User>  users= new LinkedList();
elasticsearchTemplate.update(users);

获取

User user=new User();
user.setId("tnL2ZWoBNfLC2FGsSCEb");
elasticsearchTemplate.get(user);
elasticsearchTemplate.get("tnL2ZWoBNfLC2FGsSCEb",User.class);

查询

// 列表查询
List<User> users =  elasticsearchTemplate.findAll(User.class)
// 排序Sort
List<User> users =  elasticsearchTemplate.findAll(User.class, Sort sort)
// 分页
elasticsearchTemplate.findByPage(Class<T> clazz,Page page);