ElasticSearch入门(六)SpringBoot2

private String author;

@Field(name = “word_count”, type = FieldType.Integer)

private Integer wordCount;

/**

    1. Jackson日期时间序列化问题:
  • Cannot deserialize value of type java.time.LocalDateTime from String “2020-06-04 15:07:54”: Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text ‘2020-06-04 15:07:54’ could not be parsed at index 10

  • 解决:@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”)

    1. 日期在ES存为long类型
  • 解决:需要加format = DateFormat.custom

    1. java.time.DateTimeException: Unable to obtain LocalDate from TemporalAccessor: {DayOfMonth=5, YearOfEra=2020, MonthOfYear=6},ISO of type java.time.format.Parsed
  • 解决:pattern = “uuuu-MM-dd HH:mm:ss” 即将yyyy改为uuuu,或8uuuu: pattern = “8uuuu-MM-dd HH:mm:ss”

  • 参考:https://www.elastic.co/guide/en/elasticsearch/reference/current/migrate-to-java-time.html#java-time-migration-incompatible-date-formats

*/

@JsonFormat(pattern = “yyyy-MM-dd HH:mm:ss”)

@Field(name = “publish_date”, type = FieldType.Date, format = DateFormat.custom, pattern = “uuuu-MM-dd HH:mm:ss”)

private LocalDateTime publishDate;

}

核心方法

package com.heartsuit.repository;

import java.util.List;

import com.heartsuit.domain.Book;

import org.springframework.data.elasticsearch.annotations.Query;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

/**

  • @Author Heartsuit

  • @Date 2020-06-12

*/

public interface BookRepository extends ElasticsearchRepository<Book, String> {

List findByAuthor(String author);

List findByAuthorLike(String author);

List findByTitle(String author);

List findByWordCount(Integer wordCount);

List findByAuthorAndTitle(String author, String title);

@Query(“{“bool” : {“must” : {“match” : {“title” : “?0”}}}}”)

List queryByTitle(String keyword);

}

测试接口

package com.heartsuit.controller;

import com.heartsuit.domain.Book;

import com.heartsuit.repository.BookRepository;

import lombok.extern.slf4j.Slf4j;

import org.springframework.format.annotation.DateTimeFormat;

import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

import java.util.*;

/**

  • @Author Heartsuit

  • @Date 2020-06-12

*/

@RestController

@RequestMapping(“/books”)

@Slf4j

public class BookController {

private final BookRepository bookRepository;

public BookController(BookRepository bookRepository) {

this.bookRepository = bookRepository;

}

@GetMapping(“/all”)

public List findAll() {

Iterable result = bookRepository.findAll();

Iterator res = result.iterator();

List books = new ArrayList<>();

while (res.hasNext()) {

books.add(res.next());

}

log.info(“List All, Size: {}”, books.size());

return books;

}

/**

  • 新增

  • @param book

  • @return

*/

@PostMapping(“/”)

public Book create(@RequestBody Book book) {

log.info(“Saved OK: {}”, book.getTitle());

return bookRepository.save(book);

}

/**

  • 根据ID查询

  • @param id

  • @return

*/

@GetMapping(“/{id}”)

public Book findById(@PathVariable(“id”) String id) {

log.info(“Query ID: {}”, id);

Book orElse = bookRepository.findById(id).orElse(null);

return orElse;

}

/**

  • 根据ID修改

  • @param id

  • @param title

  • @param author

  • @param wordCount

  • @param publishDate

  • @return

  • Note: 报错:Failed to convert value of type ‘java.lang.String’ to required type ‘java.time.LocalDateTime’;

  • 解决:在参数前添加注解:@DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”)

*/

@PutMapping(“/{id}”)

public Book update(@PathVariable(“id”) String id,

@RequestParam(name = “title”, required = false) String title,

@RequestParam(name = “author”, required = false) String author,

@RequestParam(name = “wordCount”, required = false) Integer wordCount,

@RequestParam(name = “publishDate”, required = false) @DateTimeFormat(pattern = “yyyy-MM-dd HH:mm:ss”) LocalDateTime publishDate) {

Optional optional = bookRepository.findById(id);

if (optional.isPresent()) {

Book book = optional.get();

if (title != null) {

book.setTitle(title);

}

if (author != null) {

book.setAuthor(author);

}

if (wordCount != null) {

book.setWordCount(wordCount);

}

if (publishDate != null) {

book.setPublishDate(publishDate);

}

return bookRepository.save(book);

}

return null;

}

/**

  • 根据ID删除

  • @param id

*/

@DeleteMapping(“/{id}”)

public void delete(@PathVariable(“id”) String id) {

log.info(“Deleted ID: {}”, id);

bookRepository.deleteById(id);

}

/**

  • 删除所有

*/

@PostMapping(“/clear”)

public void clear() {

log.info(“Delete All!”);

bookRepository.deleteAll();

}

/**

  • 根据作者查询

  • @param author

  • @return

*/

@PostMapping(“/author”)

public List byAuthor(String author) {

log.info(“By Author”);

return bookRepository.findByAuthor(author);

}

/**

  • 根据作者检索

  • @param author

  • @return

*/

@PostMapping(“/author/like”)

public List byAuthorLike(String author) {

log.info(“By Author Like”);

return bookRepository.findByAuthorLike(author);

}

/**

  • 根据标题查询

  • @param title

  • @return

*/

@PostMapping(“/title”)

public List byTitle(String title) {

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Java)

总结

谈到面试,其实说白了就是刷题刷题刷题,天天作死的刷。。。。。

为了准备这个“金三银四”的春招,狂刷一个月的题,狂补超多的漏洞知识,像这次美团面试问的算法、数据库、Redis、设计模式等这些题目都是我刷到过的

并且我也将自己刷的题全部整理成了PDF或者Word文档(含详细答案解析)

我的美团offer凉凉了?开发工程师(Java岗)三面结束等通知...

66个Java面试知识点

架构专题(MySQL,Java,Redis,线程,并发,设计模式,Nginx,Linux,框架,微服务等)+大厂面试题详解(百度,阿里,腾讯,华为,迅雷,网易,中兴,北京中软等)

我的美团offer凉凉了?开发工程师(Java岗)三面结束等通知...

算法刷题(PDF)

我的美团offer凉凉了?开发工程师(Java岗)三面结束等通知...

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
、设计模式等这些题目都是我刷到过的

并且我也将自己刷的题全部整理成了PDF或者Word文档(含详细答案解析)

[外链图片转存中…(img-PvcKLeHn-1713838410582)]

66个Java面试知识点

架构专题(MySQL,Java,Redis,线程,并发,设计模式,Nginx,Linux,框架,微服务等)+大厂面试题详解(百度,阿里,腾讯,华为,迅雷,网易,中兴,北京中软等)

[外链图片转存中…(img-xpzIwu7R-1713838410582)]

算法刷题(PDF)

[外链图片转存中…(img-rSjbVyHd-1713838410582)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 7
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值