Youzi - SpringBoot
type: channel
link: https://www.autisn.com/index.php/tag/SpringBoot/
items: ElasticSearch在SpringBoot中的简单运用
items: SpringBoot整合ElasticSearch

ElasticSearch在SpringBoot中的简单运用
type: item
date: 2023-10-23T22:33:00+08:00
description: 1.给es中建立索引。product,建立好映射关系PUT product { "mappings": { "properties": { "skuId": { "type": "long" }, "spuId": { "type": "long" }, "skuTitle": { "type": "text", "analyzer": "ik_smart" }, "skuPrice": { "type": "keyword" }, "skuImg": { "type": "keyword", "index": false, "doc_values": false }, "saleCount": { "type": "long" }, "hosStock": { "type": "boolean" }, "hotScore": { "type": "long" }, "brandId": { "type": "long" }, "catelogId": { "type": "long" }, "brandName": { "type": "keyword", "index": false, "doc_values": false }, "brandImg": { "type": "keyword", "index": false, "doc_values": false }, "catelogName": { "type": "keyword", "index": false, "doc_values": false }, "attrs": { "type": "nested", "properties": { "attrId": { "type": "long" }, "attrName": { "type": "keyword", "index": false, "doc_values": false }, "attrValue": { "type": "keyword" } } } } } }2.Service类package com.atguigu.gulimall.lsearch.service; import com.atguigu.common.to.es.SkuEsModel; import java.io.IOException; import java.util.List; public interface ProductSaveService { boolean productStatusUp(List<SkuEsModel> skuEsModels) throws IOException; }3.ServiceImpl类package com.atguigu.gulimall.lsearch.service.iml; import com.alibaba.fastjson.JSON; import com.atguigu.common.to.es.SkuEsModel; import com.atguigu.gulimall.lsearch.config.GulimallElasticSearchConfig; import com.atguigu.gulimall.lsearch.constant.EsConstant; import com.atguigu.gulimall.lsearch.service.ProductSaveService; import lombok.extern.slf4j.Slf4j; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @Slf4j @Service("pro…

SpringBoot整合ElasticSearch
type: item
date: 2022-05-06T17:10:00+08:00
description: 1.导入依赖这里的版本要和所按照的ELK版本匹配。 org.elasticsearch.client elasticsearch-rest-high-level-client 7.6.2 在spring-boot-dependencies中所依赖的ELK版本位6.8.7<elasticsearch.version>6.8.7</elasticsearch.version>需要在项目中将它改为7.6.2 ... <elasticsearch.version>7.6.2</elasticsearch.version> 2.编写配置package com.youzi.gulimall.search.config; import org.apache.http.HttpHost; import org.elasticsearch.client.*; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @Author youzi * @ClassName GulimallElasticSearchConfig * @Date 2022/5/6 9:42 * @Description //TODO */ /** * 1.导入依赖 * 2.编写配置，给容器中注入一个RestHighLevelClient * 3.参照API https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html */ @Configuration public class GulimallElasticSearchConfig { 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 esRestClient() { RestClientBuilder builder = null; //final String hostname, final int port, final String scheme builder = RestClient.builder(new HttpHost("106.13.234.61", 9200, "http")); RestHighLevelClient client = new RestHighLevelClient(builder); // RestHighLevelClient client = new RestHighLevelClient( // RestClient.builder( // new HttpHost("106.13.234.61", 9200, "http"))); return client; } }3.编写测试类package com.youzi.gulimall.search; import com.alibaba.fastjson.JSON; import com.youzi.gulimall.search.config.GulimallElasticSearchConfig; import lombok.Data; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; impo…