Django Haystack order by distance by Elasticsearch backend, not a geo_point field error

1.8k views Asked by At

I'm using django 1.4, django-haystack 2.0 and Elasticsearch 0.19.1 I have an SearchIndex like that:

from haystack import indexes
from core.models import Project

class ProjectIndex(indexes.RealTimeSearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    location = indexes.LocationField(model_attr='get_location')

    def get_model(self):
        return Project

and Project model like that:

class Project(BaseModel):
    name = models.CharField(_(u'Proje Adı'), max_length=50)
    latitude = models.FloatField()
    longitude = models.FloatField()

    def get_location(self):
        # Remember, longitude FIRST!
        return Point(self.longitude, self.latitude)

So I want to query Project objects by distance specific coordinate from near to far:

....
location = Point(project.longitude, project.latitude)
projects = SearchQuerySet().models(Project).distance('location', location).order_by('distance')

But I'm getting this error:

Failed to query Elasticsearch using ':': Non-OK status code returned (500) containing u'SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[jmUkmHkDTX-bo9DhFJdtSw][skp][2]: QueryPhaseExecutionException[[skp][2]: query[filtered(ConstantScore(NotDeleted(cache(QueryWrapperFilter(django_ct:core.project)))))->cache(_type:modelresult)],from[0],size[10],sort[]: Query Failed [Failed to execute main query]]; nested: ElasticSearchIllegalArgumentException[field [location] is not a geo_point field]; }{[jmUkmHkDTX-bo9DhFJdtSw][skp][4]: QueryPhaseExecutionException[[skp][4]: query[filtered(ConstantScore(NotDeleted(cache(QueryWrapperFilter(django_ct:core.project)))))->cache(_type:modelresult)],from[0],size[10],sort[]: Query Failed [Failed to execute main query]]; nested: ElasticSearchIllegalArgumentException[field [location] is not a geo_point field]; }]'.

What is wrong?

1

There are 1 answers

2
Koray Bilgi On BEST ANSWER

Probably the "type" aspect of your location field is mapped wrong. This could be caused by the api that you used for mapping. Some aspects in a mapping can be changed, but not the "type" aspect of a field. So you'll have to create a new mapping with location field in geo_point type, and reindex your docs.