【问题标题】:Can MongoDBs full text search be used with Spring Data REST?MongoDB 的全文搜索可以与 Spring Data REST 一起使用吗?
【发布时间】:2017-06-04 03:48:38
【问题描述】:

我刚开始使用 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 编写转换器?

【问题讨论】:

    标签: mongodb spring-data-rest


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 是的,有没有例子如何编写这样的转换器?
      • 当然。谷歌将帮助找到它。但是通过某个类包装内容存储库会更容易
      • 当然,我可以用谷歌搜索,但我的问题更多是关于通过 REST API 访问 TextIndexed 字段的默认方式。 Indexed 字段的其他查找器方法是开箱即用的。编写转换器将是第二种选择
      • docs.spring.io/spring/docs/current/spring-framework-reference/… - 关于注册自定义属性编辑器的部分是一个好的开始。
      猜你喜欢
      • 2014-10-02
      • 1970-01-01
      • 2017-11-07
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      相关资源
      最近更新 更多