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

2024-03-18

我正在使用 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:':返回非正常状态代码 (500),包含 u'SearchPhaseExecutionException[无法执行阶段 [查询],完全失败; 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[]: 查询失败[执行主查询失败]];嵌套: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)],from[0],size[10],sort[]: 查询失败[执行主查询失败]];嵌套:ElasticSearchIllegalArgumentException[field [location] 不是 geo_point 字段]; }]'。

怎么了?


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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐