SpringBoot 操作elasticsearch

SpringBoot 操作elasticsearch

版本环境

  • jdk1.8
  • elasticsearch 7.6.1

maven

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
 </dependency>

image-20200718170100737

注意版本,如果版本不对 需要手动进行版本指定

<properties>
		<java.version>1.8</java.version>
		<elasticsearch.version>7.6.1</elasticsearch.version>
	</properties>

image-20200718170246067

创建配置类

package com.oss.oss.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 声明其为一个配置对象
 * 生成一个连接elasticsearch 的连接对象 注入到spring容器中
 */
@Configuration
public class ElasticsearchClientConfiguration {
    @Bean
    public RestHighLevelClient restHighLevelClient(){
        RestHighLevelClient client = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http")));
        return client;
    }
}

创建测试方法

api 说明文档地址

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-exists.html

image-20200718231903392

package com.oss.oss;

import com.alibaba.fastjson.JSON;
import com.oss.oss.bean.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
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.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
class OssApplicationTests {
	@Autowired
	@Qualifier("restHighLevelClient")
	private RestHighLevelClient client;

	//测试创建索引
	@Test
	public void createIndex(){
		try {
			CreateIndexRequest request = new CreateIndexRequest("test04");
			CreateIndexResponse createIndexResponse =
					client.indices().create(request, RequestOptions.DEFAULT);
			System.out.println(createIndexResponse);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 判断索引是否存在
	 */
	@Test
	public void existsIndex(){
		try {
			GetIndexRequest request = new GetIndexRequest("test04");
			boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);
			System.out.println(exists);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 删除索引
	 */
	@Test
	public void deleteIndex(){
		try {
			DeleteIndexRequest request = new DeleteIndexRequest("test04");
			AcknowledgedResponse deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT);
			System.out.println(deleteIndexResponse.isAcknowledged());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 文档新增
	 */
	@Test
	public void createDocument(){
		try{
			IndexRequest request = new IndexRequest("test03");
			User User = new User("李白","诗仙李白",56);
			request.id("4");
			request.timeout("1s");

			request.source(JSON.toJSONString(User), XContentType.JSON);
			IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
			System.out.println(indexResponse.toString());
			System.out.println(indexResponse.status());
		} catch (Exception e){
			e.printStackTrace();
		}
	}

	/**
	 * 文档是否存在
	 */
	@Test
	public void existDocument(){
		try {
			GetRequest getRequest = new GetRequest("test03","4");
			getRequest.fetchSourceContext(new FetchSourceContext(false));
			getRequest.storedFields("_none_");

			boolean exis = client.exists(getRequest,RequestOptions.DEFAULT);
			System.out.println(exis);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 查询文档
	 */
	@Test
	public void searchDocument(){
		try {
			GetRequest getRequest = new GetRequest("test03","4");
			GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
			System.out.println(getResponse.getSourceAsString());
			System.out.println(getResponse);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 条件查询
	 *
	 */
	@Test
	public void mulitSearchDocument(){
		try {
			SearchRequest searchRequest = new SearchRequest("test03");
			SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
			searchSourceBuilder.query(QueryBuilders.matchQuery("name","李白"));
			searchRequest.source(searchSourceBuilder);

			SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
			System.out.println(JSON.toJSONString(searchResponse.getHits()));
			System.out.println("=================================");
			for (SearchHit documentFields : searchResponse.getHits().getHits()){
				System.out.println(documentFields.getSourceAsMap());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

古月_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值