【发布时间】:2015-08-28 09:19:53
【问题描述】:
我需要在名为 blog 的集合中搜索文档,该集合具有为标题、标签、摘要和正文定义的文本索引:
@Document(collection="blog")
public class Blog {
@Id
private String id;
@TextIndexed(weight = 10)
private String title;
@TextIndexed(weight = 9)
private String tags;
@TextIndexed(weight = 8)
private String summary;
@TextIndexed(weight = 7)
private String body;
@TextScore
private Float score;
//getters and setters
}
现在,我需要根据以下条件对 blog 集合执行文本搜索:
- 检查用户输入是否包含多个单词。
- 如果 searchKey 是单个词,则执行文本搜索并根据权重返回排序后的响应。
- 如果 searchKey 包含多个单词,则搜索完整的 PHRASE 或 PHRASE 中的任何单词。
对于第二种情况,TextCriteria 定义如下:
TextCriteria criteria = TextCriteria.forDefaultLanguage().matching("SingleWord");
对于第三种情况,如何在单个查询中为组合编写条件定义:
query 1: db.articles.find( { $text: { $search: "\"coffee cake\"" } } ) //phrase search
query 2: db.articles.find( { $text: { $search: "coffee cake" } } ) //word search
我可以用
进行搜索吗query 1 OR query 2 with sorted result based on score.
匹配完整短语的结果得分应该更高。
【问题讨论】:
-
你能找到任何解决方案吗?
标签: java mongodb spring-data-mongodb