使用 Java API 操作 elasticsearch
maven 依賴
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.9.0</version>
</dependency>
</dependencies>
程式碼示例
獲取 client
@Before
public void getClient() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();
client = new PreBuiltTransportClient(settings);
client.addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("112.124.1.100", 9300)));
}
建立 Index
@Test
public void createIndex() {
// 建立一個名稱為 blog 的索引
CreateIndexResponse response = client.admin().indices().prepareCreate("blog").execute().actionGet();
}
刪除 Index
@Test
public void deleteIndex() {
// 刪除名為 blog 的 Index
DeleteIndexResponse response = client.admin().indices().prepareDelete("blog").execute().actionGet();
}
建立 Document
@Test
public void createDocument() {
String source = "{\"content\": \"我是內容!\"}";
IndexResponse response = client.prepareIndex("blog", "article", "1").setSource(source).execute().actionGet();
}
@Test
public void createDocument() throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("name", "姜春波").endObject();
IndexResponse response = client.prepareIndex("blog", "article", "2").setSource(builder).execute().actionGet();
}
搜尋 Document
@Test
public void get() {
GetResponse response = client.prepareGet("blog", "article", "2").execute().actionGet();
System.out.println(response.getSourceAsString());
}
相關文章
- ElasticSearch Java API使用ElasticsearchJavaAPI
- ElasticSearch安裝及java Api使用ElasticsearchJavaAPI
- ElasticSearch—— Java APIElasticsearchJavaAPI
- Elasticsearch 入門實戰(9)--Java API Client 使用二ElasticsearchJavaAPIclient
- Docker 教程十三 JAVA 操作elasticsearchDockerJavaElasticsearch
- Java API操作ESJavaAPI
- 使用Java API操作zookeeper的acl許可權JavaAPI
- Java操作Elasticsearch 之 [Java High Level REST Clientedit]JavaElasticsearchRESTclient
- Elasticsearch Java High Level REST Client(Exists API)ElasticsearchJavaRESTclientAPI
- Elasticsearch Java High Level REST Client(Delete API)ElasticsearchJavaRESTclientdeleteAPI
- elasticsearch的java程式碼操作詳解ElasticsearchJava
- java內省api的操作JavaAPI
- Java API 操作Docker淺談JavaAPIDocker
- elasticsearch API使用方法備忘(Python)ElasticsearchAPIPython
- 如何在Java中使用檔案操作API: java.nio.file.Path?- marcobehlerJavaAPI
- Elasticsearch 入門實戰(8)--REST API 使用二(Search API)ElasticsearchRESTAPI
- Elasticsearch Search APIElasticsearchAPI
- .Net Api 之如何使用Elasticsearch儲存文件APIElasticsearch
- ZooKeeper 使用 Java APIJavaAPI
- EasyExcel Java API 使用ExcelJavaAPI
- 在JAVA中將Elasticsearch索引載入到Lucene APIJavaElasticsearch索引API
- .Net Core中使用NEST簡單操作ElasticsearchElasticsearch
- golang操作ElasticsearchGolangElasticsearch
- ElasticSearch - 基本操作Elasticsearch
- ElasticSearch基本操作Elasticsearch
- python操作elasticsearchPythonElasticsearch
- Java 8 Stream Api 中的 peek 操作JavaAPI
- HDFS 05 - HDFS 常用的 Java API 操作JavaAPI
- elasticsearch(八)---search apiElasticsearchAPI
- Elasticsearch(二)——Rest APIElasticsearchRESTAPI
- java api使用ElastichSearch指南JavaAPIAST
- 使用 Java 持久化 APIJava持久化API
- elasticsearch 基礎操作Elasticsearch
- go操作elasticsearch示例GoElasticsearch
- Elasticsearch CRUD基本操作Elasticsearch
- ElasticSearch 文件及操作Elasticsearch
- elasticsearch 6.x 與elasticsearch 7.x 配置與使用(Java)ElasticsearchJava
- ElasticSearch之基本用法APIElasticsearchAPI