我需要找到与文字匹配的“项目”。我正在使用mongo引擎,我尝试了以下内容。
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
)
此处location
是PointField
答案 0 :(得分:0)
似乎没有使用查询索引。
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')])