引入maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
加入配置文件
因为我本地和生产的连接地址不一样,所以在yml 中配置不同的serverUrl
@Configuration
public class ElasticSearchConfig {
@Value(value = "${elasticsearch.server.url}")
private String serverUrl ;
@Bean
RestHighLevelClient elasticsearchClient(){
ClientConfiguration configuration = ClientConfiguration.builder()
.connectedTo(serverUrl)
//.connectedTo("192.168.238.128:9200")
//.withConnectTimeout(Duration.ofSeconds(5))
//.withSocketTimeout(Duration.ofSeconds(3))
//.useSsl()
//.withDefaultHeaders(defaultHeaders)
//.withBasicAuth(username, password)
// ... other options
.build();
RestHighLevelClient client = RestClients.create(configuration).rest();
return client;
}
}
实体注解和映射
@ApiModel(value = "商品ES展示")
@Data
@Document(indexName = "es_product_vo",type = "java")
public class ProductEsVO {
@Id
private String uuid;
@ApiModelProperty("商品标题")
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String title ;
/** @see ProductType */
@ApiModelProperty("商品类型uuid")
@Field(type = FieldType.Keyword)
private String type;
@ApiModelProperty("最低价")
private BigDecimal minPrice;
@ApiModelProperty("最高价")
private BigDecimal maxPrice;
@ApiModelProperty("标签")
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String label;
@ApiModelProperty("商品展示图片")
@Field(type = FieldType.Keyword)
private String picture;
@ApiModelProperty("轮播图片(用逗号分隔开)")
@Field(type = FieldType.Keyword)
private String pictures;
@ApiModelProperty("商品详情")
private Blob detail;
@ApiModelProperty("总销量")
@Field(type = FieldType.Keyword)
private Integer totalSale;
@ApiModelProperty(value = "商品类型名称",hidden = true)
@Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
private String typeName;
依赖注入
@Repository
public interface ProductRepository extends ElasticsearchRepository<ProductEsVO,String> {
/**
* 根据标题查询文章列表
* @param title
* @param esPage
* @return
*/
public List<ProductEsVO> findByTitle(String title, Pageable esPage);
}
到这就完成了,有喜欢的小伙伴们可以点个赞,也可以关注下,里面还有其他干货可以看看。