Springdata集成ElasticSearch—增删改查

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

环境配置

maven

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.6.RELEASE</version>
        <relativePath/>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-test</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
 <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-configuration-processor</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

properties

application.properties

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

RunApp

image-20220302135505589

代码配置

bean.Product

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

image-20220302135652993

config.EsConfig

image-20220302135840034

dao.productDao

image-20220302140004378

使用

测试类注意事项

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

示例如下:

image-20220302140223424

创建索引

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

image-20220302140231320

增删改查

@Autowird 注入ProductDao

image-20220302140329868

创建

image-20220302140345254

修改

image-20220302140453308

根据id查询

image-20220302140504131

查询所有

image-20220302140515247

删除

image-20220302140523835

批量新增

image-20220302140536531

分页查询

image-20220302140550468

文档搜索-条件查询

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

posted @ 2022-03-02 14:08  yangruomao  阅读(200)  评论(0编辑  收藏  举报