如何在 Spring 中搜索名称中的一个词而不是全名?
How to make a search in Spring look for one word in the name and not for the full name?
编写了 Spring 网站的搜索。搜索按标题搜索文章,但它只搜索确切的标题,即使在文章标题中输入了搜索词,我也需要它才能找到它。比如我有一篇文章“地球为什么是圆的?”,现在你输入“地球为什么是圆的?”他就会找到这篇文章。在搜索中,但如果你只输入“为什么”这个词,他将找不到任何东西。请告诉我如何才能找到标题中的单词。
我的资料库
public interface PostRepository extends JpaRepository<Post, Long> {
Iterable<Post> findByTitle(String title) throws Exception;
}
我的服务是为这个存储库编写的(顺便说一句,告诉我好吧,我把它抽象化是为了在其中只实现 findByTitle?)
@Service
public abstract class PostService implements PostRepository {
public PostRepository postRepository;
@Override
public Iterable<Post> findByTitle(String title) throws Exception {
Iterable<Post> searchResult = postRepository.findByTitle(title);
if(searchResult != null){
throw new Exception("Пост не найден");
}
return searchResult;
}
}
我的控制器
@Controller
public class SearchController {
@Autowired
PostRepository postRepository;
@GetMapping("/search")
public String searchPage(){
return "/search";
}
@PostMapping("search")
public String searchPage(@RequestParam("searchString") String searchString, Model model){
if(searchString != null){
try {
Iterable<Post> searchResult = postRepository.findByTitle(searchString);
model.addAttribute("searchResult", searchResult);
} catch (Exception e) {
e.printStackTrace();
}
}
return "search";
}
}
如果您有一条值为“aaabbbccc”的记录,并且您希望在搜索 bc
时返回该记录,那么您需要使用 containing。
Containing
将您的搜索字符串包装在 %%
中,结果为 like %bc%
.
您还可以使用 IgnoreCase 来 - 是的 - 忽略大小写 ;)
所以你需要使用:
postRepository.findByTitleContainingIgnoreCase(searchString);
编辑:
有关更多详细信息,将搜索字符串包装在“%searchString%”中意味着:忽略搜索字符串前后的所有内容,以便搜索字符串可以位于字符串中的某个位置。
编写了 Spring 网站的搜索。搜索按标题搜索文章,但它只搜索确切的标题,即使在文章标题中输入了搜索词,我也需要它才能找到它。比如我有一篇文章“地球为什么是圆的?”,现在你输入“地球为什么是圆的?”他就会找到这篇文章。在搜索中,但如果你只输入“为什么”这个词,他将找不到任何东西。请告诉我如何才能找到标题中的单词。
我的资料库
public interface PostRepository extends JpaRepository<Post, Long> {
Iterable<Post> findByTitle(String title) throws Exception;
}
我的服务是为这个存储库编写的(顺便说一句,告诉我好吧,我把它抽象化是为了在其中只实现 findByTitle?)
@Service
public abstract class PostService implements PostRepository {
public PostRepository postRepository;
@Override
public Iterable<Post> findByTitle(String title) throws Exception {
Iterable<Post> searchResult = postRepository.findByTitle(title);
if(searchResult != null){
throw new Exception("Пост не найден");
}
return searchResult;
}
}
我的控制器
@Controller
public class SearchController {
@Autowired
PostRepository postRepository;
@GetMapping("/search")
public String searchPage(){
return "/search";
}
@PostMapping("search")
public String searchPage(@RequestParam("searchString") String searchString, Model model){
if(searchString != null){
try {
Iterable<Post> searchResult = postRepository.findByTitle(searchString);
model.addAttribute("searchResult", searchResult);
} catch (Exception e) {
e.printStackTrace();
}
}
return "search";
}
}
如果您有一条值为“aaabbbccc”的记录,并且您希望在搜索 bc
时返回该记录,那么您需要使用 containing。
Containing
将您的搜索字符串包装在 %%
中,结果为 like %bc%
.
您还可以使用 IgnoreCase 来 - 是的 - 忽略大小写 ;)
所以你需要使用:
postRepository.findByTitleContainingIgnoreCase(searchString);
编辑: 有关更多详细信息,将搜索字符串包装在“%searchString%”中意味着:忽略搜索字符串前后的所有内容,以便搜索字符串可以位于字符串中的某个位置。