pom引入

    1. <!--ElasticSearch-->
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    5. </dependency>

    yml配置

    1. #elasticsearch
    2. spring:
    3. data:
    4. elasticsearch:
    5. cluster-name: my-elasticsearch
    6. cluster-nodes: 127.0.0.1:9300,127.0.0.1:9301,127.0.0.1:9302

    实体

    1. @Document(indexName = "blog4", type = "article")
    2. public class UserEntity {
    3. @Id
    4. @Field(type = FieldType.Long, store = true)
    5. private long id;
    6. @Field(type = FieldType.Text, store = true, analyzer = "ik_max_word")
    7. private String title;
    8. @Field(type = FieldType.Text, store = true, analyzer = "ik_max_word")
    9. private String content;
    10. ……

    使用

    1. @Autowired
    2. private UserRepository userRepository;
    3. @Autowired
    4. private ElasticsearchTemplate elasticsearchTemplate;
    5. @Test
    6. public void createIndex() {
    7. elasticsearchTemplate.createIndex(UserEntity.class);
    8. elasticsearchTemplate.putMapping(UserEntity.class);
    9. }
    10. @Test
    11. public void testUserRepository() {
    12. UserEntity user = new UserEntity();
    13. user.setId(1);
    14. user.setTitle("zhangsan");
    15. user.setContent("123");
    16. userRepository.save(user);
    17. }

    如果报错(实例化失败),添加运行时参数

    1. -Des.set.netty.runtime.available.processors=false