日期:2025/04/05 09:06来源:未知 人气:51
一 准备工作
下载elasticsearch-6.7.0和展示ES数据的界面工具elasticsearch-head-master,本人电脑已安装IntelliJ IDEA 2019.3 x64的java工具。
简单点,我们直接按默认配置学习。
windows直接点击elasticsearch.bat启动ES。
图1
使用npm run start启动elasticsearch-head-master,然后就可以从界面上查看ES数据。
图2
图3
图4
二 java操作入库ES数据
1、引入elasticsearch包
2、将库表里的数据导入部分到ES上
public void testSave() throws IOException {
//对象 ArticleHomeDto是要存入ES里的字段设置
ArticleHomeDto dto = new ArticleHomeDto();
dto.setSize(50);
dto.setTag("all");
//从mysql数据库里查出要存入ES的内容
List
//开始循环入库到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内容如下:
图5
三 ES上查看操作数据
1、基本查询:查询某个字段是否包含某内容,match
图6
2、复合查询:查询某条数据
图7
3、复合查询:查询包含关键信息的数据,查询包含“极客头条”的数据
图8
4、复合查询:查询包含关键信息的数据,只要满足一条即可查出来
图9
5、复合查询:查询包含关键信息的数据,must和should混合使用
图10
四总结
本节简单介绍了windows环境下如何安装ES,如何用java导入数据到ES,并且在ES界面上查询ES里的数据,同样我们也可以在ES上操作数据,用put命令实现增删改操作。