springboot整合ElasticSearch 某个属性不存储到ES

springboot整合ElasticSearch 某个属性不存储到ES

在需要忽略的字段上加上 @Transient (org.springframework.data.annotation.Transient)即可

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;

@Document(indexName = "shop", type = "sku")
public class SkuIndex {

    @Id
    private Integer skuId;
    
    /**
     * 图片列表
     */
     @Transient
    private List<SkuImgIndex> skuImgList;
    
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot整合Elasticsearch可以使用Spring Data Elasticsearch,在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> ``` 在application.properties配置文件中添加以下配置: ``` spring.data.elasticsearch.repositories.enabled=true spring.data.elasticsearch.cluster-name=my-application spring.data.elasticsearch.cluster-nodes=localhost:9300 ``` 其中,cluster-name为Elasticsearch集群名称,cluster-nodesElasticsearch集群节点地址。 接下来,定义一个实体类,并使用注解标注该实体类对应的索引和类型: ```java @Document(indexName = "article", type = "news") public class Article { @Id private Long id; @Field(analyzer = "ik_max_word", searchAnalyzer = "ik_max_word") private String title; @Field(analyzer = "ik_max_word", searchAnalyzer = "ik_max_word") private String content; // getter/setter } ``` 在定义完实体类后,可以使用ElasticsearchRepository接口定义数据访问层的方法: ```java public interface ArticleRepository extends ElasticsearchRepository<Article, Long> { List<Article> findByTitle(String title); } ``` 在服务层中调用ArticleRepository的方法即可进行数据的增删改查操作: ```java @Service public class ArticleService { @Autowired private ArticleRepository articleRepository; public void save(Article article) { articleRepository.save(article); } public void delete(Long id) { articleRepository.deleteById(id); } public Article getById(Long id) { Optional<Article> optional = articleRepository.findById(id); return optional.isPresent() ? optional.get() : null; } public List<Article> getByTitle(String title) { return articleRepository.findByTitle(title); } } ``` 以上就是Spring Boot整合Elasticsearch的基本流程。需要注意的是,在使用Spring Data Elasticsearch时,需要根据实际情况配置相应的注解和属性,以保证数据的正确存储和检索。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值