Django haystack LocationField created as string instead of geo_point in elasticsearch

匆匆过客 提交于 2019-12-10 20:32:53

问题


I'm using django 1.8.9, django-rest-framework, django-haystack together with Elasticsearch and am attempting to get a LocationField working, the index is created however the type is always string instead of geo_point, so obviously no geo searches work.

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

the get_location property on MyModel:

from haystack.utils.geo import Point
def get_location(self):
    return Point(self.lng, self.lat)

the elasticsearch index created (sorry about the formatting!):

{  
   "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"
               }
            }
         }
      }
   }
}

Does anyone have any ideas? It feels like it's a combination of versions between django, django-haystack and elasticsearch not playing nicely, but I can't seem to get any combination working.


回答1:


Ok I've figured out what the problem is: In Elasticsearch 2.0, there are metadata changes, one of which is boost that has been removed: https://www.elastic.co/guide/en/elasticsearch/reference/current/breaking_20_mapping_changes.html#migration-meta-fields

Tracing back through elasticsearch/transport.py, the PUT request to http://127.0.0.1:9200/myindex/_mapping/modelresult includes "_boost": {"name": "boost", "null_value": 1.0} in the body.

So tracing the calls and represting them as CURL:

CREATE THE INDEX

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

THE FAILING REQUEST

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

Changing to this works

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

So, the python fix In haystack/backends/elasticsearch_backend.py, comment out the boost section from current_mapping on line 137-140




回答2:


Maybe it does not work, because get_location is not a field on MyModel. Maybe you can avoid creating location as a field by adding @property decorator, like this:

from haystack.utils.geo import Point

@property
def get_location(self):
    return Point(self.lng, self.lat)

EDIT: This does not look like it will solve a problem. You have it just like it is in documentation.



来源:https://stackoverflow.com/questions/35443179/django-haystack-locationfield-created-as-string-instead-of-geo-point-in-elastics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!