创建RestHighLevelClient对象
elasticsearch: host: 101.35.247.167 port: 9200
package com.zhang.elasticsearch.config;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "elasticsearch")
@Configuration
@Component
public class ElasticsearchConfig {
private String host;
private int port;
private String scheme = "http";
public void setHost(String host) {
this.host = host;
}
public void setPort(int port) {
this.port = port;
}
public void setScheme(String scheme) {
this.scheme = scheme;
}
@Bean
public RestHighLevelClient Client() {
return new RestHighLevelClient(RestClient.builder(new HttpHost(
host, port, scheme)));
}
}
package com.zhang.elasticsearch;
import com.alibaba.fastjson.JSON;
import com.zhang.elasticsearch.domain.User;
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.mapping.put.PutMappingRequest;
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.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.*;
import org.elasticsearch.client.ml.PostDataRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
@SpringBootTest
class ElasticsearchApplicationTests {
@Autowired
RestHighLevelClient client;
@Test
void contextLoads() {
System.out.println(client);
}
//创建索引 IndicesClient对索引进行CRUD,IndexRequest则是对文档进行CRUD
@Test
public void addIndex() throws IOException {
//获得索引库对象
IndicesClient indices = client.indices();
//创建索引请求
CreateIndexRequest index = new CreateIndexRequest("zyp");
//创建索引
CreateIndexResponse result = indices.create(index, RequestOptions.DEFAULT);
System.out.println(result.isAcknowledged());
}
/* 创建映射 analyzer为制定ik分词器
* @Author: zyp
* @Date: 2021/12/14 17:15
* @Param: []
* @Return: void
* @Description
*/
@Test
public void createIndex() throws IOException {
//获得所有客户端
IndicesClient indices = client.indices();
//创建添加映射请求
PutMappingRequest putMappingReq = new PutMappingRequest("zyp");
XContentBuilder mapping;
//构造映射
//{"properties":{"name":{"type":"text"},"age":{"type":"long"},"adr":{"type":"text","analyzer:"ik_smart""}}} startObject对应左花括号,endObject对应有括号
mapping = jsonBuilder().startObject().startObject("properties").startObject("name").
field("type", "text").endObject()
.startObject("age").field("type", "long").endObject()
.startObject("adr").field("type", "text").field("analyzer", "ik_smart").endObject()
.endObject().endObject();
putMappingReq.type("docs");
putMappingReq.source(mapping);
AcknowledgedResponse acknowledgedResponse = indices.putMapping(putMappingReq, RequestOptions.DEFAULT);
System.out.println(acknowledgedResponse.isAcknowledged());
}
/* 添加文档
* @Author: zyp
* @Date: 2021/12/14 17:15
* @Param: []
* @Return: void
* @Description
*/
@Test
public void addDoc() throws IOException {
//创建要存入的对象
User user = new User();
user.setName("张三");
user.setAge(23);
user.setAddress("永丰");
//将对象转为json字符串
String UserJson = JSON.toJSONString(user);
//创建索引请求
IndexRequest indexRequest = new IndexRequest("zyp").id("6").source(UserJson, XContentType.JSON);
//创建文档
IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.getResult());
}
@Test
/* 修改文档,添加文档,存在则为修改 其中version对应该文档的版本号
* @Author: zyp
* @Date: 2021/12/14 17:14
* @Param: []
* @Return: void
* @Description
*/
public void updateDoc() throws IOException {
//创建要存入的对象
User user = new User();
user.setName("张三");
user.setAge(23);
user.setAddress("永丰县");
//讲对象转为json字符串
String UserJson = JSON.toJSONString(user);
//获得更新索引请求对象
IndexRequest updateRequest = new IndexRequest("zyp").id("6").source(UserJson, XContentType.JSON);
IndexResponse index = client.index(updateRequest, RequestOptions.DEFAULT);
System.out.println(index.getResult());
}
@Test
/*查询文档
* @Author: zyp
* @Date: 2021/12/14 17:14
* @Param: []
* @Return: void
* @Description
*/
public void getDoc() throws IOException {
//获得查询索引的请求对象
GetRequest gerRequest = new GetRequest("zyp").id("6");
//获得文档对象
GetResponse doc = client.get(gerRequest, RequestOptions.DEFAULT);
//获得文档数据
System.out.println(doc.getSourceAsString());
}
@Test
/* 删除文档信息
* @Author: zyp
* @Date: 2021/12/14 17:13
* @Param: []
* @Return: void
* @Description
*/
public void delDoc() throws IOException {
//获得删除的索引请求对象
DeleteRequest delRequest = new DeleteRequest("zyp").id("6");
//删除文档
DeleteResponse delete = client.delete(delRequest, RequestOptions.DEFAULT);
System.out.println(delete.getIndex());
}
/*删除索引
* @Author: zyp
* @Date: 2021/12/14 17:13
* @Param: []
* @Return: void
* @Description
*/
@Test
public void delIndex() throws IOException {
IndicesClient indices = client.indices();
DeleteIndexRequest delReq = new DeleteIndexRequest("zyp");
AcknowledgedResponse delete = indices.delete(delReq, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
//从数据库中批量导入数据到es
@Test
void contextLoads() throws IOException {
//查询mysql中所有数据
List goodes = goodsMapper.listGoods();
//创建批量处理对象
BulkRequest bulkRequest = new BulkRequest();
//循环添加新增处理请求
for (Goods goods : goodes) {
goods.setSpec(JSON.parseObject(goods.getSpecStr(), Map.class));
String goodsJson = JSON.toJSONString(goods);
IndexRequest indexRequest = new IndexRequest("goods").id(goods.getId() + "").source(goodsJson, XContentType.JSON);
bulkRequest.add(indexRequest);
}
//提交批量处理对象
BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);
//查看添加状态
System.out.println(bulk.status());
}
}