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
- ElasticSearch 整合 SpringBootElasticsearchSpring Boot
- SpringBoot整合elasticsearchSpring BootElasticsearch
- ElasticSearch8 - SpringBoot整合ElasticSearchElasticsearchSpring Boot
- SpringBoot整合ESSpring Boot
- SpringBoot整合ElasticSearch7.6.2Spring BootElasticsearch
- ES系列教程01:Elasticsearch學習環境搭建Elasticsearch
- ES6 學習筆記四筆記
- SpringBoot學習之整合MybatisSpring BootMyBatis
- SpringBoot整合Activiti學習(一)Spring Boot
- Rabbit學習---SpringBoot整合RabbitMQSpring BootMQ
- SpringBoot學習之整合AOPSpring Boot
- SpringBoot整合ElasticSearch(第八更)Spring BootElasticsearch
- Elasticsearch聚合學習之四:結果排序Elasticsearch排序
- springboot整合ES及其基本使用Spring Boot
- springboot 學習筆記(四)Spring Boot筆記
- 個人學習系列 - SpringBoot整合RabbitMQSpring BootMQ
- 【springboot】學習4:整合JDBC、整合druid、整合mybatis、整合 SpringSecuritySpring BootJDBCUIMyBatisGse
- SpringBoot學習之整合SwaggerSpring BootSwagger
- 【Elastic-2】SpringBoot整合ELK、SpringBoot寫ESASTSpring Boot
- SpringBoot整合Elasticsearch遊標查詢(scroll)Spring BootElasticsearch
- Elasticsearch學習Elasticsearch
- ES6學習筆記(四)【正則,集合】筆記
- elasticsearch7.6.x 整合springboot2(一)ElasticsearchSpring Boot
- SpringBoot整合ElasticSearch實現多版本的相容Spring BootElasticsearch
- 【Python機器學習實戰】決策樹與整合學習(四)——整合學習(2)GBDTPython機器學習
- Elasticsearch 學習索引Elasticsearch索引
- ElasticSearch與SpringBoot的整合與JPA方法的使用ElasticsearchSpring Boot
- 整合學習(一):簡述整合學習
- SpringBoot+Shiro學習(四):Realm授權Spring Boot
- springboot整合ElasticSearch使用completion實現補全功能Spring BootElasticsearch
- Elasticsearch學習筆記Elasticsearch筆記
- ElasticSearch 學習總結Elasticsearch
- Elasticsearch學習記錄Elasticsearch