阿里二面被问Java ThreadPool线程池,看完这篇,一招怒怼面试官

redis 常用命令

  返回  

【ElasticSearch】之常用 JavaAPI

2021/8/20 11:23:06 浏览:

目录:

  • 1、引入依赖
  • 2、创建索引
  • 3、查询索引
  • 4、删除索引
  • 5、添加文档数据
  • 6、修改文档数据
  • 7、查询文档数据
  • 8、删除文档数据
  • 9、批量插入数据
  • 10、批量删除数据
  • 11、高级查询

1、引入依赖


<!-- ES核心组件 -->
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>7.8.0</version>
</dependency>

<!-- ES客户端 -->
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.8.0</version>
</dependency>

2、创建索引


// 创建 ES 客户端
RestHighLevelClient esClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost", 9200, "http"))
);

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

// 打印创建结果
System.out.println("索引创建结果:" + response.isAcknowledged());

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

3、查询索引


// 创建 ES 客户端
RestHighLevelClient esClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost", 9200, "http"))
);

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

// 打印索引信息
System.out.println(getIndexResponse.getAliases());
System.out.println(getIndexResponse.getMappings());
System.out.println(getIndexResponse.getSettings());

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

4、删除索引


// 创建 ES 客户端
RestHighLevelClient esClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost", 9200, "http"))
);

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

// 打印结果
System.out.println("删除结果:" + response.isAcknowledged());

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

5、添加文档数据


文档数据对象:

@Data
@Accessors(chain = true)
public class User {
    
    private String name;
    
    private String sex;
    
    private Integer age;
    
}

添加文档数据:

// 创建 ES 客户端
RestHighLevelClient esClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost", 9200, "http"))
);

// 创建添加文档数据请求
// index()指定要添加数据的索引,id()指定文档ID
IndexRequest request = new IndexRequest();
request.index("user").id("1001");

// 创建文档数据对象
User user = new User().setName("wang").setSex("man").setAge(18);

// 向ES插入数据,对象需要转换成JSON格式
// source()指定文档数据对象
request.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);

// 打印结果
System.out.println("数据添加结果:" + response.getResult());

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

注意: 索引操作使用的是 indices() 方法,文档数据使用的 index() 方法,表示对具体索引的操作。


6、修改文档数据


// 创建 ES 客户端
RestHighLevelClient esClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost", 9200, "http"))
);

// 创建修改文档请求
// index()指定要添加数据的索引,id()指定文档ID,doc()指定要修改的字段
UpdateRequest request = new UpdateRequest();
request.index("user").id("1001");
request.doc(XContentType.JSON, "sex", "woman");

// 修改数据
UpdateResponse response = esClient.update(request, RequestOptions.DEFAULT);

// 打印结果
System.out.println("数据修改结果:" + response.getResult());

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

7、查询文档数据


// 创建 ES 客户端
RestHighLevelClient esClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost", 9200, "http"))
);

// 创建查询文档请求
// index()指定要添加数据的索引,id()指定文档ID
GetRequest request = new GetRequest();
request.index("user").id("1001");

// 向ES查询数据
GetResponse response = esClient.get(request, RequestOptions.DEFAULT);

// 打印结果:结果存储在 source 中
System.out.println("文档数据内容:" + response.getSourceAsString());

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

8、删除文档数据


// 创建 ES 客户端
RestHighLevelClient esClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost", 9200, "http"))
);

// 创建删除文档请求
// index()指定要添加数据的索引,id()指定文档ID
DeleteRequest request = new DeleteRequest();
request.index("user").id("1001");

// 删除数据
DeleteResponse response = esClient.delete(request, RequestOptions.DEFAULT);

// 打印结果
System.out.println("数据删除结果:" + response.toString());

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

9、批量插入数据


// 创建 ES 客户端
RestHighLevelClient esClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost", 9200, "http"))
);

// 创建批量添加请求
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new IndexRequest().index("user").id("1001")
        .source(XContentType.JSON, "name", "wang", "sex", "man", "age", 18));
bulkRequest.add(new IndexRequest().index("user").id("1002")
        .source(XContentType.JSON, "name", "andy", "sex", "man", "age", 19));
bulkRequest.add(new IndexRequest().index("user").id("1003")
        .source(XContentType.JSON, "name", "lucy", "sex", "woman", "age", 20));

// 批量添加数据
BulkResponse response = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);

// 打印结果:took表示花费的时间,items表示数据项
System.out.println(response.getTook());
System.out.println(Arrays.toString(response.getItems()));

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

10、批量删除数据


// 创建 ES 客户端
RestHighLevelClient esClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost", 9200, "http"))
);

// 创建删除请求
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(new DeleteRequest().index("user").id("1001"));
bulkRequest.add(new DeleteRequest().index("user").id("1002"));
bulkRequest.add(new DeleteRequest().index("user").id("1003"));

// 批量删除数据
BulkResponse response = esClient.bulk(bulkRequest, RequestOptions.DEFAULT);

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

11、高级查询


1)全量查询

>
// 创建 ES 客户端
RestHighLevelClient esClient = new RestHighLevelClient(
        RestClient.builder(new HttpHost("localhost", 9200, "http"))
);



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

联系我们

如果您对我们的服务有兴趣,请及时和我们联系!

服务热线:18288888888
座机:18288888888
传真:
邮箱:888888@qq.com
地址:郑州市文化路红专路93号