小编典典

Django Haystack通过Elasticsearch后端按距离排序,而不是geo_point字段错误

elasticsearch

我正在使用django 1.4,django-haystack 2.0和Elasticsearch 0.19.1,我有一个像这样的SearchIndex:

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

和这样的项目模型:

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)

因此,我想通过从近到远的距离特定坐标来查询Project对象:

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

但我收到此错误:

无法使用’来查询Elasticsearch ‘:返回的非OK状态代码(500),包含u’SearchPhaseExecutionException
[无法执行阶段[查询],完全失败;shardFailures {[jmUkmHkDTX-bo9DhFJdtSw] [skp]
[2]:QueryPhaseExecutionException [[skp] [2]:query
[filtered(ConstantScore(NotDeleted(cache(QueryWrapperFilter(django_ct:core.project)))))->
cache(_ :modelresult)],从[0],大小[10],排序[]:查询失败[无法执行主查询]];
嵌套:ElasticSearchIllegalArgumentException [field [location]不是geo_point字段];}
{[jmUkmHkDTX-bo9DhFJdtSw] [skp] [4]:QueryPhaseExecutionException [[skp]
[4]:query
[filtered(ConstantScore(NotDeleted(cache(QueryWrapperFilter(django_ct:core.project)))))->
cache(_type :modelresult)],从[0],大小[10],排序[]:查询失败[无法执行主查询]];
嵌套:ElasticSearchIllegalArgumentException [field [location]不是geo_point字段]; }]’。

怎么了?


阅读 581

收藏
2020-06-22

共1个答案

小编典典

您的位置字段的“类型”方面可能映射错误。这可能是由您用于映射的api引起的。映射中的某些方面可以更改,但字段的“类型”方面不能更改。因此,您必须使用geo_point类型的location字段创建一个新的映射,然后为您的文档重新编制索引。

2020-06-22