spring数据mongodb全文搜索

时间:2017-06-24 18:36:07

标签: spring mongodb spring-data-mongodb

我有域名

@Document
public @Data class Note {

    @Id
    private String noteId;
    private String owner;
    @TextIndexed
    private String name;
    @TextIndexed
    private String text;
    private List<Tag> tags;
    private LocalDate date;

有两个索引字段 - nametext。我想通过这些字段进行全文搜索。

希望搜索条件符合此条件的休息

@GetMapping("/notes-rest/search/custom")
public List<Note> searchNotes(@RequestParam("criteria") String searchCriteria) {
    LOGGER.info("Attempt to perform full text search by criteria: [{}].", searchCriteria);
    return searchRepository.findNotesByCriteria(searchCriteria);
}

我传递这样的行

localhost:8080/notes-rest/search/custom?criteria={'name':'1234', 'text': 'trx'}

执行全文搜索的存储库方法如下所示

@Repository
public class SearchRepository implements ISearchRepository {

    @Autowired
    private MongoTemplate template;

    @Override
    public List<Note> findNotesByCriteria(String searchCriteria) {
        TextCriteria criteria = TextCriteria.forDefaultLanguage().matching(searchCriteria);
        Query query = TextQuery.queryText(criteria);
        return template.find(query, Note.class);
    }
}

但无论我传递什么,它总是返回数据库中的整个数据

[
    {
        "noteId": "594eae353bc51592f432b62e",
        "owner": "xxx",
        "name": "xxx note",
        "text": "My text",
        "tags": [
            {
                "tagId": null,
                "tag": "basic2"
            }
        ],
        "date": [
            1997,
            12,
            15
        ]
    },
    {
        "noteId": "594ea38a3bc5150070d0aed2",
        "owner": "system",
        "name": "system note",
        "text": "My text",
        "tags": [
            {
                "tagId": "594e629b3bc5150f4c869905",
                "tag": "basic"
            }
        ],
        "date": [
            1997,
            12,
            15
        ]
    },
    {
        "noteId": "594ea0ae3bc5150ad88a1dfc",
        "owner": "borland",
        "name": "borland note",
        "text": "My text",
        "tags": null,
        "date": [
            1992,
            12,
            15
        ]
    }
]

有什么问题?

1 个答案:

答案 0 :(得分:0)

我将搜索条件的格式更改为{name:"borl", text: "t"},似乎它开始正常工作