一 es7.x
1.1 配置pom文件
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.8.0</version>
</dependency>
<!-- elasticsearch的客户端 -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.8.0</version>
</dependency>
<!-- elasticsearch依赖2.x的log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<!-- junit单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</dependency>
<!--lombok -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
</dependencies>
1.2 构建mapping工具类
public class EsUserMapping {
public static XContentBuilder createMapping() throws IOException {
XContentBuilder builder= XContentFactory.jsonBuilder().startObject()
.startObject("user")
.startObject("properties")
.startObject("title").field("type","text").endObject()
.startObject("name").field("type","keyword").endObject()
.startObject("age").field("type","int").endObject()
.startObject("createTime").field("type","date").field("format", "yyyy-MM-dd HH:mm:ss").endObject()
.endObject()
.endObject()
.endObject();
// builder.endObject();
System.out.println("mapping:" + builder.toString());
//CreateIndexRequest index=new CreateIndexRequest("user");
return builder;
}
}
1.3 创建索引以及mapping
package com.ljf.demo;
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.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.PutMappingRequest;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
/**
* @ClassName: EsCreateUserIndexMapping
* @Description: TODO
* @Author: admin
* @Date: 2023/05/05 10:19:46
* @Version: V1.0
**/
public class EsCreateUserIndexMapping {
public static void main(String[] args) throws IOException {
String indexName="user";
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
//1.先创建索引
if(createEsIndex(esClient,indexName)){
//2.定义mapping
createEsMapping(esClient,indexName);
System.out.println("成功......");
}
else{
System.out.println("失败......");
}
esClient.close();
}
public static boolean createEsIndex(RestHighLevelClient esClient,String indexName) throws IOException {
// 创建索引
CreateIndexRequest request = new CreateIndexRequest(indexName);
CreateIndexResponse createIndexResponse =
esClient.indices().create(request,RequestOptions.DEFAULT);
// 响应状态
boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("索引操作 :" + acknowledged);
return acknowledged;
}
public static boolean createEsMapping(RestHighLevelClient esClient,String indexName) throws IOException {
// 创建索引
PutMappingRequest requestMapping=new PutMappingRequest(indexName);
requestMapping.source("{\n" +
" \"properties\": {\n" +
" \"uname\": {\n" +
" \"type\": \"text\",\n" +
" \"index\": true\n" +
" },\n" +
" \"sex\": {\n" +
" \"type\": \"text\",\n" +
" \"index\": false\n" +
" },\n" +
" \"age\": {\n" +
" \"type\": \"long\",\n" +
" \"index\": true\n" +
" },\n" +
" \"createTime\": {\n" +
" \"type\": \"date\",\n" +
" \"format\": \"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\",\n" +
" \"index\": true\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON);
AcknowledgedResponse putMappingResponse = esClient.indices().putMapping(requestMapping, RequestOptions.DEFAULT);//执行请求
// 响应状态
boolean acknowledged2 = putMappingResponse.isAcknowledged();
if(acknowledged2) {
System.out.println("mapping设置成功!");
}
return acknowledged2;
}
}
截图:
1.4 查看索引结构
http://localhost:9200/user/_mapping
1.5 添加数据
package com.ljf.demo;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
/**
* @ClassName: EsDocAdd
* @Description: TODO
* @Author: admin
* @Date: 2023/05/03 09:14:35
* @Version: V1.0
**/
public class EsDocAdd {
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 插入数据
IndexRequest request = new IndexRequest();
request.index("user").id("1001");
User user=new com.ljf.demo.User();
user.setTitle("zhongguo gongchandang wangsui");
user.setName("jurfliu");
user.setAge(34);
user.setCreatTime("2023-02-01 12:03:04");
// 向ES插入数据,必须将数据转换位JSON格式
ObjectMapper mapper = new ObjectMapper();
String userJson = mapper.writeValueAsString(user);
request.source(userJson, XContentType.JSON);
IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());
esClient.close();
}
}
查询数据
1.6 删除索引
public class EsDeleteIndex {
public static void main(String[] args) throws Exception {
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 查询索引
DeleteIndexRequest request = new DeleteIndexRequest("user");
AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);
// 响应状态
System.out.println(response.isAcknowledged());
esClient.close();
}
}
截图:
https://www.cnblogs.com/deanCopy/p/15880373.html