Mongodb 如何在mongoengine中对地理空间查询返回的查询集进行文本搜索?
我需要在附近找到与文本匹配的“项”。我正在使用mongo引擎,并尝试了以下方法Mongodb 如何在mongoengine中对地理空间查询返回的查询集进行文本搜索?,mongodb,pymongo,mongoengine,Mongodb,Pymongo,Mongoengine,我需要在附近找到与文本匹配的“项”。我正在使用mongo引擎,并尝试了以下方法 Item.objects( location__near=[item_obj.longitude, item_obj.latitude], location__max_distance=item_obj.range).filter( Q(title__icontains=item_obj.search) |
Item.objects(
location__near=[item_obj.longitude, item_obj.latitude],
location__max_distance=item_obj.range).filter(
Q(title__icontains=item_obj.search) |
Q(description__icontains=item_obj.search)
).order_by('-like_count')
我得到以下错误
OperationFailure: database error: can't find any special indices: 2d (needs index), 2dsphere (needs index), for: { $and: [ { location: { $near: { $geometry: { type: "Point", coordinates: [ 4.95, 4.95 ] } }, $maxDistance: 10000 } }, { $or: [ { title: /title/i }, { description: /title/i } ] } ] }
但是,以下方法效果很好:
items = Item.objects(
location__near=[item_obj.longitude, item_obj.latitude],
location__max_distance=item_obj.range
)
这里的位置是一个点域似乎它没有使用索引进行查询 1确保已创建索引:
Item.ensure_indexes()
2向查询添加提示以确保其使用2dsphere索引:
Item.objects(
location__near=[item_obj.longitude, item_obj.latitude],
location__max_distance=item_obj.range).filter(
Q(title__icontains=item_obj.search) |
Q(description__icontains=item_obj.search)
).order_by('-like_count').hint([('location', '2dsphere')])
看起来缺少地理索引-可以尝试:Item.sure\u index,然后运行它。尝试了,但没有区别。根据手册,PointField应自动设置2d球体索引。我在这里遗漏了什么吗?MongoDB/MongoEngine的哪个版本?MongoEngine==0.8.4 mongo v2.4.9项的输出是什么?