pom引入
<!--ElasticSearch-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
yml配置
#elasticsearch
spring:
data:
elasticsearch:
cluster-name: my-elasticsearch
cluster-nodes: 127.0.0.1:9300,127.0.0.1:9301,127.0.0.1:9302
实体
@Document(indexName = "blog4", type = "article")
public class UserEntity {
@Id
@Field(type = FieldType.Long, store = true)
private long id;
@Field(type = FieldType.Text, store = true, analyzer = "ik_max_word")
private String title;
@Field(type = FieldType.Text, store = true, analyzer = "ik_max_word")
private String content;
……
使用
@Autowired
private UserRepository userRepository;
@Autowired
private ElasticsearchTemplate elasticsearchTemplate;
@Test
public void createIndex() {
elasticsearchTemplate.createIndex(UserEntity.class);
elasticsearchTemplate.putMapping(UserEntity.class);
}
@Test
public void testUserRepository() {
UserEntity user = new UserEntity();
user.setId(1);
user.setTitle("zhangsan");
user.setContent("123");
userRepository.save(user);
}
如果报错(实例化失败),添加运行时参数
-Des.set.netty.runtime.available.processors=false