pom引入
<!--ElasticSearch--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>
yml配置
#elasticsearchspring:data:elasticsearch:cluster-name: my-elasticsearchcluster-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;……
使用
@Autowiredprivate UserRepository userRepository;@Autowiredprivate ElasticsearchTemplate elasticsearchTemplate;@Testpublic void createIndex() {elasticsearchTemplate.createIndex(UserEntity.class);elasticsearchTemplate.putMapping(UserEntity.class);}@Testpublic 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
