1.JAVA连接ES

es与java对应的时间类型 es在java中使用_java 访问es

配置如下

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.example
com.qf
1.0-SNAPSHOT
org.elasticsearch
elasticsearch
7.2.0
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.2.0
junit
junit
4.12
org.projectlombok
lombok
1.16.22
package com.qf.utils;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
public class ESClient {
public static RestHighLevelClient getClient(){
HttpHost httpHost=new HttpHost("192.128.64.128",9200);
RestClientBuilder clientBuilder= RestClient.builder(httpHost);
RestHighLevelClient client=new RestHighLevelClient(clientBuilder);
return client;
}
}
创建Demo1来测试连接
package com.qf.test;
import com.qf.utils.ESClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.junit.Test;
public class Demo1 {
@Test
public void testConnect() {
try {
RestHighLevelClient client = ESClient.getClient();
System.out.println("ok!");
} catch (Exception e) {
System.out.println(e);
}
}
}
2.Java创建索引
注意:由于之前使用的Liunx版本问题,在运行版本的时候提示java.net.ConnectException: Timeout connecting to xxxx.9200在网上找了很多办法无法解决,最后实在没办法就下载了Windows版本就可以了。
package com.qf.test;
import com.qf.utils.ESClient;
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.get.GetIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.client.indices.CreateIndexRequest;
//import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.junit.Test;
import java.io.IOException;
public class Demo2 {
String index="person";
String type="man";
@Test
public void createIndex() throws IOException {
//1.索引的settings
Settings.Builder settings = Settings.builder()
.put("number_of_shards", 3)
.put("number_of_replicas", 1);
//2.索引的mappings
XContentBuilder mappings = JsonXContent.contentBuilder();
mappings .startObject()
.startObject("properties")
.startObject("name")
.field("type", "text")
.endObject()
.startObject("age")
.field("type", "integer")
.endObject()
.startObject("birthday")
.field("type", "date")
.field("format", "yyyy-MM-dd")
.endObject()
.endObject()
.endObject();
//将settings和Mappings 封装到一个Request对象
CreateIndexRequest request = new CreateIndexRequest(index)
.settings(settings)
.mapping(type, mappings);
//通过Client对象连接ES并执行创建索引
RestHighLevelClient client = ESClient.getClient();
CreateIndexResponse createIndexResponse =client.indices().create(request,RequestOptions.DEFAULT);
System.out.println("resp:" + createIndexResponse.toString());
}
}
2.1Java查询删除索引
@Test
public void delete() throws IOException {
//1.准备request对象
DeleteIndexRequest request=new DeleteIndexRequest();
request.indices(index);
//2.通过Cilent操作
RestHighLevelClient client = ESClient.getClient();
AcknowledgedResponse delete=client.indices().delete(request,RequestOptions.DEFAULT);
/* 3.输出 */
System.out.println(delete);
}
@Test
public void exists() throws IOException {
//1.准备request对象
GetIndexRequest request=new GetIndexRequest();
request.indices(index);
//2.通过Cilent操作
RestHighLevelClient client = ESClient.getClient();
boolean exists= client.indices().exists(request,RequestOptions.DEFAULT);
//3.输出
System.out.println(exists);
}
2.2Java操作文档
2.2.1添加文档
1.创建Person类
package com.qf.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
public class Person {
@JsonIgnore
private Integer id;
private String name;
private Integer age;
@JsonFormat(pattern = "yyyy-MM-dd")
private Date birthday;
public Person(Integer id, String name, Integer age, Date birthday) {
this.id = id;
this.name = name;
this.age = age;
this.birthday = birthday;
}
public Integer getId() {
return id;
}
}
2.创建测试类Demo3
注:添加json依赖包
com.fasterxml.jackson.core
jackson-databind
2.10.2
packagecom.qf.test;importcom.fasterxml.jackson.core.JsonProcessingException;importcom.fasterxml.jackson.databind.ObjectMapper;importcom.qf.entity.Person;importcom.qf.utils.ESClient;importorg.elasticsearch.action.admin.indices.create.CreateIndexResponse;importorg.elasticsearch.action.index.IndexRequest;importorg.elasticsearch.action.index.IndexResponse;importorg.elasticsearch.client.RequestOptions;importorg.elasticsearch.client.RestHighLevelClient;importorg.elasticsearch.common.xcontent.XContentType;importorg.junit.Test;importjava.io.IOException;importjava.util.Date;public classDemo3 {
String index="person";
String type="man";
RestHighLevelClient client=ESClient.getClient();
ObjectMapper mapper=newObjectMapper();
@Testpublic void createDoc() throwsIOException {//1.准备一个json数据
Person persnotallow=new Person(1,"张三",20,newDate());
String jsnotallow=mapper.writeValueAsString(person);//2.准备一个request对象(手动指定id)
IndexRequest request=newIndexRequest(index,type,person.getId().toString());
request.source(json, XContentType.JSON);//3.通过client对象执行添加
IndexResponse resp =client.index(request, RequestOptions.DEFAULT);//4.输出返回结果
System.out.println(resp.getResult().toString());
}
}
2.2.2修改文档
@Test
public void update() throws IOException {
//创建Map修改指定内容
Mapdoc= new HashMap();
doc.put("name","张大三");
String docID="1";
UpdateRequest request=new UpdateRequest(index,type,docID);
request.doc(doc);
UpdateResponse update=client.update(request,RequestOptions.DEFAULT);
System.out.println(update.getResult().toString());
}
2.2.3删除文档
@Test
public void delete() throws IOException {
DeleteRequest request=new DeleteRequest(index,type,"1");
DeleteResponse resp=client.delete(request,RequestOptions.DEFAULT);
System.out.println(resp.getResult().toString());
}
2.2.4批量添加文档
@Test
public void BulkDoc() throws IOException {
Person p1= new Person(1,"HJH",20,new Date());
Person p2= new Person(2,"YQG",30,new Date());
Person p3= new Person(3,"LWD",22,new Date());
String json1= mapper.writeValueAsString(p1);
String json2= mapper.writeValueAsString(p2);
String json3= mapper.writeValueAsString(p3);
//创建Request,将准备好的数据封装进去
BulkRequest request=new BulkRequest();
request.add(new IndexRequest(index,type,p1.getId().toString()).source(json1,XContentType.JSON));
request.add(new IndexRequest(index,type,p2.getId().toString()).source(json1,XContentType.JSON));
request.add(new IndexRequest(index,type,p3.getId().toString()).source(json1,XContentType.JSON));
BulkResponse resp=client.bulk(request,RequestOptions.DEFAULT);
System.out.println(resp.toString());
}
2.2.4批量删除文档
@Testpublic void bulkDeleteDoc() throwsIOException {
BulkRequest request=newBulkRequest();
request.add(new DeleteRequest(index,type,"1"));
request.add(new DeleteRequest(index,type,"2"));
request.add(new DeleteRequest(index,type,"3"));
BulkResponse resp=client.bulk(request,RequestOptions.DEFAULT);
System.out.println(resp);
}