python – Django haystack在弹性搜索中将LocationField创建为字符串而不是geo_point
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – Django haystack在弹性搜索中将LocationField创建为字符串而不是geo_point,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4091字,纯文字阅读大概需要6分钟。
内容图文
![python – Django haystack在弹性搜索中将LocationField创建为字符串而不是geo_point](/upload/InfoBanner/zyjiaocheng/727/70b72a2a6067453aa5ea6b654dc7143b.jpg)
我正在使用django 1.8.9,django-rest-framework,django-haystack和Elasticsearch,并且我试图让LocationField工作,索引被创建但是类型总是字符串而不是geo_point,所以显然没有地理搜索工作.
settings.py:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.gis',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'django_extensions',
'elasticsearch',
'rest_framework',
'haystack',
)
requirements.txt:
Django==1.8.9
django-appconf==1.0.1
django-compressor==1.6
django-extensions==1.6.1
django-filter==0.11.0
django-haystack==2.4.1
djangorestframework==3.3.1
djangorestframework-jwt==1.7.2
ecdsa==0.13
elasticsearch==2.2.0
Fabric==1.10.2
future==0.15.2
geopy==1.11.0
gunicorn==19.4.1
Markdown==2.6.5
paramiko==1.16.0
psycopg2==2.6.1
pycrypto==2.6.1
PyJWT==1.4.0
python-dateutil==2.4.2
python-memcached==1.57
setproctitle==1.1.9
six==1.10.0
urllib3==1.14
search_indexes.py:
from haystack import indexes
from blah.api.models import MyModel
class MyIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
description = indexes.CharField(model_attr='description')
location = indexes.LocationField(model_attr='get_location')
created = indexes.DateTimeField(model_attr='created')
def get_model(self):
return MyModel
MyModel上的get_location属性:
from haystack.utils.geo import Point
def get_location(self):
return Point(self.lng, self.lat)
创建了elasticsearch索引(抱歉格式化!):
{
"myindex":{
"mappings":{
"modelresult":{
"properties":{
"created":{
"type":"date",
"format":"strict_date_optional_time||epoch_millis"
},
"description":{
"type":"string"
},
"django_ct":{
"type":"string"
},
"django_id":{
"type":"string"
},
"id":{
"type":"string"
},
"location":{
"type":"string"
},
"text":{
"type":"string"
}
}
}
}
}
}
有没有人有任何想法?感觉它是django,django-haystack和elasticsearch之间的版本组合不能很好地发挥,但我似乎无法让任何组合工作.
解决方法:
好的,我已经弄清楚问题是什么:
在Elasticsearch 2.0中,有元数据更改,其中一个是已删除的提升:https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking_20_mapping_changes.html#migration-meta-fields
追溯到elasticsearch / transport.py,PUT请求到http://127.0.0.1:9200/myindex/_mapping/modelresult包括“_boost”:{“name”:“boost”,“null_value”:1.0}在正文中.
因此,跟踪调用并将它们表示为CURL:
创造指数
curl -X PUT -d '{"settings": {"analysis": {"filter": {"haystack_edgengram": {"max_gram": 15, "type": "edgeNGram", "min_gram": 2}, "haystack_ngram": {"max_gram": 15, "type": "nGram", "min_gram": 3}}, "tokenizer": {"haystack_ngram_tokenizer": {"max_gram": 15, "type": "nGram", "min_gram": 3}, "haystack_edgengram_tokenizer": {"max_gram": 15, "type": "edgeNGram", "side": "front", "min_gram": 2}}, "analyzer": {"edgengram_analyzer": {"filter": ["haystack_edgengram", "lowercase"], "type": "custom", "tokenizer": "standard"}, "ngram_analyzer": {"filter": ["haystack_ngram", "lowercase"], "type": "custom", "tokenizer": "standard"}}}}}' http://127.0.0.1:9200/myindex
失败请求
curl -X PUT -d '{"modelresult": {"_boost": {"name": "boost", "null_value": 1.0}, "properties": {"django_id": {"include_in_all": false, "index": "not_analyzed", "type": "string"}, "description": {"type": "string", "analyzer": "snowball"}, "created": {"type": "date"}, "text": {"type": "string", "analyzer": "snowball"}, "django_ct": {"include_in_all": false, "index": "not_analyzed", "type": "string"}, "location": {"type": "geo_point"}}}}' http://127.0.0.1:9200/myindex/_mapping/modelresult
改为此工作
curl -X PUT -d '{"modelresult": {"properties": {"django_id": {"include_in_all": false, "index": "not_analyzed", "type": "string"}, "description": {"type": "string", "analyzer": "snowball"}, "created": {"type": "date"}, "text": {"type": "string", "analyzer": "snowball"}, "django_ct": {"include_in_all": false, "index": "not_analyzed", "type": "string"}, "location": {"type": "geo_point"}}}}' http://127.0.0.1:9200/myindex/_mapping/modelresult
所以,python修复
在haystack / backends / elasticsearch_backend.py中,从第137-140行的current_mapping中注释出boost部分
内容总结
以上是互联网集市为您收集整理的python – Django haystack在弹性搜索中将LocationField创建为字符串而不是geo_point全部内容,希望文章能够帮你解决python – Django haystack在弹性搜索中将LocationField创建为字符串而不是geo_point所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。