SpringBoot整合ElasticSearch及API详解

ELK简介

ELK是三个开源软件的缩写,分别表示:Elasticsearch , Logstash, Kibana , 它们都是开源软件。

Elasticsearch是个开源分布式搜索引擎,提供搜集、分析、存储数据三大功能。它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等。

Logstash 主要是用来日志的搜集、分析、过滤日志的工具,支持大量的数据获取方式。一般工作方式为c/s架构,client端安装在需要收集日志的主机上,server端负责将收到的各节点日志进行过滤、修改等操作在一并发往elasticsearch上去。

Kibana 也是一个开源和免费的工具,Kibana可以为 Logstash 和 ElasticSearch 提供的日志分析友好的 Web 界面,可以帮助汇总、分析和搜索重要数据日志。

ELK是向大数据发展的必经之路。而ElasticSearch主要用于搜索引擎相关方面,其也是一种Nosql数据库,相比于solr,在数据量不断增大的情况下,其性能、速度等方面有显著优势。

SpringBoot集成ElasticSearch

首先需要引入es的jar包,也可以直接引入SpringData中的包。

<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-data-elasticsearchartifactId>
dependency>

编写config

@Configuration
public class ElasticSearchClientConfig {
     

    @Bean
    public RestHighLevelClient restHighLevelClient(){
     
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost",9200,"http")
                )
        );
        return client;
    }
}

创建User实体类

@Component
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
     
    private String name;
    private int age;

}

API详解

测试API

编写测试类

/**
 * ElasticSearch 高级客户端API
 */
@SpringBootTest
class EsApiApplicationTests {
     

	@Autowired
	@Qualifier("restHighLevelClient")
	private RestHighLevelClient client;

}

索引的创建

	// 索引的创建
	@Test
	void testCreateIndex() throws IOException {
     
		//1、创建索引请求
		CreateIndexRequest request = new CreateIndexRequest("purple_index");
		//2、执行创建请求
		CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);

		System.out.println(createIndexResponse);
	}

测试获取索引,判断其是否存在

	// 测试获取索引,判断其是否存在
	@Test
	void testExistIndex() throws IOException {
     
		GetIndexRequest request = new GetIndexRequest("purple_index");
		boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
		System.out.println(exists);
	}

测试删除索引

	// 测试删除索引
	@Test
	void testDeleteIndex() throws IOException {
     
		DeleteIndexRequest request = new DeleteIndexRequest("purple_index");
		AcknowledgedResponse delete = client.indices().delete(request, RequestOptions.DEFAULT);
		System.out.println(delete.isAcknowledged());
	}

测试添加文档

	//测试添加文档
	@Test
	void testAddDocument() throws IOException {
     
		//创建对象
		User user = new User("紫色星辰",3);
		//创建请求
		IndexRequest request = new IndexRequest("purple_index");
		request.id("1");
		request.timeout(TimeValue.timeValueSeconds(1));

		//将数据放入请求 json
		request.source(JSON.toJSONString(user), XContentType.JSON);

		//客户端发送请求,获取响应结果
		IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);

		System.out.println(indexResponse.toString());
		System.out.println(indexResponse.status());
	}

获取文档,判断是否存在

	//获取文档,判断是否存在
	@Test
	void testIsExists() throws IOException {
     
		GetRequest getRequest = new GetRequest("purple_index", "1");
		//不获取返回的_source 的上下文了
		getRequest.fetchSourceContext(new FetchSourceContext(false));
		getRequest.storedFields("_none_");

		boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
		System.out.println(exists);
	}

获取文档信息

	//获取文档信息
	@Test
	void testGetDocument() throws IOException {
     
		GetRequest getRequest = new GetRequest("purple_index", "1");
		GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
		System.out.println(getResponse.getSourceAsString());
		System.out.println(getResponse);
	}

更新文档信息

	//更新文档信息
	@Test
	void testUpdateRequest() throws IOException {
     
		UpdateRequest updateRequest = new UpdateRequest("purple_index", "1");
		updateRequest.timeout("1s");
		User user = new User("香草拿铁", 18);
		updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
		UpdateResponse updateResponse = client.update(updateRequest,RequestOptions.DEFAULT);
		System.out.println(updateResponse.status());
	}

删除文档信息

	//删除文档信息
	@Test
	void testDeleteRequest() throws IOException {
     
		DeleteRequest request = new DeleteRequest("purple_index", "1");
		request.timeout("1s");
		DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
		System.out.println(deleteResponse.status());
	}

批量插入数据

	//批量插入数据
	@Test
	void testBulkRequest() throws IOException {
     
		BulkRequest bulkRequest = new BulkRequest();
		bulkRequest.timeout("10s");
		ArrayList<User> userList = new ArrayList<>();
		userList.add(new User("紫色星辰1",1));
		userList.add(new User("紫色星辰2",2));
		userList.add(new User("紫色星辰3",3));

		for (int i = 0; i < userList.size(); i++) {
     
			bulkRequest.add(
					new IndexRequest("purple_index")
					.id(""+(i+1))
					.source(JSON.toJSONString(userList.get(i)),XContentType.JSON)
			);
		}

		BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
		System.out.println(bulkResponse.hasFailures());
	}

查询

	//查询
	//SearchRequest  搜索请求
	//SearchSourceBuilder  条件构造
	//HighlightBuilder  构建高亮
	//TermQueryBuilder  精确查询
	//MatchAllQueryBuilder  全量查询
	//xxx QueryBuilder 对应所有命令
	@Test
	void testSearch() throws IOException {
     
		SearchRequest searchRequest = new SearchRequest("purple_index");
		//构建搜索条件
		SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
		//查询条件,我们可以使用 QueryBuilders工具来实现
		//QueryBuilders.termQuery 精确匹配
		//QueryBuilders.matchAllQuery() 匹配所有

		TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("age", "1");
//		MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
		sourceBuilder.query(termQueryBuilder);
//		//分页
//		sourceBuilder.from();
//		sourceBuilder.size();
		sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

		searchRequest.source(sourceBuilder);
		SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
		System.out.println(JSON.toJSONString(searchResponse.getHits()));
		System.out.println("=======================");
		for (SearchHit hit : searchResponse.getHits()) {
     
			System.out.println(hit.getSourceAsMap());
		}
	}

你可能感兴趣的:(SpringBoot整合ElasticSearch及API详解)