liberation / django-elasticsearch

Simple wrapper around elasticsearch-py to index/search a django Model.
MIT License
212 stars 73 forks source link

StringFieldMapper cannot be cast to CompletionFieldMapper when using `es.complete` #42

Closed hellosteadman closed 8 years ago

hellosteadman commented 8 years ago

I've a model whose Elasticsearch class looks like this:

mappings = {
    'name': {
        'boost': 3.0,
        'type': 'completion'
    },
    'custom_fields': {
        'type': 'object'
    }
}

fields = (
    'id',
    'name',
    'description',
    'tags',
    'taxonomy',
    'ordering_name',
    'custom_fields'
)

suggest_fields = ('name',)
facets_fields = ('parent', 'taxonomy',)
completion_fields = ('name',)

When running model.es.complete('name', q) (where q is a string) I get a very long and verbose error from Elasticsearch that boils down to the following (repeated 5 times):

ClassCastException[org.elasticsearch.index.mapper.core.StringFieldMapper cannot be cast to org.elasticsearch.index.mapper.core.CompletionFieldMapper] U

As far as I can tell, everything's defined correctly. I've tried reindexing, but always end up with the same error. Using the latest version from Git - at the time of writing - of this package, 1.9.0 of the Python package and 1.7.2 of the server. Any pointers as to where I might be going wrong would be helpful, if this isn't an issue with the library.

apollodatasolutions commented 8 years ago

I could be wrong, but I think you need something like:

"name": { "type": "string" },

in the mappings:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html#completion-suggester-mapping

lauxley commented 8 years ago

Hello, the 'completion' type field is automatically added by django-elasticsearch so @apollodatasolutions answer's is correct, you shouldn't add it in your mappings, only in completion_fields. Note that it will appear in the mappings as name__suggest (this is a convention).