# esUsingtools **Repository Path**: open-source-project---es-query/esUsingtools ## Basic Information - **Project Name**: esUsingtools - **Description**: 基于注解的形式来访问elasticsearch,屏蔽了繁琐的es客户端API,达到在一个方法上标注一个注解即可以访问es esUsingtools 注解另类实现 - **Primary Language**: Java - **License**: MulanPSL-1.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 4 - **Created**: 2022-01-21 - **Last Updated**: 2022-03-29 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # elasticSearch使用工具 #### 使用方法步骤(类似spring cloud fegin的用法) ##### 步骤一: yml引入spring.data.jest.uri=http://xx.xxx.xxx.xxx:9200 ##### 步骤二:查询实体bean标注注解(再多index、多type的场景下该步骤可以省略) @Document(indexName = "[index name]", type = "[type name]", refreshInterval = "-1") @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) ##### 步骤三:在任意接口类上标注EsHandler,该接口类的方法上标注@EsBuildParamTypeInfo ##### 步骤四:springboot启动类上标注@EnableEsProxy("步骤三时接口类所在路径") #### 使用注意点: 1、注意参数名与es存储的字段名对应 即 loanOuterNo -> loan_outer_no √ loanOuterNos -> loan_outer_no × 2、文本字段不能用作排序字段 3、分组聚合统计时需 统计字段名(statisticsFieldName)、统计字段别名(statisticsAnotherName)、操作类型(statisticsType)一一对应 4、分页时需rowOffsetFieldName、pageSizeFieldName成对出现 5、分组聚合统计目前只支持一个字段分组 6、分页参数rowOffsetFieldName与mysql的分页不同,mysql是从指定数据行开始向后查size条数据,而es中是指定第几页查size条数据 #### 示例 ##### 1、普通 @EsBuildParamTypeInfo(resultType = LoanApplyEs.class) LoanApplyEs queryOne(String productCode,String loanOuterNo); @EsBuildParamTypeInfo(resultType = LoanApplyEs.class) List queryByList(String productCode, List loanOuterNo); ##### 2、分页 @EsBuildParamTypeInfo(resultType = RepayApplyEs.class,rangeQueryField = "req_date",fromFieldName = "beginPt",toFieldName = "endPt") Integer countByPt(Map condition); ##### 3、分页+范围查询 @EsBuildParamTypeInfo(resultType = RepayApplyEs.class,orderByField = "req_date",rowOffsetFieldName = "rowOffset",pageSizeFieldName = "pageSize",rangeQueryField = "req_date",fromFieldName = "beginPt",toFieldName = "endPt") List queryAll(Map condition); ##### 4、分组聚合统计 @EsBuildParamTypeInfo(resultType = RepayPlanStatisticsEs.class,rangeQueryField = "agreed_repay_date",fromFieldName = "beginPt",toFieldName = "endPt",groupByFieldName = "agreed_repay_date", statisticsFieldName = {"return_amount","withdraw_amount","due_principal","due_interest","due_charge","paid_principal","paid_interest","paid_charge","base_premium","risk_premium"}, statisticsAnotherName = {"returnTotalAmount","withdrawTotalAmount","dueTotalPrincipal","dueTotalInterest","dueTotalCharge","paidTotalPrincipal","paidTotalInterest","paidTotalCharge","baseTotalPremium","riskTotalPremium"}, statisticsType = {StatisticsTypeEnum.SUM,StatisticsTypeEnum.SUM,StatisticsTypeEnum.SUM,StatisticsTypeEnum.SUM,StatisticsTypeEnum.SUM,StatisticsTypeEnum.SUM,StatisticsTypeEnum.SUM,StatisticsTypeEnum.SUM,StatisticsTypeEnum.SUM,StatisticsTypeEnum.SUM,}) List< RepayPlanStatisticsEs > selectAmountByProductCodeAndFundCode(Map condition); ##### 5、多index、多type查询 @EsBuildParamTypeInfo(resultType = UserAccountEs.class,types = {"user_account","loan_apply"}) List findByProductCodeAndThirdUserNo(@Param("productCode") String productCode, @Param("thirdUserNo") String thirdUserNo); #### 问题: ##### 1、如执行报错:“[match] query does not support [auto_generate_synonyms_phrase_query]”,则是客户端版本与服务端elasticsearch的版本不一致,只需本地重新依赖一致的版本即可