Springdata集成ElasticSearch—增删改查


该示例以product 为JavaBean对象,使用springdata集成ES,用productDao对ES进行方便的增删改查

环境配置

maven

    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.6.RELEASE
        
    
    
        
            org.projectlombok
            lombok
        
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-test
        
        
            junit
            junit
        
        
            org.springframework
            spring-test
        

        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
    
 
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.springframework.boot
                            spring-boot-configuration-processor
                        
                    
                
            
        
    

properties

application.properties

# elasticsearch 主机和端口
elasticsearch.host=127.0.0.1
elasticsearch.port=9200
logging.level.com.atguigu.es=debug

RunApp

代码配置

bean.Product

前四个注解是lombok注解,用于更方便创建JavaBean

config.EsConfig

dao.productDao

使用

测试类注意事项

不知道为什么,测试类一定要放在java包里,并且添加@RunWith(SpringRunner.class)注解才有效

示例如下:

创建索引

第一次运行程序会自动创建索引

增删改查

@Autowird 注入ProductDao

创建

修改

根据id查询

查询所有

删除

批量新增

分页查询

文档搜索-条件查询

文档搜索-条件查询+分页