简体   繁体   English

Django-haystack无法索引位置字段

[英]Django-haystack is unable to index location fields

I'm trying to index a bunch of addresses with django-haystack. 我正在尝试使用django-haystack索引一堆地址。 My searchindex is: 我的搜索索引是:

class AddressIndex(indexes.SearchIndex, indexes.Indexable):
    street = indexes.CharField(model_attr='street')
    city = indexes.CharField(model_attr='city')
    location = indexes.LocationField(null=True)

    def prepare_location(self, obj):
        try:
            return obj.location.point
        except AttributeError:
            return None

    def get_model(self):
        return Address

The searchindex has more fields of course, but this suffices. searchindex当然有更多的字段,但这足够了。 When I try to index this by running ./manage.py update_index -k4 -b100 -v2 location (the index is stored inside the location app) everything goes great as long as prepare_location returns None. 当我尝试通过运行./manage.py update_index -k4 -b100 -v2 location (索引存储在location应用程序中) ./manage.py update_index -k4 -b100 -v2 location索引时,只要prepare_location返回None,一切都会变好。 As soon as it returns something (eg. the point 0.000, 0.000) I get an error from Solr mentioning something about incompatible dimensions. 一旦返回某些内容(例如,点0.000、0.000),Solr就会出现一个错误,其中提到了一些不兼容的尺寸。

The exact error is org.apache.solr.common.SolrException: com.spatial4j.core.exception.InvalidShapeException: incompatible dimension (2) and values (POINT (0.0000000000000000 0.0000000000000000)). Only 0 values specified 确切的错误是org.apache.solr.common.SolrException: com.spatial4j.core.exception.InvalidShapeException: incompatible dimension (2) and values (POINT (0.0000000000000000 0.0000000000000000)). Only 0 values specified org.apache.solr.common.SolrException: com.spatial4j.core.exception.InvalidShapeException: incompatible dimension (2) and values (POINT (0.0000000000000000 0.0000000000000000)). Only 0 values specified . org.apache.solr.common.SolrException: com.spatial4j.core.exception.InvalidShapeException: incompatible dimension (2) and values (POINT (0.0000000000000000 0.0000000000000000)). Only 0 values specified I thought "maybe it doesn't like this point" and added 0.0000000000000001 to both the point.x and point.y , but the error stayed the same (except now it mentioned the new coordinates). 我以为“也许它不喜欢这个点”,并且在point.xpoint.y上都添加了0.0000000000000001,但是错误保持不变(除了现在提到了新坐标)。

Anybody got an idea what's going on here? 有人知道这里发生了什么吗?

I'm using: 我正在使用:

  • Python 2.7.5+ (yes, it actually says that when I run python --version) Python 2.7.5+(是的,实际上是说当我运行python --version时)
  • Django 1.5.2 的Django 1.5.2
  • django-haystack 2.1.0 django-干草堆2.1.0
  • Solr 4.3.0 Solr 4.3.0
  • pysolr 3.1.0 pysolr 3.1.0

On Ubuntu 13.10 with all the latest updates installed. 在安装了所有最新更新的Ubuntu 13.10上。

Apparently django-haystack doesn't do a particular, important bit itself. 显然django-haystack本身并没有做特别重要的事情。 It doesn't convert a GeoDjango Point into the required "lat,lon" format, but just passes through the Point object on to the XML document. 它不会将GeoDjango Point转换为所需的“ lat,lon”格式,而只是将Point对象传递到XML文档。

So instead of doing this: 所以不要这样做:

def prepare_location(self, obj):
    try:
        return obj.location.point
    except AttributeError:
        return None

One need to be doing this: 需要这样做:

def prepare_location(self, obj):
    try:
        return "{lat},{lon}".format(lat=obj.location.point.y, obj.location.point.x)
    except AttributeError:
        return None

It would have been a hell of a lot easier if I'd just read the documentation ... 如果我只是阅读文档的话,这本来会容易得多。

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

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