热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

2.JavaAPI操作elasticsearch

新建Maven工程添加依赖:org.elasticsearchela

新建Maven工程

添加依赖:



org.elasticsearch
elasticsearch
7.8.0



org.elasticsearch.client
elasticsearch-rest-high-level-client
7.8.0



org.apache.logging.log4j
log4j-api
2.8.2


org.apache.logging.log4j
log4j-core
2.8.2


com.fasterxml.jackson.core
jackson-databind
2.9.9



junit
junit
4.12

客户端连接:

public class ESTest_Client {
public static void main(String[] args) throws IOException {
// 创建客户端对象,高级别客户端,你需要连接服务器,需要知道ip端口号,访问方式
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
//业务逻辑
System.out.println(client);
// 关闭客户端连接
client.close();
}
}


4.2 索引操作


4.2.1 创建索引

public static void main(String[] args) throws IOException {
// 创建客户端对象,高级别客户端,你需要连接服务器,需要知道ip端口号,访问方式
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
//业务逻辑
System.out.println(client);
// 创建索引,先拿到索引,然后创建,第一个参数是请求对象,后面是选项
CreateIndexRequest request = new CreateIndexRequest("user");//索引名称
// 发完请求,就会有响应
CreateIndexResponse respOnse= client.indices().create(request, RequestOptions.DEFAULT);// 默认请求配置
// 响应的状态
boolean acknowledged = response.isAcknowledged();
//输出true
System.out.println(acknowledged);
// 关闭客户端连接
client.close();
}

查询所有索引发现索引创建成功!

 


4.2.2 索引查询

public class ESTest_Index_Search {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 查询索引
GetIndexRequest request = new GetIndexRequest("user");
GetIndexResponse respOnse= client.indices().get(request, RequestOptions.DEFAULT);
// 别名操作
System.out.println(response.getAliases());
// 结构
System.out.println(response.getMappings());

client.close();
}
}

输出:

{user=[]}
{user=org.elasticsearch.cluster.metadata.MappingMetadata@e2704661}


4.2.3 索引删除

public class DeleteIndex {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
// 删除索引 - 请求对象
DeleteIndexRequest request = new DeleteIndexRequest("user");
// 发送请求,获取响应
AcknowledgedResponse respOnse= client.indices().delete(request,RequestOptions.DEFAULT);
// 操作结果
System.out.println("操作结果 : " + response.isAcknowledged());
client.close();
}
}

 输出true:

 


4.3 文档操作


4.3.1 新建实体类

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private String sex;
private Integer age;
}

4.3.2 插入数据

public class ESTest_Doc_Insert {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));

// 插入数据,这里需要的是index
IndexRequest indexRequest = new IndexRequest();
// 索引名字,索引的id
indexRequest.index("user").id("1001");
User user = new User();
user.setName("zhangsan");
user.setAge(30);
user.setSex("男");
// 向ES插入数据,必须将数据转换为json格式
ObjectMapper mapper = new ObjectMapper();
String value = mapper.writeValueAsString(user);
// 放到请求体中
indexRequest.source(value, XContentType.JSON);
IndexResponse respOnse= client.index(indexRequest, RequestOptions.DEFAULT);
//输出:CREATED
System.out.println(response.getResult());
client.close();
}
}

postman获取文档,发现其已经创建成功!

 


4.3.3 修改数据

public class ESTest_Doc_Update {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
//修改数据
UpdateRequest request=new UpdateRequest();
request.index("user").id("1001");
request.doc(XContentType.JSON,"sex","女");
UpdateResponse respOnse= client.update(request, RequestOptions.DEFAULT);
//输出:UPDATED
System.out.println(response.getResult());
client.close();
}
}

 


4.3.4 查询数据

/**
* 查询数据
*/
public class ESTest_Doc_Query {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
GetRequest request=new GetRequest().index("user").id("1001");
GetResponse respOnse= client.get(request, RequestOptions.DEFAULT);
//打印信息
System.out.println("_index:" + response.getIndex());
System.out.println("_type:" + response.getType());
System.out.println("_id:" + response.getId());
System.out.println("source:" + response.getSourceAsString());
}
}

输出:

_index:user
_type:_doc
_id:1001
source:{"name":"zhangsan","sex":"女","age":30}


4.3.5 数据删除

/**
* 删除数据
*/
public class ESTest_Doc_Delete {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
//创建请求对象
DeleteRequest request = new DeleteRequest().index("user").id("1001");
//客户端发送请求,获取响应对象
DeleteResponse respOnse= client.delete(request, RequestOptions.DEFAULT);
//打印信息
System.out.println(response.toString());
}
}

输出:

DeleteResponse[index=user,type=_doc,id=1001,version=3,result=deleted,shards=ShardInfo{total=2, successful=1, failures=[]}]



推荐阅读
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 如何用UE4制作2D游戏文档——计算篇
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了如何用UE4制作2D游戏文档——计算篇相关的知识,希望对你有一定的参考价值。 ... [详细]
  • 使用在线工具jsonschema2pojo根据json生成java对象
    本文介绍了使用在线工具jsonschema2pojo根据json生成java对象的方法。通过该工具,用户只需将json字符串复制到输入框中,即可自动将其转换成java对象。该工具还能解析列表式的json数据,并将嵌套在内层的对象也解析出来。本文以请求github的api为例,展示了使用该工具的步骤和效果。 ... [详细]
  • Voicewo在线语音识别转换jQuery插件的特点和示例
    本文介绍了一款名为Voicewo的在线语音识别转换jQuery插件,该插件具有快速、架构、风格、扩展和兼容等特点,适合在互联网应用中使用。同时还提供了一个快速示例供开发人员参考。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Google Play推出全新的应用内评价API,帮助开发者获取更多优质用户反馈。用户每天在Google Play上发表数百万条评论,这有助于开发者了解用户喜好和改进需求。开发者可以选择在适当的时间请求用户撰写评论,以获得全面而有用的反馈。全新应用内评价功能让用户无需返回应用详情页面即可发表评论,提升用户体验。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • ElasticSerach初探第一篇认识ES+环境搭建+简单MySQL数据同步+SpringBoot整合ES
    一、认识ElasticSearch是一个基于Lucene的开源搜索引擎,通过简单的RESTfulAPI来隐藏Lucene的复杂性。全文搜索,分析系统&# ... [详细]
  • ElasticSearch成功安装完毕。 测试数据添加出现{  error:{    root_cause ... [详细]
  • Windows简单部署Exceptionless
    部署准备Elasticsearch、Exceptionless.API、Exceptionless.UI、URLRewrite、.NET运行时 1、安装ElasticSearch1 ... [详细]
  • elasticssearch+kibanna入门(撰写中)
    elasticssearchkibanna入门(撰写中)看到一篇elasticssearchkibanna的文章,觉得很好, ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 如何在服务器主机上实现文件共享的方法和工具
    本文介绍了在服务器主机上实现文件共享的方法和工具,包括Linux主机和Windows主机的文件传输方式,Web运维和FTP/SFTP客户端运维两种方式,以及使用WinSCP工具将文件上传至Linux云服务器的操作方法。此外,还介绍了在迁移过程中需要安装迁移Agent并输入目的端服务器所在华为云的AK/SK,以及主机迁移服务会收集的源端服务器信息。 ... [详细]
author-avatar
yuliu预留
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有