楼主使用es版本为7.6,使用的spring boot提供的start
1.pom文件
<!-- elasticsearch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
2.application.yml
spring: elasticsearch: rest: uris: ip:9200 username: xxxx password: xxxx data: elasticsearch: repositories: enabled: true client: reactive: # username: xxx 这种写法是不对的 # password: xxx 这种写法是不对的 # endpoints: ip:9200 这种写法是不对的 use-ssl: false
注意:
1.配置es地址要写在楼主这个位置,否则会默认为localhost:9200
2.与旧版本的配置不同 开启的端口号为9200
3.config文件
@Configuration public class ElasticSearchConfig { /** * 防止netty的bug * java.lang.IllegalStateException: availableProcessors is already set to [4], rejecting [4] */ @PostConstruct void init() { System.setProperty("es.set.netty.runtime.available.processors", "false"); } }
4.接口编写
@Autowired ElasticsearchRestTemplate elasticsearchTemplate; @Log(operationName = "日志-查询登录日志") @PostMapping("/selectLoginLog") public Result<List<LoginLogVO>> selectLoginLog(@RequestBody LoginLogInputVO loginLogInputVO) throws ParseException { // 日期格式化 SimpleDateFormat sd = new SimpleDateFormat(DateFormatEnum.YYYY_MM_DD_HH_MM_SS.getFormat()); // 返回对象 Result<List<LoginLogVO>> result = new Result(); NativeSearchQueryBuilder builder = new NativeSearchQueryBuilder(); BoolQueryBuilder bool = QueryBuilders.boolQuery(); // 用户名不为空 if (!CommonUtil.isEmpty(loginLogInputVO.getUserName())) { bool.must(QueryBuilders.wildcardQuery("userName", "*" + loginLogInputVO.getUserName() + "*")); } // 时间不为空 if (!CommonUtil.isEmpty(loginLogInputVO.getBeginTime()) && !CommonUtil.isEmpty(loginLogInputVO.getEndTime())) { List<QueryBuilder> filters = bool.filter(); filters.add(QueryBuilders.rangeQuery("time") .gte(sd.parse(loginLogInputVO.getBeginTime() + DateFormatEnum.BEGIN_HH_MM_SS.getFormat())) .lte(sd.parse(loginLogInputVO.getEndTime() + DateFormatEnum.END_HH_MM_SS.getFormat()))); } // 分页查询 if (!CommonUtil.isEmpty(loginLogInputVO.getPageSize()) && !CommonUtil.isEmpty(loginLogInputVO.getPageNum())) { // 从第0页开始 builder.withPageable(PageRequest.of(loginLogInputVO.getPageNum() - 1, loginLogInputVO.getPageSize())); } builder.withSort(SortBuilders.fieldSort("time").order(SortOrder.ASC)); // 构造查询条件 builder.withQuery(bool); NativeSearchQuery query = builder.build(); IndexCoordinates indexCoordinates = IndexCoordinates.of("XXXX-*"); SearchHits<LoginLogVO> resultIter = elasticsearchTemplate.search(query, LoginLogVO.class, indexCoordinates); // 格式化输出 List<LoginLogVO> resultList = new ArrayList<>(); List<SearchHit<LoginLogVO>> SearchHitList = resultIter.getSearchHits(); for (SearchHit<LoginLogVO> loginLogVOSearchHit : SearchHitList) { resultList.add(loginLogVOSearchHit.getContent()); } // 分页返回 if (!CommonUtil.isEmpty(loginLogInputVO.getPageSize()) && !CommonUtil.isEmpty(loginLogInputVO.getPageNum())) { result.setTotal(resultIter.getTotalHits()); result.setPageNum(loginLogInputVO.getPageNum()); result.setPageSize(loginLogInputVO.getPageSize()); } result.setData(resultList); return result; }
5.POJO
因为楼主前后端约定的时间类型为string,所以需要转换类型然后传回。
@Document(indexName = "loginlog-*", shards = 1, replicas = 0) public class LoginLogVO { @Id private String id; /** * 信息 */ @Field(type = FieldType.Keyword, analyzer = "ik_max_word") private String message; @Field(type = FieldType.Date, store = true, format = DateFormat.date_time) private Date time; } public class LoginLogInputVO extends BaseEntity { /** * 用户账号 */ private String userName; /** * 开始时间 */ private String beginTime; /** * 结束时间 */ private String endTime; } public class LoginLogOutputVO extends BaseEntity { private String id; /** * 信息 */ private String message; /** * 执行时间 */ private String time; }