def find_conflicts(self, index_name):

        existing_mappings = get_existing_mapping(index_name=index_name)
        new_mappings = get_current_mapping(index_name=index_name)

        conflicts = tuple(find_conflicts(existing_mappings, new_mappings))

        if conflicts:
            print

        for field_name in conflicts:
            print_bad("'%s.%s' has changed!" % (index_name, field_name))
            print
            print "Before:"
            print_dict(existing_mappings["modelresult"]["properties"][field_name])
            print "After:"
            print_dict(new_mappings["modelresult"]["properties"][field_name])
            print
    def find_conflicts(self, index_name):

        existing_mappings = get_existing_mapping(index_name=index_name)
        new_mappings = get_current_mapping(index_name=index_name)

        conflicts = tuple(find_conflicts(existing_mappings, new_mappings))

        if conflicts:
            print

        for field_name in conflicts:
            print_bad("'%s.%s' has changed!" % (index_name, field_name))
            print
            print 'Before:'
            print_dict(
                existing_mappings['modelresult']['properties'][field_name])
            print 'After:'
            print_dict(new_mappings['modelresult']['properties'][field_name])
            print
    def show_fields(self, index_name, *fields):

        existing_mappings = get_existing_mapping(index_name=index_name)
        new_mappings = get_current_mapping(index_name=index_name)

        conflicts = tuple(find_conflicts(existing_mappings, new_mappings))

        for field_name in fields:

            try:
                before = existing_mappings['modelresult']['properties'][
                    field_name]
            except KeyError:
                before = None

            try:
                after = new_mappings['modelresult']['properties'][field_name]
            except KeyError:
                after = None

            if before or after:

                if field_name in conflicts:
                    print_method = print_bad
                else:
                    print_method = print_good

                print_method('Mappings for %s.%s:' % (index_name, field_name))
                print
                if before:
                    print 'Before:'
                    print_dict(before)
                    print
                if after and after != before:
                    print 'After:'
                    print_dict(after)
                    print
    def show_fields(self, index_name, *fields):

        existing_mappings = get_existing_mapping(index_name=index_name)
        new_mappings = get_current_mapping(index_name=index_name)

        conflicts = tuple(find_conflicts(existing_mappings, new_mappings))

        for field_name in fields:

            try:
                before = existing_mappings["modelresult"]["properties"][field_name]
            except KeyError:
                before = None

            try:
                after = new_mappings["modelresult"]["properties"][field_name]
            except KeyError:
                after = None

            if before or after:

                if field_name in conflicts:
                    print_method = print_bad
                else:
                    print_method = print_good

                print_method("Mappings for %s.%s:" % (index_name, field_name))
                print
                if before:
                    print "Before:"
                    print_dict(before)
                    print
                if after and after != before:
                    print "After:"
                    print_dict(after)
                    print
    def setup(self):
        """
        Set up the mappings (aka schema) for the index.

        The mappings that are returned from pyelasticsearch are different to
        what Haystack builds, so there is no point in comparing it (which is
        what Haystack normally does).

        Also, it kept breaking when the index did not exist.

        Also, raise an exception and provide extra information if there are
        mapping conflicts, because they are a big deal and ruin everything.

        """

        self.setup_index_groups()

        # Set up each index group individually.
        for index_name, indexes in self.index_groups.items():

            # Make a temporary "unified index" but only for this 1 index.
            isolated_index = ElasticsearchSearchEngine.unified_index()
            isolated_index.build(indexes=indexes)

            # Build the mappings for this index.
            self.content_field_name, field_mapping = self.build_schema(isolated_index.all_searchfields())
            current_mapping = {"modelresult": {"properties": field_mapping}}

            try:

                # Try to push those mappings into ElasticSearch.
                # Make sure the index is there first.
                self.conn.create_index(index_name, self.DEFAULT_SETTINGS)
                self.conn.put_mapping("modelresult", current_mapping, indexes=[index_name])
                self.existing_mapping = current_mapping

            except Exception as error:

                # Something went wrong.
                # Find out what the current mappings are in ElasticSearch.
                try:
                    self.existing_mapping = self.conn.get_mapping()[index_name]
                except KeyError:
                    pass
                except Exception:
                    if not self.silently_fail:
                        raise error

                if settings.DEBUG or settings.TEST_MODE or not self.silently_fail:
                    # Check for obvious conflicts, otherwise just raise the error.
                    try:
                        for field_name in find_conflicts(self.existing_mapping, current_mapping):
                            raise HaystackError(
                                "There is a mapping conflict for the %r field. Use the 'check_index' command."
                                % field_name
                            )
                        else:
                            raise error
                    except Exception:
                        raise error
                else:
                    logging.exception(log_function=logging.error)

        self.setup_complete = True
Esempio n. 6
0
    def setup(self):
        """
        Set up the mappings (aka schema) for the index.

        The mappings that are returned from pyelasticsearch are different to
        what Haystack builds, so there is no point in comparing it (which is
        what Haystack normally does).

        Also, it kept breaking when the index did not exist.

        Also, raise an exception and provide extra information if there are
        mapping conflicts, because they are a big deal and ruin everything.

        """

        self.setup_index_groups()

        # Set up each index group individually.
        for index_name, indexes in self.index_groups.items():

            # Make a temporary "unified index" but only for this 1 index.
            isolated_index = ElasticsearchSearchEngine.unified_index()
            isolated_index.build(indexes=indexes)

            # Build the mappings for this index.
            self.content_field_name, field_mapping = self.build_schema(
                isolated_index.all_searchfields())
            current_mapping = {'modelresult': {'properties': field_mapping}}

            try:

                # Try to push those mappings into ElasticSearch.
                # Make sure the index is there first.
                self.conn.create_index(index_name, self.DEFAULT_SETTINGS)
                self.conn.put_mapping('modelresult',
                                      current_mapping,
                                      indexes=[index_name])
                self.existing_mapping = current_mapping

            except Exception as error:

                # Something went wrong.
                # Find out what the current mappings are in ElasticSearch.
                try:
                    self.existing_mapping = self.conn.get_mapping()[index_name]
                except KeyError:
                    pass
                except Exception:
                    if not self.silently_fail:
                        raise error

                if settings.DEBUG or settings.TEST_MODE or not self.silently_fail:
                    # Check for obvious conflicts, otherwise just raise the error.
                    try:
                        for field_name in find_conflicts(
                                self.existing_mapping, current_mapping):
                            raise HaystackError(
                                "There is a mapping conflict for the %r field. Use the 'check_index' command."
                                % field_name)
                        else:
                            raise error
                    except Exception:
                        raise error
                else:
                    logging.exception(log_function=logging.error)

        self.setup_complete = True