当前位置: 首页 > 工具软件 > WuKong Search > 使用案例 >

Elasticsearch与springboot结合应用(四)

孔驰
2023-12-01

本文主要介绍Elasticsearch与springboot结合应用

一、新建项目并添加依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.9.0</version>
</dependency>

二、添加配置类

新建ElasticSearchConfig.java配置类,并在该配置类中配置一个RestHighLevelClient对象

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;

@Configuration
public class ElasticSearchConfig{

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

三、创建ElasticSearchApi类

将增删改查等操作,作一下封装,方便后期调用

public class ElasticSearchApi{
	 @Autowired
	 RestHighLevelClient restHighLevelClient;
	 /**
	  * 创建索引请求
	  * @param index 索引
	  * @throws IOException
	  */
	void CreateIndex(String index) throws IOException{
        CreateIndexRequest request = new CreateIndexRequest(index);
        CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request,RequestOptions.DEFAULT);
        System.out.println("创建成功,创建的索引名为:" +createIndexResponse.index());
	}


     /**
	 * 新增一条文档
	 * @param indexObj JSONObject格式,包含index,type,id
	 * @param source JSON格式的字符串string,比如  "{\"name\":\"齐天大圣孙悟空\",\"age\":685,\"bir\":\"1685-01-01\",\"introduce\":\"花果山水帘洞美猴王齐天大圣孙悟空是也!\"," +
	                        "\"address\":\"花果山\"}"
	 * @throws IOException
	 */
	public void AddDoc(JSONObject indexObj,String source) throws IOException {
	    /**
	      * 向ES中的索引christy下的type类型中添加一天文档
	      */
		String index = indexObj.getString("index");
		String type  = indexObj.getString("type");
		String id    = indexObj.getString("id");
	    IndexRequest indexRequest = new IndexRequest(index,type,id);
	    indexRequest.source(source, XContentType.JSON);
	    IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
	    System.out.println(indexResponse.status());
	}   

    /**
	 * 删除一条文档
	 * @throws IOException
	 */
	public void deleteDoc(JSONObject indexObj) throws IOException {
		String index = indexObj.getString("index");
		String type  = indexObj.getString("type");
		String id    = indexObj.getString("id");
	    DeleteRequest deleteRequest = new DeleteRequest(index,type,id);
	    DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);
	    System.out.println(deleteResponse.status());
	}

    /**
	 * 更新一条文档
	 * @param indexObj JSONObject格式,包含index,type,id
	 * @param source  eg: "{\"name\":\"调皮捣蛋的hardy\"}"
	 * @throws IOException
	 */
	public void updateDoc(JSONObject indexObj,String source) throws IOException {
		String index = indexObj.getString("index");
		String type  = indexObj.getString("type");
		String id    = indexObj.getString("id");
	    UpdateRequest updateRequest = new UpdateRequest(index,type,id);
	    updateRequest.doc(source,XContentType.JSON);
	    UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);
	    System.out.println(updateResponse.status());
	}

}

上一篇:Elasticsearch与springboot结合应用(三)_一个高效工作的家伙的博客-CSDN博客

下一篇:

 类似资料: