Springboot集成ES并且使用(ElasticSearch)

Springboot集成ES(ElasticSearch)

ElasticSearch Client文档官网

1.maven依赖

Springboot集成ES并且使用(ElasticSearch)_第1张图片
Springboot集成ES并且使用(ElasticSearch)_第2张图片

原生依赖

		<dependency>
            <groupId>org.elasticsearch.clientgroupId>
            <artifactId>elasticsearch-rest-high-level-clientartifactId>
            dependency>
        <dependency>
            <groupId>org.elasticsearchgroupId>
            <artifactId>elasticsearchartifactId>
        dependency>

2.对象初始化,写配置类

Springboot集成ES并且使用(ElasticSearch)_第3张图片

//1.找到对象 2.放入spring容器中
@Configuration
public class ElasticSearchClientConfig {

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

3. 使用索引API操作

3.1 创建索引

@SpringBootTest
class ElasticsearchApi01ApplicationTests {

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

	//测试索引创建
	@Test
	void contextLoads() throws IOException {
		//1.创建索引请求
		CreateIndexRequest request = new CreateIndexRequest("wjm_index");

		//2.执行创建请求 IndecesClient 请求后获得响应
		CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);

		System.out.println(createIndexResponse);

	}
}

Springboot集成ES并且使用(ElasticSearch)_第4张图片
Springboot集成ES并且使用(ElasticSearch)_第5张图片

3.2获取索引

	//测试获取索引
	@Test
	void contextLoads2() throws IOException {
		//1.获取索引请求
		GetIndexRequest getIndexRequest = new GetIndexRequest("wjm_index");

		//2.client 索引操作 获取索引
		GetIndexResponse getIndexResponse = client.indices().get(getIndexRequest, RequestOptions.DEFAULT);

		System.out.println(getIndexResponse);
	}

3.3删除索引

	//测试删除索引
	@Test
	void contextLoads3() throws IOException {
		//1.获取索引请求
		DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("wjm_index");

		//2.client 索引操作 获取索引
		AcknowledgedResponse delete = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);

		System.out.println(delete.isAcknowledged());

	}

Springboot集成ES并且使用(ElasticSearch)_第6张图片
Springboot集成ES并且使用(ElasticSearch)_第7张图片
没了

4. 使用文档API操作

4.1创建文档

	//创建文档
	@Test
	void contextLoadsDocumentsOne() throws IOException {
		//创建对象
		GamePlayer player = new GamePlayer(1,"一只小小狗",18,"79892965","79892965");

		//创建请求
		IndexRequest request = new IndexRequest("wjm_index");

		//规则  put /wjm_index/_doc/1;
		request.id("1");
		request.timeout(TimeValue.timeValueSeconds(1));
		request.timeout("1s");

		//source  将我们的数据放入请求json
		request.source(JSON.toJSONString(player), XContentType.JSON);

		IndexResponse index = client.index(request, RequestOptions.DEFAULT);

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

Springboot集成ES并且使用(ElasticSearch)_第8张图片

4.2获取文档

	@Test
	void contextLoadsDocumentsTwo() throws IOException {

		//获取文档请求
		GetRequest wjm_index = new GetRequest("wjm_index", "1");

		//不获取返回的 文档 的上下文
		//wjm_index.fetchSourceContext(new FetchSourceContext(false));
		//wjm_index.storedFields("_none_");

		//客户端直接获取文档
		GetResponse documentFields = client.get(wjm_index, RequestOptions.DEFAULT);
		boolean exists = client.exists(wjm_index, RequestOptions.DEFAULT);

		System.out.println(documentFields.getSourceAsString());
		System.out.println(documentFields.toString());
		System.out.println(exists);
	}

Springboot集成ES并且使用(ElasticSearch)_第9张图片
在这里插入图片描述

4.3修改文档

	//更新文档
	@Test
	void contextLoadsDocumentsT() throws IOException {

		//获取文档请求
		UpdateRequest updateRequest = new UpdateRequest("wjm_index", "1");

		updateRequest.timeout("1s");

		GamePlayer player = new GamePlayer(1,"一只小小狗",20,"123","123");

		updateRequest.doc(JSON.toJSONString(player),XContentType.JSON);

		UpdateResponse update = client.update(updateRequest, RequestOptions.DEFAULT);

		System.out.println(update.status());
	}

在这里插入图片描述

4.4删除文档

	//删除文档
	@Test
	void contextLoadsDocumentsD() throws IOException {

		//获取文档请求
		DeleteRequest deleteRequest = new DeleteRequest("wjm_index", "1");
		deleteRequest.timeout("1s");
		
		DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT);

		System.out.println(delete.status());
	}

4.5批量插入文档

	//批量查询
	@Test
	void contextLoadsDocumentsQP() throws IOException, InterruptedException {

		BulkRequest bulkRequest = new BulkRequest();
		bulkRequest.timeout("10s");

		ArrayList<GamePlayer> gamePlayers = new ArrayList<>();
		for (int i = 0;i<30;i++){
			gamePlayers.add(new GamePlayer(i,"一只小小狗",i,""+i,""+i));
		}

		TimeUnit.SECONDS.sleep(1);

		//批处理请求
		for (int i=0;i<gamePlayers.size();i++){
			bulkRequest.add(
					new IndexRequest("wjm_index")
					.id(""+(i+1))
					.source(JSON.toJSONString(gamePlayers.get(i)),XContentType.JSON)
			);
		}

		BulkResponse bulk = client.bulk(bulkRequest, RequestOptions.DEFAULT);

		System.out.println(bulk.status());
	}

4.6批量搜索 中文查不出来

	//批量查询
	@Test
	void contextLoadsDocumentsQP3() throws IOException, InterruptedException {

		SearchRequest searchRequest = new SearchRequest("wjm_index");

		//构建搜索 精确匹配
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("id", "1");

		searchSourceBuilder.query(termQueryBuilder);

		searchSourceBuilder.timeout(new TimeValue(20, TimeUnit.SECONDS));

		//构建搜索放入请求
		searchRequest.source(searchSourceBuilder);

		SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);

		System.out.println(JSON.toJSONString(search.getHits()));
		System.out.println("===============");
		for (SearchHit documentFields:search.getHits().getHits()
			 ) {
			System.out.println(documentFields.getSourceAsMap());
		}
	}

Springboot集成ES并且使用(ElasticSearch)_第10张图片

你可能感兴趣的:(ElasticSearch,elasticsearch)