1. JAVA API
官网的资料才是最新的,最全的:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-overview.html
引入maven
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.11.1</version>
</dependency>
<repositories>
<repository>
<id>es-snapshots</id>
<name>elasticsearch snapshot repo</name>
<url>https://snapshots.elastic.co/maven/</url>
</repository>
</repositories>
<repository>
<id>elastic-lucene-snapshots</id>
<name>Elastic Lucene Snapshots</name>
<url>https://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/83f9835</url>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
</repository>
1.1 基本增删改查
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class TestElasticsearch {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = createClient();//创建链接,创建客户端
try {
// createIndex(client); //创建索引
// addData(client); //添加数据
// queryData(client); //查询数据
// updateData(client); //修改数据
// queryData(client); //查询数据
// deleteData(client); //删除数据
// deleteIndex(client); //删除索引
bulk(client); //批量操作
}catch (Exception e){
e.printStackTrace();
}finally {
if (client != null){
closeClient(client); //关闭客户端
}
}
}
/**
* 创建链接,创建客户端
* @return
*/
public static RestHighLevelClient createClient(){
//创建链接,创建客户端
HttpHost http9200 = new HttpHost("8.140.122.156", 9200, "http");
// HttpHost http9201 = new HttpHost("8.140.122.156", 9201, "http");
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(http9200));
return client;
}
/**
* 关闭客户端
* @param client
* @throws IOException
*/
public static void closeClient(RestHighLevelClient client) throws IOException {
client.close();
}
/**
* 创建索引
* PUT order1
* {
* "settings": {
* "number_of_shards": 3,
* "number_of_replicas": 2
* },
* "mappings": {
* "properties": {
* "id":{
* "type": "long"
* },
* "age":{
* "type": "short"
* },
* "mail":{
* "type": "text",
* "fields": {
* "keyword" : {
* "type" : "keyword",
* "ignore_above" : 64
* }
* }
* },
* "name":{
* "type": "text",
* "fields": {
* "keyword" : {
* "type" : "keyword",
* "ignore_above" : 64
* }
* }
* },
* "createTime":{
* "type": "date",
* "format": ["yyyy-MM-dd HH:mm:ss"]
* }
* }
* }
* }
* @param client
* @throws IOException
*/
public static void createIndex(RestHighLevelClient client) throws IOException {
//设置3个分片,每个分片2副本
Settings settings = Settings.builder()
.put("number_of_shards",3)
.put("number_of_replicas",2)
.build();
//新增索引
CreateIndexRequest createIndexRequest = new CreateIndexRequest("order1");
createIndexRequest.mapping("properties","{\"id\":{\"type\":\"long\"},\"age\":{\"type\":\"short\"},\"mail\":{\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":64}}},\"name\":{\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":64}}},\"createTime\":{\"type\":\"date\",\"format\":[\"yyyy-MM-dd HH:mm:ss\"]}}",XContentType.JSON);
createIndexRequest.settings(settings);
//执行
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println("创建索引结果: "+createIndexResponse.isAcknowledged());
}
/**
* 插入数据
*
* @param client
* @throws IOException
*/
public static void addData(RestHighLevelClient client) throws IOException {
//数据
Map personMap = new HashMap();
personMap.put("id",1);
personMap.put("age",28);
personMap.put("name","王帆");
personMap.put("email","wangfan@yxinsur.com");
personMap.put("createTime","2020-02-02 12:00:00");
//新增索引
IndexRequest indexRequest = new IndexRequest("order1","_doc","1");
indexRequest.source(personMap);
//执行
IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println("插入数据结果: "+indexResponse.toString());
}
/**
* 查询数据
* GET order1/_doc/1
* @param client
* @throws IOException
*/
public static void queryData(RestHighLevelClient client) throws IOException {
//查询索引
GetRequest getRequest = new GetRequest("order1","_doc","1");
//执行
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
System.out.println("查询数据结果: "+getResponse.toString());
}
/**
* 修改数据
* PUT order1/_doc/1
* {
* "name": "王帆",
* "age": 29,
* "email": "wangfan@yxinsur.com"
* }
* @param client
* @throws IOException
*/
public static void updateData(RestHighLevelClient client) throws IOException {
//实际数据
Map<String,Object> personMap = new HashMap<>();
personMap.put("id",1);
personMap.put("age",29);
personMap.put("name","王帆");
personMap.put("email","wangfan@yxinsur.com");
personMap.put("createTime","2020-02-02 12:00:00");
//修改索引
UpdateRequest updateRequest = new UpdateRequest("order1","_doc","1");
updateRequest.doc(personMap);
//或者是upsert
//updateRequest.upsert(person);
//执行
UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
System.out.println("修改数据结果: "+updateResponse.toString());
}
/**
* 删除数据
* DELETE order1/_doc/1
* @param client
* @throws IOException
*/
public static void deleteData(RestHighLevelClient client) throws IOException {
//删除数据
DeleteRequest deleteRequest = new DeleteRequest("order1","_doc","1");
//执行
DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println("删除数据结果: "+deleteResponse.toString());
}
/**
* 删除索引
* DELETE order1/_doc/1
* @param client
* @throws IOException
*/
public static void deleteIndex(RestHighLevelClient client) throws IOException {
//删除索引
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("order1");
//执行
DeleteIndexResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
System.out.println("删除索引结果: "+deleteIndexResponse.isAcknowledged());
}
/**
* 批量操作
* @param client
*/
public static void bulk(RestHighLevelClient client) throws IOException {
//准备数据
Map<String,Object> personMap = new HashMap<>();
personMap.put("id",2);
personMap.put("age",29);
personMap.put("name","王帆");
personMap.put("email","wangfan@yxinsur.com");
personMap.put("createTime","2020-02-02 12:00:00");
//插入数据操作
IndexRequest indexRequest2 = new IndexRequest("order1","_doc","2");
indexRequest2.source(personMap,XContentType.JSON);
IndexRequest indexRequest3 = new IndexRequest("order1","_doc","3");
indexRequest3.source(personMap,XContentType.JSON);
//修改数据操作
personMap.put("age",30);
UpdateRequest updateRequest = new UpdateRequest("order1","_doc","2");
updateRequest.doc(personMap,XContentType.JSON);
//删除数据操作
DeleteRequest deleteRequest = new DeleteRequest("order1","_doc","2");
//将操作都放入bulk里面
BulkRequest bulkRequest = new BulkRequest();
bulkRequest.add(indexRequest2);
bulkRequest.add(indexRequest3);
bulkRequest.add(updateRequest);
bulkRequest.add(deleteRequest);
//执行
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
System.out.println("批量操作结果: "+bulkResponse.toString());
}
}
创建索引结果: true插入数据结果: IndexResponse[index=order1,type=_doc,id=1,version=1,result=created,seqNo=0,primaryTerm=1,shards={“total”:3,”successful”:1,”failed”:0}] 查询数据结果: {“_index”:”order1”,”_type”:”_doc”,”_id”:”1”,”_version”:1,”found”:true,”_source”:{“createTime”:”2020-02-02 12:00:00”,”name”:”王帆”,”id”:1,”age”:28,”email”:”wangfan@yxinsur.com”},”fields”:{“_seq_no”:[0],”_primary_term”:[1]}}修改数据结果: UpdateResponse[index=order1,type=_doc,id=1,version=2,seqNo=1,primaryTerm=1,result=updated,shards=ShardInfo{total=3, successful=1, failures=[]}]
查询数据结果: {“_index”:”order1”,”_type”:”_doc”,”_id”:”1”,”_version”:2,”found”:true,”_source”:{“createTime”:”2020-02-02 12:00:00”,”name”:”王帆”,”id”:1,”age”:29,”email”:”wangfan@yxinsur.com”},”fields”:{“_seq_no”:[1],”_primary_term”:[1]}}
删除数据结果: DeleteResponse[index=order1,type=_doc,id=1,version=3,result=deleted,shards=ShardInfo{total=3, successful=1, failures=[]}]
删除索引结果: true
批量操作结果: org.elasticsearch.action.bulk.BulkResponse@793be5ca