代码拉取完成,页面将自动刷新
字段没加@Field会把字段名转换成_分隔
/**
* @author zhouchj
* @date 2023/7/18
*/
@Data
@Document(indexName = "test-document-item")
public class Item implements Serializable {
/**
* id
*/
@Id
@Field(name = "title", type = FieldType.Keyword)
private Long id;
/**
* 标题
*/
@Field(name = "title", type = FieldType.Keyword)
private String title;
/**
* 分类
*/
@Field(name = "category", type = FieldType.Keyword)
private String category;
/**
* 品牌
*/
@Field(name = "brand", type = FieldType.Keyword)
private String brand;
/**
* 价格
*/
@Field(name = "price", type = FieldType.Double)
private Double price;
/**
* 图片地址
*/
@Field(name = "images", type = FieldType.Keyword)
private String images;
/**
* 更新时间
*/
@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
@Field(name = "update_time", type = FieldType.Date)
private Date updateTime = new Date();
}
QueryPageEntity<Item> page = esToolkitFactory.lambdaQuery(Item.class)
.term(Item::getId, Arrays.asList(1, 2))
.agg(Item::getPrice)
.withSort(Sort.Direction.DESC, Item::getPrice)
.withPage(new PageForm())
.page();
Item item = new Item();
item.setId(2L);
item.setTitle("怡宝");
item.setCategory("矿泉水");
item.setBrand("农夫山泉饮料有限公司");
item.setPrice(2.0D);
item.setImages("假装这个是个图片");
item.setUpdateTime(new Date());
esToolkitFactory.lambdaUpdate(Item.class)
.save(item);
esToolkitFactory.lambdaUpdate(Item.class)
.term(Item::getId, 2)
.set(Item::getTitle, "大宝")
.update();
// 按照id删除
esToolkitFactory.lambdaUpdate(Item.class)
.id(1L)
.id(2L)
.delete();
//按照查询删除
esToolkitFactory.lambdaUpdate(Item.class)
.term(Item::getPrice,2)
.delete();
// 创建索引
esToolkitFactory.indexOperate(Item.class)
.createIndexIfExists();
// 删除索引
esToolkitFactory.indexOperate(Item.class)
.deleteIndex();
// 创建新索引
EsIndexOperate<ItemNew> newIndexOperate=esToolkitFactory.indexOperate("test-document-item-v1",ItemNew.class);
newIndexOperate.createIfExists();
// 查询老索引中的数据
List<Item> list=esToolkitFactory.lambdaQuery("test-document-item",Item.class)
.list();
// 老索引数据实体转换成新索引数据实体
List<ItemNew> newList=list.stream().map(r->{
ItemNew itemNew=new ItemNew();
itemNew.setId(r.getId());
itemNew.setTitle(r.getTitle());
itemNew.setCategory(r.getCategory());
itemNew.setBrand(r.getBrand());
itemNew.setPrice(r.getPrice());
itemNew.setImages(r.getImages());
itemNew.setUpdateTime(r.getUpdateTime());
return itemNew;
}).collect(Collectors.toList());
// 保存数据到新索引
esToolkitFactory.lambdaUpdate("test-document-item-v1",ItemNew.class)
.saveBatch(newList);
// 删除老索引
esToolkitFactory.indexOperate("test-document-item",Item.class)
.deleteIndex();
// 新索引别名设置为老索引名称
newIndexOperate.addAlias("test-document-item");
例如@Document(indexName = "test-document-item") 则什么都不用设置
/**
* @author zhouchj
* @date 2023/9/4
*/
@Service
public class ItemServiceImpl extends EsServiceImpl<Item> implements ItemService {
@Override
public List<Item> list() {
return this.lambdaQuery()
.list();
}
}
实体类上没有@Document注解
可以再构造方法中调用index方法
/**
* @author zhouchj
* @date 2023/9/4
*/
@Service
public class ItemServiceImpl extends EsServiceImpl<Item> implements ItemService {
public ItemServiceImpl() {
index("test-document-item");
}
@Override
public List<Item> list() {
return this.lambdaQuery()
.list();
}
}
如 需要在索引中配置id
可以调用lambdaQuery(String indexName)方法
/**
* @author zhouchj
* @date 2023/9/4
*/
@Service
public class ItemServiceImpl extends EsServiceImpl<Item> implements ItemService {
@Override
public List<Item> list() {
return this.lambdaQuery(getIndexName())
.list();
}
private String getIndexName() {
return "test-document-item-" + 1L;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。