Docker 教程十三 JAVA 操作elasticsearch
第一步:依賴 jar
<!-- https://mvnrepository.com/artifact/org.elasticsearch.client/transport -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.6.10</version>
</dependency>
第二步:新增索引
public final static String HOST = "10.211.55.33";
public final static int PORT = 9300;//http請求的埠是9200,客戶端是9300
private static TransportClient client;
public static void main(String[] args) throws Exception {
Settings settings = Settings.builder().put("cluster.name", "es01").build();
//建立客戶端
client = new PreBuiltTransportClient(settings).addTransportAddresses(
new InetSocketTransportAddress(InetAddress.getByName(HOST),PORT));
IndexResponse response = client.prepareIndex("books", "book", "2")
.setSource(jsonBuilder().
startObject()
.field("book_name", "ElasticSearch入門")
.field("author", "老邢")
.field("publish_time", new Date())
.endObject())
.get();
//關閉客戶端
client.close();
}
第四步:獲取索引
public final static String HOST = "10.211.55.33";
public final static int PORT = 9300;//http請求的埠是9200,客戶端是9300
private static TransportClient client;
public static void main(String[] args) throws Exception {
Settings settings = Settings.builder().put("cluster.name", "es01").build();
//建立客戶端
client = new PreBuiltTransportClient(settings).addTransportAddresses(
new InetSocketTransportAddress(InetAddress.getByName(HOST),PORT));
GetResponse getResponse = client.prepareGet("books", "book", "2").get();
System.err.println("索引庫的資料:" + getResponse.getSourceAsString());
//關閉客戶端
client.close();
}
第五步:修改索引
//修改
JsonObject jsonObject=new JsonObject();
jsonObject.addProperty("author","feri");
UpdateResponse updateResponse = client.prepareUpdate("books", "book", "2")
.setDoc(jsonObject.toString(),XContentType.JSON).get();
第六步:刪除索引
DeleteResponse deleteResponse = client.prepareDelete("books", "book", "1").get();
System.err.println("deleteResponse索引名稱:" + deleteResponse.getIndex() + "\n deleteResponse型別:" + deleteResponse.getType()
+ "\n deleteResponse文件ID:" + deleteResponse.getId() + "\n當前例項deleteResponse狀態:" + deleteResponse.status());
相關文章
- Docker 教程十三安裝elasticsearchDockerElasticsearch
- 使用 Java API 操作 elasticsearchJavaAPIElasticsearch
- ElasticSearch 2.3.3 java API操作二ElasticsearchJavaAPI
- docker安裝elasticsearch及kibana部署、操作DockerElasticsearch
- Elasticsearch 在 docker 和 CentOS 下的安裝教程ElasticsearchDockerCentOS
- Java操作Elasticsearch 之 [Java High Level REST Clientedit]JavaElasticsearchRESTclient
- elasticsearch的java程式碼操作詳解ElasticsearchJava
- Java入門教程十三(多執行緒)Java執行緒
- Java API 操作Docker淺談JavaAPIDocker
- Docker最全教程——Redis容器化以及排行榜實戰(十三)DockerRedis
- ElasticSearch的Java Api基本操作入門指南ElasticsearchJavaAPI
- docker-部署ElasticsearchDockerElasticsearch
- docker安裝elasticsearchDockerElasticsearch
- Elasticsearch使用系列-Docker搭建Elasticsearch叢集ElasticsearchDocker
- Elasticsearch - Docker安裝Elasticsearch8.12.2ElasticsearchDocker
- golang操作ElasticsearchGolangElasticsearch
- ElasticSearch基本操作Elasticsearch
- ElasticSearch - 基本操作Elasticsearch
- Docker(十三)-Docker save and load映象儲存Docker
- 無涯教程:Docker - Java安裝DockerJava
- elasticsearch教程一Elasticsearch
- Docker Elasticsearch 叢集配置DockerElasticsearch
- Docker部署ElasticSearch叢集DockerElasticsearch
- elasticsearch 基礎操作Elasticsearch
- Elasticsearch CRUD基本操作Elasticsearch
- ElasticSearch 文件及操作Elasticsearch
- python操作elasticsearchPythonElasticsearch
- go操作elasticsearch示例GoElasticsearch
- [第十三篇]——Docker ComposeDocker
- docker 執行elasticsearch單例項(elasticsearch:7.12.0)DockerElasticsearch單例
- ElasticSearch安裝教程Elasticsearch
- ElasticSearch 安裝教程Elasticsearch
- Elasticsearch入門教程Elasticsearch
- (三)docker-Elasticsearch 叢集DockerElasticsearch
- Docker部署並執行ElasticsearchDockerElasticsearch
- docker操作Docker
- (2)Elasticsearch基本操作整理Elasticsearch
- PHP 操作 ElasticSearch7.8.1PHPElasticsearch