From a34fd9ac89c3ebd294f57d4b471fb6f5b31e1e91 Mon Sep 17 00:00:00 2001 From: Thomas Erker <35792856+th-erker@users.noreply.github.com> Date: Thu, 8 Mar 2018 11:25:36 +0000 Subject: [PATCH 1/3] Add testcase for #1751 --- tests/document/class_methods.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/document/class_methods.py b/tests/document/class_methods.py index dd3addb7..8701b4b2 100644 --- a/tests/document/class_methods.py +++ b/tests/document/class_methods.py @@ -187,6 +187,19 @@ class ClassMethodsTest(unittest.TestCase): self.assertEqual(BlogPostWithTags.compare_indexes(), { 'missing': [], 'extra': [] }) self.assertEqual(BlogPostWithCustomField.compare_indexes(), { 'missing': [], 'extra': [] }) + def test_compare_indexes_for_text_indexes(self): + """ Ensure that compare_indexes behaves correctly for text indexes """ + + class Doc(Document): + a = StringField() + meta = { 'indexes': ['$a']} + + Doc.drop_collection() + Doc.ensure_indexes() + actual = Doc.compare_indexes() + expected = {'missing': [], 'extra': []} + self.assertEqual(actual, expected) + def test_list_indexes_inheritance(self): """ ensure that all of the indexes are listed regardless of the super- or sub-class that we call it from From 08b64338439a33a1b869ef0c77f09fd9b50bae2c Mon Sep 17 00:00:00 2001 From: Erdenezul Batmunkh Date: Sun, 20 May 2018 11:03:13 +0800 Subject: [PATCH 2/3] fix compare_indexes for text indexes #1751 --- mongoengine/document.py | 12 ++++++++++-- tests/document/class_methods.py | 8 +++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/mongoengine/document.py b/mongoengine/document.py index c948dac2..18c52d9d 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -967,8 +967,16 @@ class Document(BaseDocument): """ required = cls.list_indexes() - existing = [info['key'] - for info in cls._get_collection().index_information().values()] + + existing = [] + for info in cls._get_collection().index_information().values(): + if '_fts' in info['key'][0]: + index_type = info['key'][0][1] + text_index_fields = info.get('weights').keys() + existing.append( + [(key, index_type) for key in text_index_fields]) + else: + existing.append(info['key']) missing = [index for index in required if index not in existing] extra = [index for index in existing if index not in required] diff --git a/tests/document/class_methods.py b/tests/document/class_methods.py index 8701b4b2..3f052d45 100644 --- a/tests/document/class_methods.py +++ b/tests/document/class_methods.py @@ -192,7 +192,13 @@ class ClassMethodsTest(unittest.TestCase): class Doc(Document): a = StringField() - meta = { 'indexes': ['$a']} + b = StringField() + meta = {'indexes': [ + {'fields': ['$a', "$b"], + 'default_language': 'english', + 'weights': {'a': 10, 'b': 2} + } + ]} Doc.drop_collection() Doc.ensure_indexes() From e50d66b3032df29331f72f6ce238b3813ae35b50 Mon Sep 17 00:00:00 2001 From: Erdenezul Batmunkh Date: Sun, 20 May 2018 11:26:30 +0800 Subject: [PATCH 3/3] skip mongodb 2.4 --- tests/document/class_methods.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/document/class_methods.py b/tests/document/class_methods.py index 3f052d45..2fab1f72 100644 --- a/tests/document/class_methods.py +++ b/tests/document/class_methods.py @@ -5,6 +5,7 @@ from mongoengine import * from mongoengine.queryset import NULLIFY, PULL from mongoengine.connection import get_db +from tests.utils import needs_mongodb_v26 __all__ = ("ClassMethodsTest", ) @@ -187,6 +188,7 @@ class ClassMethodsTest(unittest.TestCase): self.assertEqual(BlogPostWithTags.compare_indexes(), { 'missing': [], 'extra': [] }) self.assertEqual(BlogPostWithCustomField.compare_indexes(), { 'missing': [], 'extra': [] }) + @needs_mongodb_v26 def test_compare_indexes_for_text_indexes(self): """ Ensure that compare_indexes behaves correctly for text indexes """