MongoDB全文搜索建议多个词

0 投票
2 回答
1237 浏览
提问于 2025-04-17 01:29

我正在尝试为我的一个MongoDB集合实现某种形式的全文搜索(就像flowdock那样)。我为每个文档创建了一个_keywords字段,并用该文档其他字段中的小写单词填充它。然后我像这样进行搜索(前缀搜索),例如searchString = 'car'。

found_shots = connection.Shot.find({'_keywords': re.compile('^%s' % searchString.lower())}).limit(limit).skip(skip)

问题是,当我尝试搜索多个单词时(例如searchstring = ['car', 'online']),

regex1 = re.compile('^%s' % searchStrings[0].lower())
regex2 = re.compile('^%s' % searchStrings[1].lower())
found_shots = connection.Shot.find({'$and':[{'_keywords':regex1},{'_keywords':regex2}]}).limit(limit).skip(skip)

这就不行了。有没有什么好的建议呢?

2 个回答

1

MongoDB 2.6现在可以通过使用$text命令和一个全文搜索索引,来进行完整的文本搜索。

1

$and 这个功能只在 1.9.x 版本中可用。

因为你现在用的是 1.8.2 版本,所以这个功能不能正常工作。

如果你升级到新版本,就能获得最新的指令,并且可以使用 $and 这个功能。

撰写回答