MongoDB 无法规范化查询:BadValue 文本表达式太多

MongoDB Can't canonicalize query: BadValue Too many text expressions

我正在尝试在 Java 中为 MongoDB 构建查询,但我 运行 遇到了这个错误,"Can't canonicalize query: BadValue Too many text expressions" 每当我 运行询问。

数据库中充满了包含文本 属性 的文档,这些文档已编入索引以进行全文搜索。我正在尝试构建一个查询以查找与 queryList 中的一个或多个查询匹配并且在特定时间范围内的任何文档。如果 queryList 中只有一个查询,它工作正常,否则失败。

有什么想法吗?

BasicDBList queryList = new BasicDBList();

for(String queryString : queryStrings)
{
    queryList.add(new BasicDBObject("$text", new BasicDBObject("$search", queryString)));
}

BasicDBObject queries = new BasicDBObject("$or", queryList);

DBObject query =  QueryBuilder.start()
    .and(endpointQuery,
         QueryBuilder.start().put("time").greaterThan(minTime).get(),
         QueryBuilder.start().put("time").lessThan(maxTime).get())
    .get();

DBCursor cursor = tweets.find(query);

错误非常准确。您要做的是在 $or condition. MongoDB cannot do that and it is in fact stated in the first line of Restrictions in the manual page for $text.

中创建 "multiple text queries"

此外,您 应该 这样做,而是在集合中指定 一个 文本索引以搜索多个字段,如果要求:

db.collection.ensureIndex({ "comments": "text", "title": "text" })

然后您可能想要分配权重 as shown here

但似乎您真正想要的只是搜索 "multiple terms"。因此,您不为此使用 $or,而只需提交由 spaces:

分隔的术语列表
db.collection.find({ "$text": { "$search": "something else" } })

然后在文本索引中的任何字段的上下文中搜索 space 分隔列表中的任何单词,并且将返回包含这些单词的 "any" 的任何文档。结果按 "weight" 排序,该列表中的单词更多匹配。