Spring Data REST 可以使用 MongoDB 的全文搜索吗?

Can MongoDBs full text search be used with Spring Data REST?

我刚开始使用 Spring Data Rest 并想公开一个字段的查找器,该字段是在 MongoDB 中索引的文本。我有以下索引定义:

@TextIndexed
private String title;

并验证索引已创建。我在存储库定义上创建了一个查找器方法:

public interface ContentRespository extends MongoRepository<Content, String> {
    public Page<Content> findByTitle(@Param("title") TextCriteria title, @Param("pageable") Pageable pageable);
}

通过调用 REST API URL:

http://localhost:8080/contents/search/findByTitle?title=test

我收到以下错误:

2017-01-19 13:16:41.831 ERROR 16705 --- [nio-8080-exec-9] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.data.repository.support.QueryMethodParameterConversionException: Failed to convert test into org.springframework.data.mongodb.core.query.TextCriteria!] with root cause

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.String] to type [@org.springframework.data.repository.query.Param  org.springframework.data.mongodb.core.query.TextCriteria]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:324) ~[spring-core-4.3.4.RELEASE.jar:4.3.4.RELEASE]
....

调用 REST API 的正确方法是什么?我找不到任何关于它的文档。或者如何为 TextCriteria 编写转换器?

与 mongo 无关 - 但 spring rest 告诉你它不能将传入的字符串(来自你的网络请求)转换为公开的方法参数。您有 2 个选择 - 创建并注册转换器或包装方法,然后自己将字符串转换为所需类型

一段时间后,我回到这个问题并通过使用 @Query 符号定义查找器找到了答案:

@Query("{$text: {$search: ?0}}")
public Page<Content> findByTitle(@Param("title") String title, @Param("pageable") Pageable pageable);