java es mysql_java操作ES整个流程介绍

一 准备工作

下载elasticsearch-6.7.0和展示ES数据的界面工具elasticsearch-head-master,本人电脑已安装IntelliJ IDEA 2019.3 x64的java工具。

简单点,我们直接按默认配置学习。

windows直接点击elasticsearch.bat启动ES。

620f61a7c59c6e2ada82a10eb8332b89.png图1

使用npm run start启动elasticsearch-head-master,然后就可以从界面上查看ES数据。

c988366baceac26f7787755b7649fd04.png图2

b46137ef2f82f151cc955ff0e0921f8c.png图3

f7c1de932a7d88f710341c2df6baa13f.png图4

二 java操作入库ES数据

1、引入elasticsearch包

io.searchbox

jest

${searchbox.version}

org.elasticsearch

elasticsearch

${elasticsearch.version}

2、将库表里的数据导入部分到ES上

public void testSave() throws IOException {

//对象 ArticleHomeDto是要存入ES里的字段设置

ArticleHomeDto dto = new ArticleHomeDto();

dto.setSize(50);

dto.setTag("__all__");

//从mysql数据库里查出要存入ES的内容

List apArticles = apArticleMapper.loadArticleListByLocation(dto, (short)0);

//开始循环入库到ES

for (ApArticle apArticle : apArticles) {

ApArticleContent apArticleContent = apArticleContentMapper.selectByArticleId(apArticle.getId());

EsIndexEntity esIndexEntity = new EsIndexEntity();

esIndexEntity.setChannelId(new Long(apArticle.getChannelId()));

esIndexEntity.setId(apArticle.getId().longValue());

esIndexEntity.setContent(ZipUtils.gunzip(apArticleContent.getContent()));

esIndexEntity.setPublishTime(apArticle.getPublishTime());

esIndexEntity.setStatus(new Long(1));

esIndexEntity.setTag("article");

esIndexEntity.setTitle(apArticle.getTitle());

Index.Builder builder = new Index.Builder(esIndexEntity);

builder.id(apArticle.getId().toString());

builder.refresh(true);

Index index = builder.index(ESIndexConstants.ARTICLE_INDEX).type(ESIndexConstants.DEFAULT_DOC).build();

JestResult result = jestClient.execute(index);

if (result != null && !result.isSucceeded()) {

throw new RuntimeException(result.getErrorMessage() + "插入更新索引失败!");

}

最终入库到ES内容如下:

5a2775df4d29d1df909d5c2c5686b125.png图5

三 ES上查看操作数据

1、基本查询:查询某个字段是否包含某内容,match

eabce7bf6fefcea79ba7a0f1b8e37e97.png图6

2、复合查询:查询某条数据

1cfa79de8f92e93cd28f1b7dd13d8326.png图7

3、复合查询:查询包含关键信息的数据,查询包含“极客头条”的数据

9f07d64fc8d5260ce3f5b68f68254dae.png图8

4、复合查询:查询包含关键信息的数据,只要满足一条即可查出来

2e7c31820e0cd604c44514ded9ba3511.png图9

5、复合查询:查询包含关键信息的数据,must和should混合使用

7921c39622a76e57d601dae7890b78c2.png图10

四总结

本节简单介绍了windows环境下如何安装ES,如何用java导入数据到ES,并且在ES界面上查询ES里的数据,同样我们也可以在ES上操作数据,用put命令实现增删改操作。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值