elasticsearch导入数据
1.创建应该springboot工程
2.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
1
2
3
4
5
3.yml文件
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: ip:9300
1
2
3
4
5
4.引导类
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class SearchService {
public static void main(String[] args) {
SpringApplication.run(SearchService.class, args);
}
}
1
2
3
4
5
6
7
8
9
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信息的json结构
//get。set
}
1
2
3
4
5
6
7
8
9
10
11
12
6。goodsrespostory
public interface GoodsRepository extends ElasticsearchRepository<Goods, Long> {
}
1
2
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);//从数据库获取的所有数据,在这里需要分页查询,以免数据量过大
}
}