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开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

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

[外链图片转存中…(img-TACfkt18-1715670600713)]

[外链图片转存中…(img-NYUHQZyg-1715670600713)]

[外链图片转存中…(img-cYH5I6zt-1715670600714)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

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

  • 17
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值