[英]Mongo text index doesn't finish when nothing is returned
I am not sure I have the proper vocabulary to describe the error that I am having so bear with me. 我不确定我是否有合适的词汇来描述我所承受的错误。
Here is the general schema for the documents in my collection 这是我收藏中的文档的一般架构
{
_id: ObjectId(),
name: String,
business: String,
address: {
search_type: Character,
address: String,
city: String,
state: String,
zip: Number
}
}
I wanted to search based on the address.search_type
so I created a text index for that fields in my collection. 我想基于
address.search_type
进行搜索,因此我为集合中的这些字段创建了文本索引。
{
v: 1,
key: { _fts: 'text', _ftsx: 1 },
name: 'address.search_type_text',
ns: 'admin.customer',
default_language: 'none',
weights: { 'address.search_type': 1 },
language_override: 'language',
textIndexVersion: 3
}
Now I know that my data should really only have C, G, or T as a search type and when I run a find query on this collection with one of the supported search_types the query runs just fine. 现在,我知道我的数据实际上应该仅以C,G或T作为搜索类型,并且当我使用支持的search_types之一对此集合运行查找查询时,查询运行就很好。
db.collection('blah').find({'address.search_type':'C'}).limit(10).toArray(function(err, result){
if(err) console.log(err);
else console.log(result[0]);
db.close();
});
But when I run this query with a address.search_type
that should return 0 documents my query either never finishes or times out. 但是,当我使用应返回0个文档的
address.search_type
运行此查询时,我的查询永远不会完成或超时。
db.collection('blah').find({'address.search_type':'Z'}).limit(10).toArray(function(err, result){
if(err) console.log(err);
else console.log(result[0]);
db.close();
});
Why would my query not finish running / timeout when there are supposed to be 0 documents but works just fine when it can find documents? 为什么在应该有0个文档的情况下我的查询无法完成运行/超时,但是在可以找到文档时却能正常工作?
After playing around with different types of indexes I realized that I did not need a text index at all. 在使用了不同类型的索引之后,我意识到我根本不需要文本索引。
This is probably an embarrassing mistake but hey I just started learning mongodb
so whatever. 这可能是一个令人尴尬的错误,但是嘿,我刚刚开始学习
mongodb
所以无论如何。
Anyway, I realized that all I needed to do was the create a single field index on address.search_type
. 无论如何,我意识到我要做的就是在
address.search_type
上创建一个字段索引。
{ v: 1,
key: { 'address.Search_Type': 1 },
name: 'address.Search_Type_1',
ns: 'admin.customer'
}
I am not still totally sure why the text index was not working since I had set the language to none, but it makes sense that all I would need is a single index. 由于将语言设置为none,我仍然不确定为什么文本索引不起作用,但是我所需要的只是一个索引。
I think text index is mostly used for searching phrases or keywords in largeish blocks of text. 我认为文本索引主要用于搜索较大文本块中的短语或关键字。 So the fact that my search was looking for a single character might have been causing problems since that is not what the text index was intended to be used for.
因此,我的搜索正在寻找单个字符的事实可能已引起问题,因为这不是文本索引的预期用途。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.