Question

I'm trying to index a bunch of addresses with 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. 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. As soon as it returns something (eg. the point 0.000, 0.000) I get an error from Solr mentioning something about incompatible dimensions.

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. 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).

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)
  • Django 1.5.2
  • django-haystack 2.1.0
  • Solr 4.3.0
  • pysolr 3.1.0

On Ubuntu 13.10 with all the latest updates installed.

Was it helpful?

Solution

Apparently django-haystack doesn't do a particular, important bit itself. 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.

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...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top