spring boot 集成es分页、条件查询

这里用的es7.8版本,该版本最好和你安装的版本一致

 
        
            org.springframework.boot
            spring-boot-starter-data-elasticsearch
        
        
        
            org.springframework.data
            spring-data-elasticsearch
        
        
            org.elasticsearch
            elasticsearch
            7.8.0
        
        
        
            org.elasticsearch.client
            elasticsearch-rest-high-level-client
            7.8.0
        

对应的实体类,需要注意这里的indexName一定要是小写的


/**
 * 实体类
 */
@Data
@ApiModel(value = "Users对象", description = "实体对象")
//索引名,分片,副本
@Document(indexName = "user",shards = 3,replicas = 1)
public class Users {
    /**
     * 账号
     */
    private String id;


    /**
     * 账号
     */
    @ApiModelProperty(value = "账号", name = "account")
    @Field(type = FieldType.Keyword)//关键词不分词
    private String account;

    /**
     * 用户名
     */
    @ApiModelProperty(value = "用户名", name = "username")
    private String username;

    /**
     * 临时添加的,
     */
    @ApiModelProperty(value = "年龄", name = "age")
    private Integer age;


    /**
     * 密码
     */
    @ApiModelProperty(value = "密码", name = "password")
    @Field(type = FieldType.Keyword,index = false)//关键词不分词,且不作为索引查询
    private String password;

    /**
     * 描述
     */
    @ApiModelProperty(value = "描述", name = "described")
    private String described;

    /**
     * 创建人
     */
    @ApiModelProperty(value = "创建人", name = "cname")
    private String cname;

    /**
     * 逻辑删除
     */
    @ApiModelProperty(value = "逻辑删除", name = "deleted")
    @TableLogic
    @TableField(value = "DELETED", fill = FieldFill.INSERT, jdbcType = JdbcType.VARCHAR)
    private String deleted;

}

一些简单的查询可以直接用ElasticsearchRepository中提供的方法,具体需求可以根据实体类的属性和关键字来拼接


@Repository
public interface EsService extends ElasticsearchRepository  {

    /**
     * 查询用户名为username的用户
     * @param username
     * @return
     */
    List findByUsername(String username);

    /**
     * 查询用户名不是username的所有用户
     * @param username
     * @return
     */
    List findByUsernameNot(String username);



}

如果想要实现分页、排序、多条件查询就需要使用ElasticsearchOperations
(注意:
① ElasticsearchRestTemple是ElasticsearchOperations的子类的子类
② 在ES7.x以下的版本使用的是ElasticsearchTemple,7.x以上版本已弃用ElasticsearchTemple,使用ElasticsearchRestTemple替代

  @Autowired
    private ElasticsearchOperations elasticsearchOperations;
 /**
     * 模糊、分页、排序查询
     * @return
     */
    @ApiOperation(value="模糊、分页、排序查询")
    @GetMapping("getLikeUser")
    public BaseResultModel getLikeUser(Users users,  @RequestParam Integer pageNumber,@RequestParam Integer pageSize) {
        if (pageNumber<0){
            pageNumber=0;
        }
        if (pageSize<=0){
            pageSize=20;
        }
        Pageable pageable = PageRequest.of(pageNumber, pageSize);
        QueryBuilder queryBuilder=null;
        if (users.getAccount()!=null&&users.getAccount().trim().length()>0){
            queryBuilder=QueryBuilders.matchQuery("account",users.getAccount());
        }
        NativeSearchQuery build = new NativeSearchQueryBuilder()
                .withQuery(queryBuilder)
                .withPageable(pageable)
                .build();

        SearchHits search = elasticsearchOperations.search(build, Users.class);
        System.out.println("检索后的总分页数目为:" + search.getTotalHits());
        List> searchHits = search.getSearchHits();

        return BaseResultModel.success(searchHits);
    }


你可能感兴趣的:(spring,elasticsearch,spring,boot,搜索引擎)