def search(self, query_string, user, global_and_search=False):
        elapsed_time = 0
        start_time = datetime.datetime.now()
        result_set = set()
        search_dict = {}

        if not self.model:
            self.model = apps.get_model(self.app_label, self.model_name)
            if not self.label:
                self.label = self.model._meta.verbose_name

        if 'q' in query_string:
            # Simple search
            for search_field in self.get_all_search_fields():
                search_dict.setdefault(search_field.get_model(), {
                    'searches': [],
                    'label': search_field.label,
                    'return_value': search_field.return_value
                })
                search_dict[search_field.get_model()]['searches'].append(
                    {
                        'field_name': [search_field.field],
                        'terms': self.normalize_query(
                            query_string.get('q', '').strip()
                        )
                    }
                )
        else:

            for search_field in self.get_all_search_fields():
                if search_field.field in query_string and query_string[search_field.field]:
                    search_dict.setdefault(search_field.get_model(), {
                        'searches': [],
                        'label': search_field.label,
                        'return_value': search_field.return_value
                    })
                    search_dict[search_field.get_model()]['searches'].append(
                        {
                            'field_name': [search_field.field],
                            'terms': self.normalize_query(
                                query_string[search_field.field]
                            )
                        }
                    )

        for model, data in search_dict.items():
            logger.debug('model: %s', model)

            # Initialize per model result set
            model_result_set = set()

            for query_entry in data['searches']:
                # Fashion a list of queries for a field for each term
                field_query_list = self.assemble_query(
                    query_entry['terms'], query_entry['field_name']
                )

                logger.debug('field_query_list: %s', field_query_list)

                # Initialize per field result set
                field_result_set = set()

                # Get results per search field
                for query in field_query_list:
                    logger.debug('query: %s', query)
                    term_query_result_set = set(
                        model.objects.filter(query).values_list(
                            data['return_value'], flat=True
                        )
                    )

                    # Convert the QuerySet to a Python set and perform the
                    # AND operation on the program and not as a query.
                    # This operation ANDs all the field term results
                    # belonging to a single model, making sure to only include
                    # results in the final field result variable if all the
                    # terms are found in a single field.
                    if not field_result_set:
                        field_result_set = term_query_result_set
                    else:
                        field_result_set &= term_query_result_set

                    logger.debug(
                        'term_query_result_set: %s', term_query_result_set
                    )
                    logger.debug('field_result_set: %s', field_result_set)

                if global_and_search:
                    if not model_result_set:
                        model_result_set = field_result_set
                    else:
                        model_result_set &= field_result_set
                else:
                    model_result_set |= field_result_set

            result_set = result_set | model_result_set

        elapsed_time = unicode(
            start_time - datetime.datetime.now()
        ).split(':')[2]

        queryset = self.model.objects.filter(
            pk__in=list(result_set)[:setting_limit.value]
        )

        if self.permission:
            try:
                Permission.check_permissions(user, [self.permission])
            except PermissionDenied:
                queryset = AccessControlList.objects.filter_by_access(
                    self.permission, user, queryset
                )

        RecentSearch.objects.add_query_for_user(
            user, query_string, len(result_set)
        )

        return queryset, result_set, elapsed_time
dataset/ETHPy150Open mayan-edms/mayan-edms/mayan/apps/dynamic_search/classes.py/SearchModel.search/swapped operands of `-`