索引库操作
索引库操作有哪些?
- 创建索引库:PUT /索引库名
PUT /feifei
{
"mappings": {
"properties": {
"name":{
"type": "keyword",
"index": false
},
"address":{
"type": "text",
"analyzer": "ik_max_word"
},
"age":{
"type": "integer",
"index": false
}
}
}
}
- 查询索引库:GET /索引库名
- 删除索引库:DELETE /索引库名
- 添加字段:PUT /索引库名/_mapping
PUT /feifei/_mapping
{
"properties":{
"hobbby":{
"type": "text",
"analyzer": "ik_smart"
}
}
}
文档操作
文档操作有哪些?
- 创建文档:POST /{索引库名}/_doc/文档id { json文档 }
POST /feifei/_doc/1
{
"address":"河南省安阳市文峰区文昌大道和东工路",
"name":"feifei",
"age":"18"
}
-
查询文档:GET /{索引库名}/_doc/文档id
-
删除文档:DELETE /{索引库名}/_doc/文档id
-
修改文档:
- 全量修改:PUT /{索引库名}/_doc/文档id { json文档 }
- 增量修改:POST /{索引库名}/_update/文档id { “doc”: {字段}}
//如边定义的索引库我们缺少hobbyfiled并没有进行定义,此时需要为此filed进行赋值的话就需要使用增量修改 POST /feifei/_update/1 { "doc":{ "honbby":"国标" } }
RestAPI
pom依赖
<properties>
<java.version>1.8</java.version>
<repackage.classifier/>
<elasticsearch.version>7.12.1</elasticsearch.version>
</properties>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>
创建索引库
public class test {
private RestHighLevelClient client;
/**
* 每次测试之前创建es客户端
*/
@BeforeEach
void setUp(){
this.client=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://106.14.19.158:9200")));
}
//创建索引库代码如下
/**
* 代码主要分三步
* 1.创建request对象,因为是创建索引库的操作,因此request是createindexrequest
* 2.添加请求参数,其实就是dsl语言的json部分
* 3.发送请求 client.indices()方法的返回值是indicesClient类型,封装了所有和索引库有关的方法
* @throws IOException
*/
@Test
public void PutTest() throws IOException {
//创建request对象 等同于dsl语言中创建索引库代码中的 put /hotel
CreateIndexRequest Request = new CreateIndexRequest("hotel");
/**
* 请求参数 MAPPING_TEMPLATE 是静态常量字符串,内容是创建索引库的dsl语句
*/
Request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
/**
* client.indices() 返回的对象中包含 索引库操作的所有方法
*/
CreateIndexResponse Response = client.indices().create(Request, RequestOptions.DEFAULT);
boolean acknowledged = Response.isAcknowledged();
System.out.println(acknowledged);//true
}
/**
* 每次测试结束关闭es客户端
* @throws IOException
*/
@AfterEach
void close() throws IOException {
this.client.close();
}
}
删除索引库
public class test {
private RestHighLevelClient client;
/**
* 每次测试之前创建es客户端
*/
@BeforeEach
void setUp(){
this.client=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://106.14.19.158:9200")));
}
//删除索引库代码如下
/**
* 对应的dsl语言非常简单
* delete /hotel
* - 请求方式从put变为delete
* - 请求路径不变
* - 无请求参数
* 代码主要分三步
* 1.创建request,这次DeleteIndexRequest对象
* 2.准备参数。这里是无参
* 3.发送请求 client.indices()方法的返回值是indicesClient类型,封装了所有和索引库有关的方法 改为delete方法
* @throws IOException
*/
@Test
public void PutTest() throws IOException {
DeleteIndexRequest Request = new DeleteIndexRequest("hotel");
//发送请求
AcknowledgedResponse response = client.indices().delete(Request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());
}
/**
* 每次测试结束关闭es客户端
* @throws IOException
*/
@AfterEach
void close() throws IOException {
this.client.close();
}
}
判断索引库是否存在
public class test {
private RestHighLevelClient client;
/**
* 每次测试之前创建es客户端
*/
@BeforeEach
void setUp(){
this.client=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://106.14.19.158:9200")));
}
//判断索引库是否存在代码如下
/*
本质其实就是 GET /hotel
1.创建request对象,这次是GetIndexRequest对象
2.准备参数,这里是无参
3.发送请求改为 exists方法
*/
@Test
public void PutTest() throws IOException {
//创建request对象
GetIndexRequest Request = new GetIndexRequest("hotel");
//发送请求
boolean exists = client.indices().exists(Request, RequestOptions.DEFAULT);
System.out.println(exists?"存在":"不存在");
}
/**
* 每次测试结束关闭es客户端
* @throws IOException
*/
@AfterEach
void close() throws IOException {
this.client.close();
}
}
RestClient操作文档
新增文档
public class test {
@Autowired
TbHotelServiceImpl hotelService;
private RestHighLevelClient client;
/**
* 每次测试之前创建es客户端
*/
@BeforeEach
void setUp(){
this.client=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://106.14.19.158:9200")));
}
/*
对应的dsl代码为
post /hotel/_doc/{id}
{
"":""
}
对应的java代码步骤如下
1. 查询我们需要的数据
2. 将数据序列化
3. 创建对应的 新增文档的indexRequest的代码
4. 准备请求参数,将request.resource
5. 发送请求
*/
@Test
public void PutTest() throws IOException {
//根据id查找酒店数据
TbHotel hotel = hotelService.getById(39106);
//转换为文档类型
HotelDoc hotelDoc = new HotelDoc(hotel);
//转为json
String json = JSON.toJSONString(hotelDoc);
//准备request对象
IndexRequest Request = new IndexRequest("hotel").id(hotelDoc.getId().toString());
//准备json文档
Request.source(json,XContentType.JSON);
//发送请求!!
IndexResponse index = client.index(Request, RequestOptions.DEFAULT);
}
/**
* 每次测试结束关闭es客户端
* @throws IOException
*/
@AfterEach
void close() throws IOException {
this.client.close();
}
}
查询文档
public class test {
@Autowired
TbHotelServiceImpl hotelService;
private RestHighLevelClient client;
/**
* 每次测试之前创建es客户端
*/
@BeforeEach
void setUp(){
this.client=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://106.14.19.158:9200")));
}
/*
对应的查询dsl语句为
get /hotel/_doc/{id}
代码分两步:
1. 准备request对象
2. 发送请求
*/
@Test
public void PutTest() throws IOException {
GetRequest Request = new GetRequest("hotel","1");
GetResponse response = client.get(Request, RequestOptions.DEFAULT);
String result = response.getSourceAsString();
HotelDoc hotelDoc = JSON.parseObject(result, HotelDoc.class);
System.out.println(hotelDoc);
}
/**
* 每次测试结束关闭es客户端
* @throws IOException
*/
@AfterEach
void close() throws IOException {
this.client.close();
}
}
删除文档
public class test {
@Autowired
TbHotelServiceImpl hotelService;
private RestHighLevelClient client;
/**
* 每次测试之前创建es客户端
*/
@BeforeEach
void setUp(){
this.client=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://106.14.19.158:9200")));
}
/*
删除文档的dsl的代码为
delete /hotel/_doc/{id}
1. 准备request对象,因为是删除,request对象是 DeleteRequest 要指定库名和要删除的id
2. 发送参数,无参的
3. 发送请求
*/
@Test
public void PutTest() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("hotel","61803");
client.delete(deleteRequest, RequestOptions.DEFAULT);
}
/**
* 每次测试结束关闭es客户端
* @throws IOException
*/
@AfterEach
void close() throws IOException {
this.client.close();
}
}
修改文档
public class test {
@Autowired
TbHotelServiceImpl hotelService;
private RestHighLevelClient client;
/**
* 每次测试之前创建es客户端
*/
@BeforeEach
void setUp(){
this.client=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://106.14.19.158:9200")));
}
/*
删除文档的dsl的代码为
delete /hotel/_doc/{id}
1. 准备request对象,因为是删除,request对象是 DeleteRequest 要指定库名和要删除的id
2. 发送参数,无参的
3. 发送请求
*/
@Test
public void PutTest() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest("hotel","61803");
client.delete(deleteRequest, RequestOptions.DEFAULT);
}
/**
* 每次测试结束关闭es客户端
* @throws IOException
*/
@AfterEach
void close() throws IOException {
this.client.close();
}
}
批量写入
@SpringBootTest
public class test {
@Autowired
TbHotelServiceImpl hotelService;
private RestHighLevelClient client;
/**
* 每次测试 之前创建es客户端
*/
@BeforeEach
void setUp(){
this.client=new RestHighLevelClient(RestClient.builder(HttpHost.create("http://106.14.19.158:9200")));
}
/*
批量处理bulkrequest,其本质就是将多个普通crud请求组合在一起
大体步骤如下
1. 创建bulkrequest对象
2. 准备参数。批处理的参数,就是其他request对象,添加进bulkrequest
3. 发起请求。这里是批处理,调用的方法为clieat.bulk()方法
*/
@Test
public void PutTest() throws IOException {
BulkRequest bulkRequest = new BulkRequest();
List<TbHotel> tbhotels = hotelService.list(new QueryWrapper<TbHotel>().gt("id", 50000));
for (TbHotel tbhotel : tbhotels) {
HotelDoc hotelDoc = new HotelDoc(tbhotel);
bulkRequest.add(new IndexRequest("hotel").id(hotelDoc.getId().toString()).source(JSON.toJSONString(hotelDoc),XContentType.JSON));
}
//发送请求
bulkRequest.add(new IndexRequest(""))
}
/**
* 每次测试结束关闭es客户端
* @throws IOException
*/
@AfterEach
void close() throws IOException {
this.client.close();
}
}