elasticsearch学习1—springboot整合:基本使用,分页

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 15:47   2869   0

1、创建springboot项目,pom依赖如下:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>

    <!--es依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
<!--使用阿里云仓库-->
<repositories>
    <repository>
        <id>aliyun-repos</id>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>aliyun-plugin</id>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </pluginRepository>
</pluginRepositories>

二、接口:UserDao

/**
 * @author: windy
 * @create: 2019-11-30 15:51
 */
public interface UserDao extends ElasticsearchRepository<User, String> {
}

三、实体类

/**
 * @author: windy
 * @create: 2019-11-30 15:47
 */
@Document(indexName = "info",type = "user")
@Data
public class User {

    @Id
    private String id;

    private String name;

    private Integer age;

    private Integer sex;
}

四、controller

@Controller
public class EsController {

    @Autowired
    private UserDao userDao;

    @ResponseBody
    @RequestMapping("add")
    public User addUser(@RequestBody User user){
       return userDao.save(user);
    }

    @ResponseBody
    @RequestMapping("findById")
    public Optional<User> findById(String id){
        return userDao.findById(id);
    }

    /**
     * 分页
     */
    @ResponseBody
    @RequestMapping("/search")
    public Page<User> search(String keyWord, @PageableDefault(page = 0, value = 3) Pageable pageable) {
        // 查询所有的
        BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
        if (!StringUtils.isEmpty(keyWord)) {
            // 模糊查询 一定要ik中文
            MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", keyWord);
            boolQuery.must(matchQuery);
        }
        Page<User> page = userDao.search(boolQuery, pageable);
        return page;
    }
}

五、启动类:

@SpringBootApplication
@EnableElasticsearchRepositories(basePackages = "com.windy.springbootes.repository")
public class SpringbootEsApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootEsApplication.class, args);
    }

}

六、利用postman测试

1、添加数据

2、分页查询,size=3

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP