def test_add_extension(self): # Add some extensions. composer.add_extension('resolwe.elastic.tests.test_composer.MyExtendableType', 42) composer.add_extension('resolwe.elastic.tests.test_composer.MyExtendableType', 'foo') # Check that they are all here. extensions = composer.get_extensions(MyExtendableType) self.assertEqual(extensions, [42, 'foo']) extensions = composer.get_extensions('resolwe.elastic.tests.test_composer.MyExtendableType') self.assertEqual(extensions, [42, 'foo']) extensions = composer.get_extensions(MyExtendableType()) self.assertEqual(extensions, [42, 'foo'])
def custom_filter_text(self, value, search): """Support general query using the 'text' attribute.""" if isinstance(value, list): value = ' '.join(value) should = [ Q('match', slug={'query': value, 'operator': 'and', 'boost': 10.0}), Q('match', **{'slug.ngrams': {'query': value, 'operator': 'and', 'boost': 5.0}}), Q('match', name={'query': value, 'operator': 'and', 'boost': 10.0}), Q('match', **{'name.ngrams': {'query': value, 'operator': 'and', 'boost': 5.0}}), Q('match', contributor_name={'query': value, 'operator': 'and', 'boost': 5.0}), Q('match', **{'contributor_name.ngrams': {'query': value, 'operator': 'and', 'boost': 2.0}}), Q('match', owner_names={'query': value, 'operator': 'and', 'boost': 5.0}), Q('match', **{'owner_names.ngrams': {'query': value, 'operator': 'and', 'boost': 2.0}}), Q('match', descriptor_data={'query': value, 'operator': 'and'}), ] # Add registered text extensions. for extension in composer.get_extensions(self): if hasattr(extension, 'text_filter'): should += extension.text_filter(value) search = search.query('bool', should=should) return search
def test_add_extension(self): # Add some extensions. composer.add_extension( "resolwe.elastic.tests.test_composer.MyExtendableType", 42) composer.add_extension( "resolwe.elastic.tests.test_composer.MyExtendableType", "foo") # Check that they are all here. extensions = composer.get_extensions(MyExtendableType) self.assertEqual(extensions, [42, "foo"]) extensions = composer.get_extensions( "resolwe.elastic.tests.test_composer.MyExtendableType") self.assertEqual(extensions, [42, "foo"]) extensions = composer.get_extensions(MyExtendableType()) self.assertEqual(extensions, [42, "foo"])
def custom_filter_text(self, value, search): """Support general query using the 'text' attribute.""" if isinstance(value, list): value = " ".join(value) should = [ Q("match", slug={"query": value, "operator": "and", "boost": 10.0}), Q( "match", **{"slug.ngrams": {"query": value, "operator": "and", "boost": 5.0}}, ), Q("match", name={"query": value, "operator": "and", "boost": 10.0}), Q( "match", **{"name.ngrams": {"query": value, "operator": "and", "boost": 5.0}}, ), Q( "match", contributor_name={"query": value, "operator": "and", "boost": 5.0}, ), Q( "match", **{ "contributor_name.ngrams": { "query": value, "operator": "and", "boost": 2.0, } }, ), Q("match", owner_names={"query": value, "operator": "and", "boost": 5.0}), Q( "match", **{ "owner_names.ngrams": { "query": value, "operator": "and", "boost": 2.0, } }, ), Q("match", descriptor_data={"query": value, "operator": "and"}), Q("match", description={"query": value, "operator": "and"}), ] # Add registered text extensions. for extension in composer.get_extensions(self): if hasattr(extension, "text_filter"): should += extension.text_filter(value) search = search.query("bool", should=should) return search
def __init__(self, *args, **kwargs): """Construct Data viewset.""" # Add registered viewset extensions. We take care not to modify the original # class-derived attributes. self.ordering_map = self.ordering_map.copy() for extension in composer.get_extensions(self): filtering_fields = getattr(extension, 'filtering_fields', []) filtering_map = getattr(extension, 'filtering_map', {}) ordering_fields = getattr(extension, 'ordering_fields', []) ordering_map = getattr(extension, 'ordering_map', {}) self.filtering_fields = self.filtering_fields + tuple( filtering_fields) self.filtering_map.update(filtering_map) self.ordering_fields = self.ordering_fields + tuple( ordering_fields) self.ordering_map.update(ordering_map) super().__init__(*args, **kwargs)