Elasticsearch學習<四>SpringBoot整合es
一、Java操作es有兩種方式:
1、tcp的方式
2、http的方式
二、具體步驟:
1、建立一個springboot專案
2、開啟官方文件:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.x/java-rest-high.html
3、安裝文件操作
3.1、新增maven依賴:
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.11.0</version> </dependency>
因為springbotot中有es的依賴版本號,這塊我們改一下和我們自己的一致:進行這四步操作
3.2、新增操作客戶端
@Configuration
public class ElasticSearchConfig {
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
// builder.addHeader("Authorization", "Bearer " + TOKEN);
// builder.setHttpAsyncResponseConsumerFactory(
// new HttpAsyncResponseConsumerFactory
// .HeapBufferedResponseConsumerFactory(30 * 1024 * 1024 * 1024));
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient esClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("ip", 9200, "http")));
//ip地址,埠號,互動方式
return client;
}
}
4、新增
public void save() throws IOException {
IndexRequest request = new IndexRequest("users");
request.id("1");
User user = new User();
user.setAge(2);
user.setName("我是中國人");
user.setSex("男");
request.source(new ObjectMapper().writeValueAsString(user), XContentType.JSON);
IndexResponse index = client.index(request, GulimallElasticSearchConfig.COMMON_OPTIONS);
System.out.println(index);
}
5、查詢
public void searchSyn() throws IOException {
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("users");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
System.out.println(searchResponse.toString());
}
複雜操作可以安裝官網上的例子進行操作。
相關文章
- Elasticsearch學習(三)springboot整合ElasticSearchElasticsearchSpring Boot
- 「Elasticsearch」SpringBoot快速整合ESElasticsearchSpring Boot
- 手把手教你SpringBoot整合Elasticsearch(ES)Spring BootElasticsearch
- SpringBoot整合ElasticSearch 入門demo學習筆記Spring BootElasticsearch筆記
- SpringBoot 整合 elasticsearchSpring BootElasticsearch
- SpringBoot整合elasticsearchSpring BootElasticsearch
- ElasticSearch 整合 SpringBootElasticsearchSpring Boot
- SpringBoot整合ESSpring Boot
- ElasticSearch8 - SpringBoot整合ElasticSearchElasticsearchSpring Boot
- SpringBoot整合ElasticSearch7.6.2Spring BootElasticsearch
- SpringBoot整合ElasticSearch(第八更)Spring BootElasticsearch
- SpringBoot學習之整合SwaggerSpring BootSwagger
- SpringBoot學習之整合MybatisSpring BootMyBatis
- SpringBoot學習之整合AOPSpring Boot
- Rabbit學習---SpringBoot整合RabbitMQSpring BootMQ
- SpringBoot整合Activiti學習(一)Spring Boot
- springboot整合ES及其基本使用Spring Boot
- 【springboot】學習4:整合JDBC、整合druid、整合mybatis、整合 SpringSecuritySpring BootJDBCUIMyBatisGse
- 【Elastic-2】SpringBoot整合ELK、SpringBoot寫ESASTSpring Boot
- springboot 學習筆記(四)Spring Boot筆記
- ES系列教程01:Elasticsearch學習環境搭建Elasticsearch
- ES6 學習筆記四筆記
- SpringBoot整合Elasticsearch遊標查詢(scroll)Spring BootElasticsearch
- 個人學習系列 - SpringBoot整合RabbitMQSpring BootMQ
- Elasticsearch聚合學習之四:結果排序Elasticsearch排序
- SpringBoot整合ElasticSearch實現多版本的相容Spring BootElasticsearch
- elasticsearch7.6.x 整合springboot2(一)ElasticsearchSpring Boot
- 【Python機器學習實戰】決策樹與整合學習(四)——整合學習(2)GBDTPython機器學習
- SpringBoot+Shiro學習(四):Realm授權Spring Boot
- ElasticSearch與SpringBoot的整合與JPA方法的使用ElasticsearchSpring Boot
- springboot整合ElasticSearch使用completion實現補全功能Spring BootElasticsearch
- jackson學習之九:springboot整合(配置檔案)Spring Boot
- Spring boot學習(四)Spring boot整合DruidSpring BootUI
- ES6學習筆記(四)【正則,集合】筆記
- Elasticsearch學習Elasticsearch
- springboot整合elasticsearch7.2(基於官方high level client)Spring BootElasticsearchclient
- jackson學習之十(終篇):springboot整合(配置類)Spring Boot
- ES[7.6.x]學習筆記(十一)與SpringBoot結合筆記Spring Boot