作者:京东物流 陈晓娟

一、ES

Elasticsearch是一个流行的开源搜索引擎,它可以将大量数据快速存储和检索。Elasticsearch还提供了强大的实时分析和聚合查询功能,数据模式更加灵活。它不需要预先定义固定的数据结构,可以随时添加或修改数据字段,而不需要进行繁琐的数据库迁移。横向扩展性好,ES的分布式特性,可以简单地将数据水平切分到多个节点上,实现规模的无缝扩展。

二、ES操作
1、postman

在Elasticsearch的早期版本中,用户主要依靠通用的API工具来进行操作和管理。Postman作为一个通用的API测试工具,能够发送各种HTTP请求,包括GET、POST、PUT、DELETE等,用于与Elasticsearch进行交互。由于Elasticsearch通过RESTful API提供服务,Postman成为了一个非常流行的选择。

(1)postman创建索引

Elasticearch索引mapping写入、查看、修改_ide

(2)查询索引

Elasticearch索引mapping写入、查看、修改_Elastic_02

(3)删除索引

Elasticearch索引mapping写入、查看、修改_ide_03

(4)删除文档

Elasticearch索引mapping写入、查看、修改_Elastic_04

2、head插件

随着Elasticsearch的发展,Head插件逐渐被开发出来,提供了更多便利的功能。Head插件不仅能够显示集群的拓扑结构,还能执行索引和节点级别的操作,提供了可视化的查询接口,支持JSON验证器等。这些功能使得Head插件成为了管理和操作Elasticsearch的重要工具。

(1)安装head插件

< https://blog.51cto.com/u_16099317/10710264>

(2)安装浏览器扩展程序

a、< https://github.com/tradiff/elasticsearch-head-chrome>

b、下载压缩包 解压

c、添加到浏览器扩展程序中,输入连接信息

Elasticearch索引mapping写入、查看、修改_Elastic_05

三、head插件下索引的增删改查操作
1、创建索引 (PUT/索引名)

DSL语言

{"settings": {
//索引的分片数,确定后无法修改,通常一个分片处理20-50G数据"number_of_shards": "2",
//每个分片有多少个副本,可动态修改"number_of_replicas": "1"},"mappings": {"XXX": {"dynamic": "false","_all": {"enabled": false},"properties": {"uuid": {"type": "keyword"},"interface_config": {"type": "text"},...}}}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

Elasticearch索引mapping写入、查看、修改_ide_06

Elasticearch索引mapping写入、查看、修改_数据_07

2、查询 (GET/索引名)
GET/dp_test_temp_interface_execution_record_pre
  • 1.

Elasticearch索引mapping写入、查看、修改_Elastic_08

3、删除 (DELETE/索引名)
DELETE /userinfo GET /userinfo
  • 1.
  • 2.
  • 3.

Elasticearch索引mapping写入、查看、修改_Elastic_09

Elasticearch索引mapping写入、查看、修改_Elastic_10

4、修改 (PUT/索引名)
PUT /userinfo/ _mapping
{"properties": {"age": {"type": "interger"}}
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

Elasticearch索引mapping写入、查看、修改_Elastic_11

四、编辑器与ES服务器的交互
1、创建客户端
private static RestHighLevelClient createClient() {final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();credentialsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("XXX", "XXX"));RestClientBuilder builder = RestClient.builder(new HttpHost("XXX", XXX, "http")).setHttpClientConfigCallback(httpClientBuilder ->httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));return new RestHighLevelClient(builder);}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
2、对创建好的索引进行增删改查操作
RestHighLevelClient client = createClient();
//查询
private static void getDocument(RestHighLevelClient client) throws IOException {GetRequest getRequest = new GetRequest("XXX", "pcgat5EBH6d8RU1tFckI");GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);System.out.println("Get response: " + getResponse.getSourceAsString());}
//更新
private static void updateDocument(RestHighLevelClient client) throws IOException {Map<String, Object> updateMap = new HashMap<>();updateMap.put("message", "Elasticsearch is cool!");UpdateRequest updateRequest = new UpdateRequest("posts", "1").doc(updateMap);UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);System.out.println("Update response: " + updateResponse.getResult());}
//删除
private static void deleteDocument(RestHighLevelClient client) throws IOException {DeleteRequest deleteRequest = new DeleteRequest("posts", "pcgat5EBH6d8RU1tFckI");DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);System.out.println("Delete response: " + deleteResponse.getResult());}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

Elasticearch索引mapping写入、查看、修改_ide_12