1 Star 0 Fork 1

半眸微凉丶 / elasticsearch

forked from 周阿菜 / elasticsearch 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

elasticsearch

没有把公司自定义的异常拉过来 抛出的都是RuntimeException 可以自己修改抛出的异常

1、首先需要按照spring-data-elasticseach的方式创建实体类

字段没加@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();

}

2、查询示例

    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();

3、创建示例

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);

4、更新示例

esToolkitFactory.lambdaUpdate(Item.class)
                .term(Item::getId, 2)
                .set(Item::getTitle, "大宝")
                .update();

5、删除示例

// 按照id删除
esToolkitFactory.lambdaUpdate(Item.class)
                .id(1L)
                .id(2L)
                .delete();

//按照查询删除
esToolkitFactory.lambdaUpdate(Item.class)
                .term(Item::getPrice,2)
                .delete();

6、索引示例

// 创建索引
esToolkitFactory.indexOperate(Item.class)
                .createIndexIfExists();
// 删除索引
esToolkitFactory.indexOperate(Item.class)
                .deleteIndex();

7、更新索引(存在数据)

// 创建新索引
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");

8、也可以继承EsServiceImpl来直接获得es相关能力

8.1 如果实体类中存在索引名称

例如@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();
    }
}

8.2 如果索引需要加上环境

实体类上没有@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();
    }
}

8.3 如果每次调用都是新的索引名

如 需要在索引中配置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;
    }
}

空文件

简介

对spring data elasticsearch简单的一个封装 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/bmweiliang/elasticsearch.git
git@gitee.com:bmweiliang/elasticsearch.git
bmweiliang
elasticsearch
elasticsearch
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891