Django,Haystack,Elasticsearch:拒绝映射更新?

时间:2018-06-18 12:20:06

标签: python django python-3.x elasticsearch django-haystack

... [transcription-index] as the final mapping would have more than 1 type: [doc, modelresult]

我在一个完全干净的Django上得到了上述错误(即删除了所有以前的迁移和sqlite.db)。 Elasticsearch是6.2.4,elasticsearch_dsl是6.1.0,Django是2.0.5。

然后我运行python manage.py makemigrationspython manage.py migrate,效果很好。在python manage.py createsuperuser之后,我可以登录,看看http://127.0.0.1:8000/admin/elasticsearchapp/transcription/是否为空,但是包含所有正确的字段(identifier, speaker, title, description, trans)

当我尝试通过运行python manage.py populate来填充我的sqlite.db数据时出现问题,我得到了

raise BulkIndexError('%i document(s) failed to index.' % len(errors), errors)

elasticsearch.helpers.BulkIndexError: ('1 document(s) failed to index.', [{'index': {'_index': 'transcription-index', '_type': 'modelresult', '_id': 'elasticsearchapp.transcription.1', 'status': 400, 'error': {'type': 'illegal_argument_exception', 'reason': 'Rejecting mapping update to [transcription-index] as the final mapping would have more than 1 type: [doc, modelresult]'}

I have read several issues on this, but most of them refer to "Indices created in Elasticsearch 6.0.0 or later may only contain a single mapping type."。但是,我无法看到与我相关的方法?

完整回溯位于下方。

代码

search_indexes.py

from haystack import indexes
from elasticsearchapp.models import transcription


class transcription_index(indexes.SearchIndex, indexes.Indexable):
    text = indexes.EdgeNgramField(
        document=True,
        use_template=True)

    identifier = indexes.CharField(model_attr='identifier')
    speaker = indexes.CharField(model_attr='speaker')
    title = indexes.CharField(model_attr='title')
    description = indexes.CharField(model_attr='description')
    trans = indexes.CharField(model_attr='trans')

    def get_model(self):
        return transcription

    def index_queryset(self, using=None):
        return self.get_model().objects.all()

search.py

from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import DocType, Text
from elasticsearch.helpers import bulk
from elasticsearch import Elasticsearch
from . import models

connections.create_connection()


class transcription_index(DocType):
    identifier = Text()
    speaker = Text()
    title = Text()
    description = Text()
    trans = Text()

    class Meta:
        index = 'transcription-index'


def bulk_indexing():
    transcription_index.init()
    es = Elasticsearch()
    bulk(client=es, actions=(b.indexing() for b in models.transcription.objects.all().iterator()))

models.py

from __future__ import unicode_literals

from django.db import models
from .search import transcription_index


class transcription(models.Model):
    identifier = models.CharField(max_length=20, default='')
    title = models.CharField(max_length=200, default='')
    speaker = models.CharField(max_length=50, default='')
    description = models.CharField(max_length=1000, default='')
    trans = models.CharField(max_length=30000, default='')

    # Method for indexing the model
    def indexing(self):
        obj = transcription_index(
            identifier=self.identifier,
            speaker=self.speaker,
            title=self.title,
            description=self.description,
            trans=self.trans
        )
        obj.save()
        return obj.to_dict(include_meta=True)

    def save(self, *args, **kwargs):
        return super(transcription, self).save(*args, **kwargs)

    def __unicode__(self):
        return "{}:{}".format(
            self.identifier,
            self.title,
            self.speaker,
            self.description,
            self.trans)

populate.py

from django.core.management import BaseCommand

from elasticsearchapp.models import transcription
from elasticsearchapp.data_get import entry_make


class Command(BaseCommand):
    # Show this when the user types help
    help = "Load data from dataframe to db"

    # A command must define handle()
    def handle(self, *args, **options):
        # Process data with Pandas
        dbentry = entry_make()

        for entry in dbentry.itertuples():
            entry = transcription.objects.create(
                identifier=entry.identifier,
                speaker=entry.speaker,
                title=entry.title,
                description=entry.description,
                trans=entry.trans)

回溯

PUT http://127.0.0.1:9200/transcription-index/_mapping/modelresult [status:400 request:0.013s]
Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/Users/xxx/projects/django_elasticsearch/elasticsearchapp/management/commands/populate.py", line 22, in handle
    trans=entry.trans)
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/django/db/models/query.py", line 417, in create
    obj.save(force_insert=True, using=self.db)
  File "/Users/xxx/projects/django_elasticsearch/elasticsearchapp/models.py", line 31, in save
    return super(transcription, self).save(*args, **kwargs)
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/django/db/models/base.py", line 729, in save
    force_update=force_update, update_fields=update_fields)
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/django/db/models/base.py", line 769, in save_base
    update_fields=update_fields, raw=raw, using=using,
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 178, in send
    for receiver in self._live_receivers(sender)
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 178, in <listcomp>
    for receiver in self._live_receivers(sender)
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/haystack/signals.py", line 52, in handle_save
    index.update_object(instance, using=using)
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/haystack/indexes.py", line 284, in update_object
    backend.update(self, [instance])
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/haystack/backends/elasticsearch_backend.py", line 190, in update
    bulk(self.conn, prepped_docs, index=self.index_name, doc_type='modelresult')
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/elasticsearch/helpers/__init__.py", line 257, in bulk
    for ok, item in streaming_bulk(client, actions, **kwargs):
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/elasticsearch/helpers/__init__.py", line 192, in streaming_bulk
    raise_on_error, **kwargs)
  File "/anaconda3/envs/django_elasticsearch/lib/python3.6/site-packages/elasticsearch/helpers/__init__.py", line 137, in _process_bulk_chunk
    raise BulkIndexError('%i document(s) failed to index.' % len(errors), errors)
elasticsearch.helpers.BulkIndexError: ('1 document(s) failed to index.', [{'index': {'_index': 'transcription-index', '_type': 'modelresult', '_id': 'elasticsearchapp.transcription.1', 'status': 400, 'error': {'type': 'illegal_argument_exception', 'reason': 'Rejecting mapping update to [transcription-index] as the final mapping would have more than 1 type: [doc, modelresult]'}, 'data': {'id': 'elasticsearchapp.transcription.1', 'django_ct': 'elasticsearchapp.transcription', 'django_id': '1', 'text': 'The .... '

0 个答案:

没有答案