elasticsearch导入数据

1.创建应该springboot工程

2.引入依赖

  <!-- elasticsearch -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

3.yml文件

spring:
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: ip:9300

4.引导类

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SearchService {

    public static void main(String[] args) {
        SpringApplication.run(SearchService.class, args);
    }
}

5.创建引入es的实体类

@Document(indexName = "goods", type = "docs", shards = 1, replicas = 0)
public class Goods {
    @Id
    private Long id; // 编号
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String all; // 所有需要被搜索的信息
    @Field(type = FieldType.Keyword, index = false)
    private String subTitle;// 标题
    @Field(type = FieldType.Keyword, index = false)
    private String skus;// List<sku>信息的json结构
    //get。set
}

6。goodsrespostory

public interface GoodsRepository extends ElasticsearchRepository<Goods, Long> {
}

7.创建索引

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SearchApplication.class)
public class ElasticsearchTest {

    @Autowired
    private GoodsReponsitory goodsReponsitory;

    @Autowired
    private ElasticsearchTemplate template;

   @Test
    public void createIndex(){
        // 创建索引
        this.elasticsearchTemplate.createIndex(Goods.class);
        // 配置映射
        this.elasticsearchTemplate.putMapping(Goods.class);

        this.goodsReponsitory.saveAll(goodsList);//从数据库获取的所有数据,在这里需要分页查询,以免数据量过大
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值