QQ个性网:专注于分享免费的QQ个性内容

关于我们| 网站公告| 广告服务| 联系我们| 网站地图

搜索
编程 JavaScript Java C++ Python SQL C Io ML COBOL Racket APL OCaml ABC Sed Bash Visual Basic Modula-2 Logo Delphi IDL Groovy Julia REXX Chapel X10 Forth Eiffel C# Go Rust PHP Swift Kotlin R Dart Perl Ruby TypeScript MATLAB Shell Lua Scala Objective-C F# Haskell Elixir Lisp Prolog Ada Fortran Erlang Scheme Smalltalk ABAP D ActionScript Tcl AWK IDL J PostScript IDL PL/SQL PowerShell

java操作ES整个流程介绍

日期: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......

一 准备工作

下载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包

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内容如下:

图5

三 ES上查看操作数据

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

图6

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

图7

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

图8

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

图9

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

图10

四总结

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

关于我们|网站公告|广告服务|联系我们| 网站地图

Copyright © 2002-2023 某某QQ个性网 版权所有 | 备案号:粤ICP备xxxxxxxx号

声明: 本站非腾讯QQ官方网站 所有软件和文章来自互联网 如有异议 请与本站联系 本站为非赢利性网站 不接受任何赞助和广告