import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.time.LocalDateTime;
/**
-
@Author Heartsuit
-
@Date 2020-06-12
*/
@Data
//@Document(indexName = "book", useServerConfiguration = true, createIndex = false)
@Document(indexName = "book")
public class Book {
@Id
private String id;
@Field(type = FieldType.Text)
private String title;
@Field(type = FieldType.Keyword, analyzer = "ik_smart", searchAnalyzer = "ik_smart")
private String author;
@Field(name = "word_count", type = FieldType.Integer)
private Integer wordCount;
/**
-
- Jackson日期时间序列化问题:
-
Cannot deserialize value of type
java.time.LocalDateTimefrom 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")
-
- 日期在ES存为long类型
-
解决:需要加format = DateFormat.custom
-
- 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"
*/
@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")
最后
看完上述知识点如果你深感Java基础不够扎实,或者刷题刷的不够、知识不全面
小编专门为你量身定制了一套<Java一线大厂高岗面试题解析合集:JAVA基础-中级-高级面试+SSM框架+分布式+性能调优+微服务+并发编程+网络+设计模式+数据结构与算法>
针对知识面不够,也莫慌!还有一整套的<Java核心进阶手册>,可以瞬间查漏补缺
全都是一丢一丢的收集整理纯手打出来的
更有纯手绘的各大知识体系大纲,可供梳理:Java筑基、MySQL、Redis、并发编程、Spring、分布式高性能架构知识、微服务架构知识、开源框架知识点等等的xmind手绘图~
相关阅读docs.qq.com/doc/DSmxTbFJ1cmN1R2dB