class ReuseSearch(ModelSearchAdapter): model = Reuse fuzzy = True fields = ( 'title^4', 'description^2', 'datasets.title', ) facets = { 'tag': TermFacet('tags'), 'organization': ModelTermFacet('organization', Organization), 'owner': ModelTermFacet('owner', User), 'dataset': ModelTermFacet('dataset.id', Dataset), 'type': ReuseTypeFacet('type'), 'datasets': RangeFacet('metrics.datasets'), 'followers': RangeFacet('metrics.followers'), 'featured': BoolFacet('featured'), 'extra': ExtrasFacet('extras'), 'badge': TermFacet('badges', labelizer=reuse_badge_labelizer), } sorts = { 'title': Sort('title.raw'), 'created': Sort('created'), 'last_modified': Sort('last_modified'), 'datasets': Sort('metrics.datasets'), 'followers': Sort('metrics.followers'), 'views': Sort('metrics.views'), } mapping = { 'properties': { 'title': { 'type': 'string', 'analyzer': i18n_analyzer, 'fields': { 'raw': { 'type': 'string', 'index': 'not_analyzed' } } }, 'description': { 'type': 'string', 'analyzer': i18n_analyzer }, 'url': { 'type': 'string' }, 'organization': { 'type': 'string' }, 'owner': { 'type': 'string' }, 'type': { 'type': 'string' }, 'tags': { 'type': 'string', 'index_name': 'tag', 'index': 'not_analyzed' }, 'tag_suggest': { 'type': 'completion', 'index_analyzer': 'simple', 'search_analyzer': 'simple', 'payloads': False, }, 'badges': { 'type': 'string', 'index_name': 'badges', 'index': 'not_analyzed' }, 'created': { 'type': 'date', 'format': 'date_hour_minute_second' }, 'last_modified': { 'type': 'date', 'format': 'date_hour_minute_second' }, 'dataset': { 'type': 'object', 'properties': { 'id': { 'type': 'string' }, 'title': { 'type': 'string' } } }, 'metrics': metrics_mapping(Reuse), 'featured': { 'type': 'boolean' }, 'reuse_suggest': { 'type': 'completion', 'index_analyzer': 'simple', 'search_analyzer': 'simple', 'payloads': True, }, 'extras': { 'type': 'object', 'index_name': 'extra', }, } } boosters = [ BoolBooster('featured', 1.1), GaussDecay('metrics.datasets', max_datasets, decay=0.8), GaussDecay('metrics.followers', max_followers, decay=0.8), ] @classmethod def is_indexable(cls, reuse): return (reuse.deleted is None and len(reuse.datasets) > 0 and not reuse.private) @classmethod def serialize(cls, reuse): """By default use the ``to_dict`` method and exclude ``_id``, ``_cls`` and ``owner`` fields. """ return { 'title': reuse.title, 'description': reuse.description, 'url': reuse.url, 'organization': (str(reuse.organization.id) if reuse.organization else None), 'owner': str(reuse.owner.id) if reuse.owner else None, 'type': reuse.type, 'tags': reuse.tags, 'tag_suggest': reuse.tags, 'badges': [badge.kind for badge in reuse.badges], 'created': reuse.created_at.strftime('%Y-%m-%dT%H:%M:%S'), 'last_modified': reuse.last_modified.strftime('%Y-%m-%dT%H:%M:%S'), 'dataset': [{ 'id': str(d.id), 'title': d.title } for d in reuse.datasets if isinstance(d, Dataset)], 'metrics': reuse.metrics, 'featured': reuse.featured, 'extras': reuse.extras, 'reuse_suggest': { 'input': cls.completer_tokenize(reuse.title) + [reuse.id], 'output': str(reuse.id), 'payload': { 'title': reuse.title, 'slug': reuse.slug, 'image_url': reuse.image(40), }, }, }
class ReuseSearch(ModelSearchAdapter): model = Reuse fuzzy = True class Meta: doc_type = 'Reuse' title = String(analyzer=i18n_analyzer, fields={'raw': String(index='not_analyzed')}) description = String(analyzer=i18n_analyzer) url = String(index='not_analyzed') organization = String(index='not_analyzed') owner = String(index='not_analyzed') type = String(index='not_analyzed') tags = String(index='not_analyzed', fields={'i18n': String(index='not_analyzed')}) badges = String(index='not_analyzed') topic = String(index='not_analyzed') tag_suggest = Completion(analyzer=simple, search_analyzer=simple, payloads=False) datasets = Object(properties={ 'id': String(index='not_analyzed'), 'title': String(), }) created = Date(format='date_hour_minute_second') last_modified = Date(format='date_hour_minute_second') metrics = Reuse.__search_metrics__ featured = Boolean() reuse_suggest = Completion(analyzer=simple, search_analyzer=simple, payloads=True) extras = Object() facets = { 'tag': TermsFacet(field='tags'), 'organization': ModelTermsFacet(field='organization', model=Organization), 'owner': ModelTermsFacet(field='owner', model=User), 'dataset': ModelTermsFacet(field='dataset.id', model=Dataset), 'type': TermsFacet(field='type', labelizer=reuse_type_labelizer), 'datasets': RangeFacet(field='metrics.datasets', ranges=[('none', (None, 1)), ('few', (1, 5)), ('many', (5, None))], labels={ 'none': _('No datasets'), 'few': _('Few datasets'), 'many': _('Many datasets'), }), 'followers': RangeFacet(field='metrics.followers', ranges=[('none', (None, 1)), ('few', (1, 5)), ('many', (5, None))], labels={ 'none': _('No followers'), 'few': _('Few followers'), 'many': _('Many followers'), }), 'badge': TermsFacet(field='badges', labelizer=reuse_badge_labelizer), 'featured': BoolFacet(field='featured'), 'topic': TermsFacet(field='topic', labelizer=reuse_topic_labelizer), } sorts = { 'title': 'title.raw', 'created': 'created', 'last_modified': 'last_modified', 'datasets': 'metrics.datasets', 'followers': 'metrics.followers', 'views': 'metrics.views', } boosters = [ BoolBooster('featured', lazy('featured_boost')), GaussDecay('metrics.datasets', max_datasets, decay=lazy('datasets_decay')), GaussDecay('metrics.followers', max_followers, decay=lazy('followers_decay')), ] @classmethod def is_indexable(cls, reuse): return (reuse.deleted is None and len(reuse.datasets) > 0 and not reuse.private) @classmethod def serialize(cls, reuse): """By default use the ``to_dict`` method and exclude ``_id``, ``_cls`` and ``owner`` fields. """ datasets = Dataset.objects(id__in=[r.id for r in reuse.datasets]) datasets = list(datasets.only('id', 'title').no_dereference()) organization = None owner = None if reuse.organization: organization = Organization.objects( id=reuse.organization.id).first() elif reuse.owner: owner = User.objects(id=reuse.owner.id).first() return { 'title': reuse.title, 'description': reuse.description, 'url': reuse.url, 'organization': str(organization.id) if organization else None, 'owner': str(owner.id) if owner else None, 'type': reuse.type, 'topic': reuse.topic, 'tags': reuse.tags, 'tag_suggest': reuse.tags, 'badges': [badge.kind for badge in reuse.badges], 'created': to_iso_datetime(reuse.created_at), 'last_modified': to_iso_datetime(reuse.last_modified), 'dataset': [{ 'id': str(d.id), 'title': d.title } for d in datasets], 'metrics': reuse.metrics, 'featured': reuse.featured, 'extras': reuse.extras, 'reuse_suggest': { 'input': cls.completer_tokenize(reuse.title) + [reuse.id], 'output': str(reuse.id), 'payload': { 'title': reuse.title, 'slug': reuse.slug, 'image_url': reuse.image(500, external=True), }, }, }