首页 > 编程语言 >【Elasticsearch】- 使用JavaAPI操作elasticsearch

【Elasticsearch】- 使用JavaAPI操作elasticsearch

时间:2022-12-06 10:31:23浏览次数:51  
标签:JavaAPI request new elasticsearch esClient Elasticsearch RestHighLevelClient 客户端


文章目录

  • ​​工程准备​​
  • ​​索引操作​​
  • ​​创建索引​​
  • ​​查询索引​​
  • ​​删除索引​​
  • ​​文档操作​​
  • ​​添加文档数据​​
  • ​​批量添加文档数据​​
  • ​​修改文档数据​​
  • ​​根据id查询文档数据​​
  • ​​查询当前索引所有文档数据​​
  • ​​分页查询​​
  • ​​模糊查询​​
  • ​​删除文档数据​​

工程准备

创建maven工程并引入Elasticsearch相关依赖:

<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.8.0</version>
</dependency>
<!-- elasticsearch 的客户端 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
<!-- elasticsearch 依赖 2.x 的 log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<!-- junit 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>

索引操作

创建索引

@Test
public void index_create_test() throws IOException {
//创建Elasticsearch客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);

//创建索引
CreateIndexRequest request = new CreateIndexRequest("student");
CreateIndexResponse createIndexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);

boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("响应状态:"+acknowledged);

//关闭Elasticsearch客户端
esClient.close();
}

使用Postman测试索引是否成功:

【Elasticsearch】- 使用JavaAPI操作elasticsearch_Elastic

查询索引

@Test
public void index_get_test() throws IOException {
//创建Elasticsearch客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);

//查询索引
GetIndexRequest getIndexRequest = new GetIndexRequest("student");
GetIndexResponse getIndexResponse = esClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);

System.out.println(getIndexResponse.getAliases());
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getSettings());

//关闭Elasticsearch客户端
esClient.close();
}

【Elasticsearch】- 使用JavaAPI操作elasticsearch_elasticsearch_02

删除索引

@Test
public void index_delete_test() throws IOException {
//创建Elasticsearch客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);

//删除索引
DeleteIndexRequest getIndexRequest = new DeleteIndexRequest("student");
AcknowledgedResponse delete = esClient.indices().delete(getIndexRequest, RequestOptions.DEFAULT);

System.out.println(delete.isAcknowledged());

//关闭Elasticsearch客户端
esClient.close();
}

文档操作

进行文档操作前,先创建文档数据对应的java实体类Student:

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Student {
private String name;
private int age;
private String sex;
private String level;
}

添加文档数据

@Test
public void doc_insert_test() throws IOException {
//创建Elasticsearch客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);

IndexRequest request = new IndexRequest();
request.index("student").id("10000");

Student student = new Student();
student.setName("王同学");
student.setAge(22);
student.setSex("男");
student.setLevel("研究生");

ObjectMapper mapper = new ObjectMapper();
String studentJson = mapper.writeValueAsString(student);

request.source(studentJson, XContentType.JSON);
IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());

//关闭Elasticsearch客户端
esClient.close();
}

批量添加文档数据

@Test
public void doc_insert_batch_test() throws IOException {
//创建Elasticsearch客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);

BulkRequest request = new BulkRequest();

request.add(new IndexRequest().index("student").id("10001").source(XContentType.JSON, "name", "张三"));
request.add(new IndexRequest().index("student").id("10002").source(XContentType.JSON, "name", "李四"));
request.add(new IndexRequest().index("student").id("10003").source(XContentType.JSON, "name", "王五"));

BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getItems());

//关闭Elasticsearch客户端
esClient.close();
}

【Elasticsearch】- 使用JavaAPI操作elasticsearch_java_03

修改文档数据

@Test
public void doc_update_test() throws IOException {
//创建Elasticsearch客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);

UpdateRequest request = new UpdateRequest();
request.index("student").id("10000");
request.doc(XContentType.JSON,"sex","女");

UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());

//关闭Elasticsearch客户端
esClient.close();
}

根据id查询文档数据

@Test
public void doc_search_test() throws IOException {
//创建Elasticsearch客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);

GetRequest request = new GetRequest();
request.index("student").id("10000");
GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());

//关闭Elasticsearch客户端
esClient.close();
}

【Elasticsearch】- 使用JavaAPI操作elasticsearch_Elastic_04

查询当前索引所有文档数据

@Test
public void doc_search_all_test() throws IOException {
//创建Elasticsearch客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);

SearchRequest request = new SearchRequest();
request.indices("student");

request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));

SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

SearchHits hits = response.getHits();
System.out.println(hits.getTotalHits());
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}

//关闭Elasticsearch客户端
esClient.close();
}

【Elasticsearch】- 使用JavaAPI操作elasticsearch_elasticsearch_05

分页查询

@Test
public void doc_search_size_test() throws IOException {
//创建Elasticsearch客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);

SearchRequest request = new SearchRequest();
request.indices("student");

SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
builder.from(0);
builder.size(2);

request.source(builder);

SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

SearchHits hits = response.getHits();
System.out.println(hits.getTotalHits());
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}

//关闭Elasticsearch客户端
esClient.close();
}

【Elasticsearch】- 使用JavaAPI操作elasticsearch_搜索引擎_06

模糊查询

@Test
public void doc_search_match_test() throws IOException {
//创建Elasticsearch客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);

SearchRequest request = new SearchRequest();
request.indices("student");
request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("name","王")));

SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(hits.getTotalHits());
for(SearchHit hit:hits){
System.out.println(hit.getSourceAsString());
}

//关闭Elasticsearch客户端
esClient.close();
}

【Elasticsearch】- 使用JavaAPI操作elasticsearch_搜索引擎_07

删除文档数据

@Test
public void doc_delete_test() throws IOException {
//创建Elasticsearch客户端
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost",9200,"http"))
);

DeleteRequest request = new DeleteRequest();
request.index("student").id("10000");
DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());

//关闭Elasticsearch客户端
esClient.close();
}


标签:JavaAPI,request,new,elasticsearch,esClient,Elasticsearch,RestHighLevelClient,客户端
From: https://blog.51cto.com/u_15901218/5914795

相关文章

  • Springboot 系列 (20) - Springboot+ElasticSearch 实现全文搜索(二)
    在“Springboot系列(19)-Springboot+ElasticSearch实现全文搜索(一)”里我们演示了安装配置ElasticSearch和使用curl调用ElasticSearchAPI,本文将演示在Springb......
  • Springboot 系列 (19) - Springboot+ElasticSearch 实现全文搜索(一)
    全文检索(Full-textSearch)是指计算机索引程序通过扫描文章中的每一个词,对每一个词建立一个索引,指明该词在文章中出现的次数和位置,当用户查询时,检索程序就根据事先建立的索......
  • ElasticSearch学习笔记
    简介Elasticsearch是开源的全文搜索引擎,它可以快速地储存、搜索和分析海量数据。维基百科、StackOverflow、Github都采用它Elastic的底层是开源库Lucene。但是,你没......
  • Linux使用 RPM 安装 Elasticsearch
    参考网站:https://www.elastic.co/guide/en/elasticsearch/reference/8.5/rpm.html#rpm-repo1.导入ElasticsearchGPG签名密钥rpm--importhttps://artifacts.elastic.c......
  • elasticsearch安装以及注意事项
    一、修改linux限制1./etc/security/limits.conf#1.修改系统中允许应用最多创建多少文件等的限制权限。一般限制应用最多创建的文件为65535,但是es至少需要65536的文件创建权......
  • SpringBoot+ElasticSearch 实现模糊查询,批量CRUD,排序,分页,高亮!
    导入elasticsearch依赖创建高级客户端基本用法创建、判断存在、删除索引对文档的CRUD批量CRUD数据查询所有、模糊查询、分页查询、排序、高亮显示总结大致流程......
  • 微服务之分布式搜索引擎elasticsearchDSL查询功能
    DSLQuery的分类Elasticsearch提供了基于JSON的DSL(DomainSpecific Language)来定义查询。常见的查询类型包括:查询所有:查询出所有数据,一般测试用。例如:match_all全文检......
  • JAVA面试题--Elasticsearch
    Elasticsearch1.为什么要使用Elasticsearch?2.Elasticsearch是如何实现Master选举的?3.Elasticsearch中的节点(比如共20个),其中的10个选了一个master,另外10个选了另一个ma......
  • ElasticSearch
    `ElasticSearch1.什么是ElasticSearchElasticSearch简称ES,是基于ApacheLucene构建的开源搜索引擎,是当前流行的企业级搜索引擎(分布式搜索引擎)。Lucene本身就可以......
  • ElasticSearch笔记
    原文地址:https://www.kuangstudy.com/bbs/1354069127022583809笔记记录B站狂神说Java的ElasticSearch课程:https://www.bilibili.com/video/BV17a4y1x7zq在学习E......