作者:狮子也疯狂
专栏:《es》
坚持做好每一步,幸运之神自然会驾凌在你的身上
一、🐇前言
大家好!我是狮子,在上几篇文章中,详细介绍了在kibana中操作es,现在我们来看看真实开发中,如何使用Java操作es。
二、🐇原生JAVA操作ES_搭建项目
咱们先来了解一下,原生Java操作ES api接口,但是原生Java操作es的索引和文档太繁琐,我们只需要稍作了解。
Ⅰ、创建项目
我们先来创建一个普通Maven项目,并且引入以下依赖。
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.17.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-resthigh-level-client</artifactId>
<version>7.17.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
Ⅱ、索引操作
在原生Java操作es索引的时候,连接es的对象为
RestHighLevelClient
,其实就五步操作:
1.创建客户端对象,连接ES
2.创建请求对象
3.发送请求
4.操作响应结果
5.关闭客户端
2.1 创建空索引
// 创建空索引
@Test
public void createIndex() throws IOException{
// 1.创建客户端对象,连接ES
RestHighLevelClient client = new RestHighLevelClient(RestClient
.builder(new HttpHost("192.168.0.187", 9200, "http")));
// 2.创建请求对象
CreateIndexRequest request = new CreateIndexRequest("student");
// 3.发送请求
CreateIndexResponse response = client.indices()
.create(request,RequestOptions.DEFAULT);
// 4.操作响应结果
System.out.println(response.index());
// 5.关闭客户端
client.close();
}
tips:如果项目初次访问es失败,需要添加外部访问配置
打开Elasticsearch安装路径下config目录下的elasticsearch.yml文件,加入如下配置:
discovery.seed_hosts: ["host1"]
network.host: 0.0.0.0
重新启动ES即可。
2.2 给索引添加结构
@Test
public void mappingIndex() throws IOException {
// 1.创建客户端对象,连接ES
RestHighLevelClient client = new RestHighLevelClient(RestClient
.builder(new HttpHost("192.168.0.187", 9200, "http")));
// 2.创建请求对象
PutMappingRequest request = new PutMappingRequest("student");
request.source("{\n" +
" \"properties\":{\n" +
" \"id\":{\n" +
" \"type\":\"integer\"\n" +
" },\n" +
" \"name\":{\n" +
" \"type\":\"text\"\n" +
" },\n" +
" \"age\":{\n" +
" \"type\":\"integer\"\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON);
// 3.发送请求
AcknowledgedResponse response = client.indices()
.putMapping(request,RequestOptions.DEFAULT);
// 4.操作响应结果
System.out.println(response.isAcknowledged());
// 5.关闭客户端
client.close();
}
2.3 删除索引
// 删除索引
@Test
public void deleteIndex() throws IOException{
// 1.创建客户端对象,连接ES
RestHighLevelClient client = new RestHighLevelClient(RestClient
.builder(new HttpHost("192.168.0.187", 9200, "http")));
// 2.创建请求对象
DeleteIndexRequest request = new DeleteIndexRequest("student");
// 3.发送请求
AcknowledgedResponse response = client.indices()
.delete(request,RequestOptions.DEFAULT);
// 4.操作响应结果
System.out.println(response.isAcknowledged(
));
// 5.关闭客户端
client.close();
}
Ⅲ、文档操作
在进行文档操作时,还是老套路,先创建客户端对象,连接es(和前面一样),但是请求对象创建不同,如果是新增或者修改文档,则请求
IndexRequest
对象,如果是根据id查询,则请求GetRequest
,如果是删除,则创建DeleteRequest
对象。
3.1 新增&修改文档
@Test
public void addDocument() throws IOException{
// 1.创建客户端对象,连接ES
RestHighLevelClient client = new RestHighLevelClient(RestClient
.builder(new HttpHost("192.168.0.187", 9200, "http")));
// 2.创建请求对象
IndexRequest request = new IndexRequest("student").id("1");
request.source(XContentFactory.jsonBuilder()
.startObject()
.field("id", 1)
.field("name", "i love conding")
.field("age", 20)
.endObject());
// 3.发送请求
IndexResponse response = client.index(request,RequestOptions.DEFAULT);
// 4.操作响应结果
System.out.println(response.status());
// 5.关闭客户端
client.close();
}
3.2 根据id查询文档
// 根据id查询文档
@Test
public void findByIdDocument() throws IOException {
// 1.创建客户端对象,连接ES
RestHighLevelClient client = new RestHighLevelClient(RestClient
.builder(new HttpHost("192.168.0.187", 9200, "http")));
// 2.创建请求对象
GetRequest request = new GetRequest("student", "1");
// 3.发送请求
GetResponse response = client.get(request, RequestOptions.DEFAULT);
// 4.操作响应结果
System.out.println(response.getSourceAsString());
// 5.关闭客户端
client.close();
}
3.3 删除文档
// 删除文档
@Test
public void DeleteDocument() throws IOException {
// 1.创建客户端对象,连接ES
RestHighLevelClient client = new RestHighLevelClient(RestClient
.builder(new HttpHost("192.168.0.187", 9200, "http")));
// 2.创建请求对象
DeleteRequest request = new DeleteRequest("student", "1");
// 3.发送请求
DeleteResponse response = client.delete(request,RequestOptions.DEFAULT);
// 4.操作响应结果
System.out.println(response.status());
// 5.关闭客户端
client.close();
}
Ⅳ、搜索操作
搜索操作是es的最主要的操作,刚开始的两步和前面一样,但是创建请求对象时,创建的是
SearchRequest
。使用不同的方法来操作索引。文章来源:https://www.toymoban.com/news/detail-414064.html
4.1 搜索所有文档
@Test
public void queryAllDocument() throws IOException {
// 创建客户端对象,链接ES
RestHighLevelClient client = new RestHighLevelClient(RestClient
.builder(new HttpHost("192.168.1.58",9200,"http")));
// 创建搜索条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
// 创建请求对象
SearchRequest request = new SearchRequest("student").source(searchSourceBuilder);
// 发送请求
SearchResponse response = client.search(request,RequestOptions.DEFAULT);
// 输出返回结果
for (SearchHit hit:response.getHits()) {
System.out.println(hit.getSourceAsString());
}
// 关闭客户端
client.close();
}
4.2 根据关键词搜索文档
@Test
public void queryTermDocument() throws IOException {
// 创建客户端对象,链接ES
RestHighLevelClient client = new RestHighLevelClient(RestClient
.builder(new HttpHost("192.168.1.58",9200,"http")));
// 创建请求条件
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.termQuery("info","boy"));
// 创建请求对象
SearchRequest request = new SearchRequest("student")
.source(searchSourceBuilder);
// 发送请求
SearchResponse response = client.search(request,RequestOptions.DEFAULT);
// 输出返回结果
for (SearchHit hit:response.getHits()) {
System.out.println(hit.getSourceAsString());
}
// 关闭客户端
client.close();
}
三、🐇总结
这个部分不难理解,就是搞清楚几个对象之前的作用,准确调用方法就好,咱们只需要了解以下用法,需要使用的时候,再查资料就行。但是真实开发中,一般都是使用框架封装好的对象(为咱么省略了好多繁琐步骤。)文章来源地址https://www.toymoban.com/news/detail-414064.html
到了这里,关于Elasticsearch连续剧之实战篇Java操作es的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!