瀏覽代碼

Pass update_all_types=True to put_mapping on ES2 - workaround for #2968

Matt Westcott 8 年之前
父節點
當前提交
b4a4e5eb92
共有 2 個文件被更改,包括 12 次插入1 次删除
  1. 1 0
      tox.ini
  2. 11 1
      wagtail/wagtailsearch/backends/elasticsearch2.py

+ 1 - 0
tox.ini

@@ -29,6 +29,7 @@ deps =
     dj110head: git+https://github.com/django/django.git@stable/1.10.x#egg=Django
     postgres: psycopg2>=2.6
     mysql: mysqlclient==1.3.6
+    elasticsearch: elasticsearch>=1,<2
     elasticsearch2: elasticsearch>=2,<3
 
 setenv =

+ 11 - 1
wagtail/wagtailsearch/backends/elasticsearch2.py

@@ -102,7 +102,17 @@ class Elasticsearch2Mapping(ElasticsearchMapping):
 
 
 class Elasticsearch2Index(ElasticsearchIndex):
-    pass
+    def add_model(self, model):
+        # Get mapping
+        mapping = self.mapping_class(model)
+
+        # Put mapping
+        self.es.indices.put_mapping(
+            # pass update_all_types=True as a workaround to avoid "Can't redefine search field" errors -
+            # see https://github.com/torchbox/wagtail/issues/2968
+            index=self.name, doc_type=mapping.get_document_type(), body=mapping.get_mapping(),
+            update_all_types=True
+        )
 
 
 class Elasticsearch2SearchQuery(ElasticsearchSearchQuery):