简体   繁体   English

具有文本搜索功能的Mongoengine地理空间查询无法正常工作

[英]Mongoengine geo spatial query with text search does not work as expected

The following is my query: 以下是我的查询:

items = Item.objects(
                    location__near=[item_obj.longitude, item_obj.latitude],
                    location__max_distance=item_obj.range,
                    status__status=ITEM_STATUS_DISPLAYED
                ).filter(
                    Q(title__icontains=item_obj.search) |
                    Q(description__icontains=item_obj.search
                )
            ).hint([('location', '2dsphere')])

This query does not seem to work as objects outside the range is getting returned. 由于返回了超出范围的对象,因此该查询似乎不起作用。 And the item status also seems to be ignored. 并且项目状态似乎也被忽略了。 The range is given in meters. 范围以米为单位。

The strange thing is the following query works without any issues: 奇怪的是,以下查询可以正常工作:

items = Item.objects(
                    location__near=[item_obj.longitude, item_obj.latitude],
                    location__max_distance=item_obj.range,
                    status__status=ITEM_STATUS_DISPLAYED
                )

I am not sure what is wrong. 我不确定是怎么了。

I recommend that you stay away from the $near handle since mongoengine is using a deprecated call to PyMongo and your application will fail if you update mongoDB to the latest version. 我建议您远离$ near句柄,因为mongoengine使用了不推荐使用的PyMongo调用,如果将mongoDB更新到最新版本,则应用程序将失败。

What I did was to use the geo_within query, specifically the geo_within_sphere since I am finding points within a circle around locations on earth. 我所做的是使用geo_within查询,尤其是geo_within_sphere,因为我是在围绕地球位置的圆周内找到点。 You can find reference here: MongoEngine geo query 您可以在此处找到参考: MongoEngine地理查询

One thing that they don't explain in there is converting the radius. 他们在那里没有解释的一件事就是转换半径。 If you use Km then you have to do radius/6371.0 if you use miles then radius/3959.0 如果使用Km,则必须使用radius / 6371.0(如果使用英里),则必须做radius / 3959.0

My queries look like this: 我的查询如下所示:

data = data_set.objects(
           location__geo_within_sphere=[[longitude, latitude], radius/6371.0]
       )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM