如何将Spring Data MongoDB Query与全文搜索结合起来?

mor*_*wed 4 search full-text-search spring-data-mongodb

我最近升级到Spring Data MongoDB 1.6.0.RC1,非常好,并实现了MongoDB 2.6(huuurah!)的全文搜索功能.我的问题是:我如何组合Criteria和TextCriteria类来生成复杂的查询?

示例对象模型:

{
  textProperty: "a text that is indexed for full text search",
  language:"en",
  aBooleanProperty: true,
  anIntegerProperty: 1
}
Run Code Online (Sandbox Code Playgroud)

查询:

db.collection({ anIntegerProperty: 1,  $text: { $search: "indexed", $language: "en" } })
Run Code Online (Sandbox Code Playgroud)

那么,如何使用Spring Data MongoDB类编写上述查询?

Oli*_*ohm 8

注意:此帖子之前有非工作代码,但现在已修复!

A TextCriteria是a CriteriaDefinition,因此可以Query像这样使用:

Quer query = Query.query(
  Criteria.where("aBooleanProperty").is(true).
  and(anIntegerProperty).is(1)).
  addCriteria(TextCriteria.
    forLanguage("en"). // effectively the same as forDefaultLanguage() here
    matching("a text that is indexed for full text search")));

List<YourDocumentType> result = mongoTemplate.findAll(query. YourDocumentType.class);
Run Code Online (Sandbox Code Playgroud)

通过使用静态导入来修改它Query.query,Criteria.where并且它读起来非常流畅.