package com.gulimall.gulimallsearch.search;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.SortOrder;
import co.elastic.clients.elasticsearch.core.*;
import co.elastic.clients.elasticsearch.core.bulk.BulkOperation;
import co.elastic.clients.elasticsearch.core.search.Hit;
import co.elastic.clients.elasticsearch.core.search.HitsMetadata;
import co.elastic.clients.elasticsearch.indices.CreateIndexResponse;
import co.elastic.clients.elasticsearch.indices.GetMappingResponse;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gulimall.gulmallsearch.entity.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
* @Author BaiYZ
* @Program gulimail
* @Description 测试elasticsearch
* @Date 2022-03-27 22:43:48
*/
@SpringBootTest
public class TestElasticsearch {
@Autowired
private ElasticsearchClient elasticsearchClient;

@Test
public void testSearch() {
System.out.println(elasticsearchClient);
}

@Test
void indexData() throws IOException {
// lambda传递了function接口的apply的实现,参数t会根据function<R,T>进行定义,R为返回T为参数类型,也就是传入lambda参数会自动类型推断——>fn接口中的apply()方法被实现了,那么调用fun.apply(参数)就会执行传递的方法的实现
CreateIndexResponse java_client_index = elasticsearchClient.indices().create(createIndexBuilder ->
createIndexBuilder.index("java_client_index")
);
//是否成功创建
Boolean acknowledged = java_client_index.acknowledged();
System.out.println("索引操作:" + acknowledged);
}

@Test
void searchData() throws IOException {
GetMappingResponse mapping = elasticsearchClient.indices().getMapping(e -> e.index("java_client_index"));
System.out.println(mapping);
System.out.println(elasticsearchClient.indices().get(r -> r.index("java_client_index")).result().size());
System.out.println(elasticsearchClient.indices().getAlias(e -> e.index("java_client_index")));
System.out.println(elasticsearchClient.indices().getSettings(e -> e.index("java_client_index")));

}

@Test
void deleteIndex() throws IOException {
System.out.println(elasticsearchClient.indices().delete(r -> r.index("java_client_index")).acknowledged());
}

@Test
void insertIndex() throws IOException {
User user = new User("byz", "男", 25);
ObjectMapper objectMapper = new ObjectMapper();
String userJson = objectMapper.writeValueAsString(user);
System.out.println(userJson);
IndexResponse indexResponse = elasticsearchClient.index(r -> r.index("user").id("1001").document(user));
System.out.println(indexResponse.result().jsonValue());
}

@Test
void updateIndex() throws IOException {
User user = new User();
user.setAge(15);
UpdateResponse<User> updateUser = elasticsearchClient.update(r -> r.index("user").id("1001").doc(user), User.class);
System.out.println(updateUser);
}

/**
* 判断是否存在
*
* @throws IOException 异常
*/
@Test
void existDocument() throws IOException {
BooleanResponse userBoolean = elasticsearchClient.exists(r -> r.index("user").id("1001"));
System.out.println(userBoolean.value());
}

/**
* 查询一条数据
*
* @throws IOException 异常
*/
@Test
void getDocument() throws IOException {
GetResponse<User> resp = elasticsearchClient.get(r -> r.index("user").id("1001"), User.class);
User source = resp.source();
System.out.println(source);
}

/**
* 删除一个文档
*
* @throws IOException 异常
*/
@Test
void deleteDocument() throws IOException {
DeleteResponse deleteResponse = elasticsearchClient.delete(r -> r.index("user").id("1001"));
System.out.println(deleteResponse.result().jsonValue());
}

/**
* 批量插入
*
* @throws IOException 异常
*/
@Test
void bulkList() throws IOException {
List<User> userList = new ArrayList<>();
userList.add(new User("jon", "男", 18));
userList.add(new User("lucy", "男", 17));
userList.add(new User("jack", "女", 199));
userList.add(new User("person", "男", 28));
userList.add(new User("user", "女", 23));
List<BulkOperation> bulkOperations = new ArrayList<>();
for (User user : userList) {
bulkOperations.add(BulkOperation.of(o -> o.index(r -> r.document(user))));
}
BulkResponse bulkResponse = elasticsearchClient.bulk(b -> b.index("user").operations(bulkOperations));
System.out.println(bulkResponse.errors());
}

/**
* 条件查询
*
* @throws IOException 异常
*/
@Test
void searchTest() throws IOException {
SearchResponse<User> search = elasticsearchClient.search(s -> s
.index("user")
.query(q -> q
.term(t -> t
.field("sex")
.value(v -> v.stringValue("男"))
))
.from(0)
.size(3)
.sort(f -> f.field(o -> o.field("age").order(SortOrder.Desc))), User.class);
HitsMetadata<User> hits = search.hits();
List<Hit<User>> hitsUser = hits.hits();
System.out.println(hits.total().value());
for (Hit<User> userHit : hitsUser) {
System.out.println(userHit.source());
}
}
}