当前位置:   article > 正文

IDEA中对ElasticSearch的使用_idea elasticsearch

idea elasticsearch

1、ElasticSearch依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency> 
  • 1
  • 2
  • 3
  • 4

2、构建RestHighLevelClient(高级客户端)对象,实现SpringBoot和elasticSearch集成,返回一个client
在IDEA中,如何使用已经配好的ElasticSearch,将项目个ElasticSearch和SpringBoot整合在一起
首先在官网是看到
在这里插入图片描述
翻译:
在这里插入图片描述
所以一个高级的客户端(RestHighLevelClient)需要一个Rest低级客户端构造器来构建
所以我们要在定义一个配之类来提供这种功能。elasticSearch就是通过这一步和SpringBoot集成的

@Configuration
public RestHighLevelClient restHighLevelClient(){
   
	@Bean
	public RestHighLevelClient restHighLevelClient() {
   
	RestHighLevelClient client = new RestHighLevelClient(
		RestClient.builder(
			//整个new HttpHost("localhost",9200,“http”)可以配置多个
			//在集群的时候就可以配置多个
			new HttpHost("localhost", 9200, "http")));
	return client;
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

ElasticSearch中的常用方法工具类封装(就是在一个类中封装了对索引的创建、删除等一系列的操作)

/**
*@Component声明这个类将被SpringIOC容器扫描装配
*/
@Component
public class ElasticSearchUtils<T> {
   
@Autowired
private RestHighLevelClient restHighLevelClient;

/**
* 判断索引是否存在
*/
public boolean existsIndex(String index) throws IOException {
   
	GetIndexRequest request = new GetIndexRequest(index);
	boolean exists = client.indices().exists(request,RequestOptions.DEFAULT);
	return exists;
}

/**
*创建索引
*/
public boolean createIndex(String index) throws IOException {
   
CreateIndexRequest request = new CreateIndexRequest(index);
CreateIndexResponse createIndexResponse=client.indices().create(request,RequestOptions.DEFAULT);
return createIndexResponse.isAcknowledged();
}

/**
*删除索引
*/
public boolean deleteIndex(String index) throws IOException {
   
	DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
	AcknowledgedResponse response = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
	return response.isAcknowledged();
}

/**
*判断某索引下文档id是否存在
*/
public boolean docExists(String index, String id) throws IOException {
   
	GetRequest getRequest = new GetRequest(index,id);
	//只判断索引是否存在不需要获取_source
	//getRequest.fetchSourceContext(new FetchSourceContext(false));
	//getRequest.storedFields("_none_");
	boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
	return exists;
}

/**
*添加文档记录
*/
public boolean addDoc(String index,String id,T t) throws IOException {
   
	IndexRequest request = new IndexRequest(index);
	request.id(id);
	//设置超时时间
	request.timeout(TimeValue.timeValueSeconds(1));
	request.timeout("1s");
	//将前端获取来的数据封装成一个对象转换成JSON格式放入请求中
	request.source(JSON.toJSONString(t), XContentType.JSON);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/949275
推荐阅读
相关标签
  

闽ICP备14008679号