使用JHIPSTER / SPRING DATA MONGODB进行MongoDB全文搜索

时间:2017-03-02 22:17:51

标签: spring mongodb spring-data jhipster spring-data-mongodb

我正在尝试使用最新版本的jhîpster(4.0.7)和spring数据mongodb进行一些全文查询。

我创建了一个扩展CrudRepository的存储库:

public interface PublicSearchRepository extends CrudRepository<Transcriptionrequest, String> {

  Page<Transcriptionrequest> findBy(TextCriteria textCriteria, Pageable pageable);

并按如下方式应用查询:

TextCriteria textCriteria = TextCriteria.forDefaultLanguage().caseSensitive(Boolean.FALSE);
        textCriteria.matching(query);

Page<Transcriptionrequest> page = publicSearchRepository.findBy(textCriteria, pageable);

我的对象使用注释

编制索引
@Document(collection = "transcriptionrequest")
public class Transcriptionrequest implements Serializable {

    private static final long serialVersionUID = 1L;

    @TextScore
    private Float score;

    @Id
    private String id;

    @Field("request_id")
    private String request_id;

    @NotNull
    @Field("song_name")
    @TextIndexed(weight = 2)
    private String song_name;

当我尝试搜索时,我总是有这个错误:

org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 4 and error message 'Missing expected field "$search"' on server localhost:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 4 and error message 'Missing expected field "$search"' on server localhost:27017
    at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:107)
    at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2134)
    at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1977)
    at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1783)
    at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1766)
    at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:640)

当我提高mongo类的日志级别时,我有查询。永远不会发送搜索字段。

Created query Query: { }, Fields: null, Sort: null
2017-03-02 22:53:45.625 DEBUG 6428 --- [ XNIO-2 task-20] o.s.data.mongodb.core.MongoTemplate      : find using query: { "$text" : { "$caseSensitive" : false}} fields: { "score" : { "$meta" : "textScore"}} for class: class info.touret.songtranscriptmarket.domain.Transcriptionrequest in collection: transcriptionrequest
2017-03-02 22:53:45.626 DEBUG 6428 --- [ XNIO-2 task-20] o.s.data.mongodb.core.MongoDbUtils       : Getting Mongo Database name=[songtranscriptmarket]

我该如何解决?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我的查询等于null。 在这种情况下,框架不会填充$ search属性。