MongoDB无法规范化查询:BadValue文本表达式过多 [英] MongoDB Can't canonicalize query: BadValue Too many text expressions

查看:69
本文介绍了MongoDB无法规范化查询:BadValue文本表达式过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java构建MongoDB的查询,并且每次运行查询时,我都会遇到此错误无法规范化查询:BadValue文本表达式过多".

I am attempting to build a query for MongoDB in Java and I am running into this error, "Can't canonicalize query: BadValue Too many text expressions" whenever I run the query.

数据库中充满了带有text属性的文档,这些属性被索引以进行全文搜索.我正在尝试建立一个查询,以查找与queryList中的一个或多个查询相匹配且在特定时间范围内的任何文档.如果queryList中只有一个查询,它会很好地工作,否则将失败.

The database is full of documents with a text property that is indexed for full text searching. I am trying to build a query to find any documents that match one or more of the queries in queryList and are within a certain time range. It works fine if there is only one query in the queryList, but fails otherwise.

有什么想法吗?

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 条件.MongoDB无法做到这一点,实际上它在限制的第一行中进行了说明> $ text .

The error is pretty exact. What you are trying to do is create "multiple text queries" inside an $or condition. MongoDB cannot do that and it is in fact stated in the first line of Restrictions in the manual page for $text.

此外,您不是应该这样做的,而是在集合上指定一个文本索引,以便在需要时搜索多个字段:

Moreover, you are not supposed to do that, but rather specify one text index on your collection to search on multiple fields if required:

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

然后您可能想分配权重如此处所示.

And then you probably want to assign weights as shown here.

但是您似乎真正要问的是搜索多个词".因此,您无需为此使用 $ or ,而只需提交用空格分隔的术语列表即可:

But it seems all you are really asking is to search for "multiple terms". So you don't use an $or for this, but just submit the list of terms separated by spaces:

db.collection.find({ "$text": { "$search": "something else" } })

然后在文本索引内任何字段的上下文中搜索以空格分隔的列表中的所有单词,然后将返回包含这些单词中的"any"的任何文档.根据结果​​的权重"排序,结果是该列表中单词的更多匹配项.

Any words in the space delimited list are then searched for within the context of whatever fields are within the text index, and any document that contains "any" of those words will be returned. With the results ordered by "weight" of more matches of the words in that list.

这篇关于MongoDB无法规范化查询:BadValue文本表达式过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆