From a2485980df55f14ea45ce47d9ca6427461ea94de Mon Sep 17 00:00:00 2001 From: Sean McArthur Date: Fri, 5 Aug 2011 22:29:29 -0700 Subject: [PATCH] adding SearchMixin, and changing Package _type in ES to use 'jetpack_package' instead of 'addon' and 'library' --- apps/jetpack/models.py | 7 ++++--- apps/search/cron.py | 25 +++++++++++++++++++++++++ apps/search/models.py | 7 +++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 apps/search/models.py diff --git a/apps/jetpack/models.py b/apps/jetpack/models.py index 8c86a511..8ad69bf7 100644 --- a/apps/jetpack/models.py +++ b/apps/jetpack/models.py @@ -36,6 +36,7 @@ from jetpack.errors import (SelfDependencyException, FilenameExistException, UpdateDeniedException, SingletonCopyException, DependencyException, AttachmentWriteException) from jetpack.managers import PackageManager +from search.models import SearchMixin from utils import validator from utils.exceptions import SimpleException from utils.helpers import pathify, alphanum, alphanum_plus @@ -1268,7 +1269,7 @@ class PackageRevision(BaseModel): return self.pk == self.package.latest.pk -class Package(BaseModel): +class Package(BaseModel, SearchMixin): """ Holds the meta data shared across all PackageRevisions """ @@ -1623,7 +1624,7 @@ class Package(BaseModel): pass try: - es.index(data, settings.ES_INDEX, self.get_type_name(), self.id, + es.index(data, settings.ES_INDEX, self._meta.db_table, id=self.id, bulk=bulk) except Exception, e: log.error("ElasticSearch errored for addon (%s): %s" % (self, e)) @@ -1641,7 +1642,7 @@ class Package(BaseModel): @es_required def remove_from_index(self, es, bulk=False): try: - es.delete(settings.ES_INDEX, self.get_type_name(), self.id, + es.delete(settings.ES_INDEX, self._meta.db_table, id=self.id, bulk=bulk) except PyesNotFoundException: log.debug('Package %d tried to remove from index but was not found.' diff --git a/apps/search/cron.py b/apps/search/cron.py index f6ba66c4..fa396fc5 100644 --- a/apps/search/cron.py +++ b/apps/search/cron.py @@ -1,7 +1,11 @@ import commonware.log import cronjobs +from pyes.exceptions import ElasticSearchException from celery.messaging import establish_connection from celeryutils import chunked +from elasticutils import get_es + +from django.conf import settings from jetpack.models import Package from search import tasks @@ -17,3 +21,24 @@ def index_all(): with establish_connection() as conn: for chunk in chunked(ids, 100): tasks.index_all.apply_async(args=[chunk], connection=conn) + +@cronjobs.register +def setup_mapping(): + """Create index, and setup mapping, for ES.""" + + package_mapping = { + 'properties': { + # type is only ever 'a' or 'l', and we do exact matchs. + # 'a' gets analyzed otherwise + 'type': {'type': 'string', 'index': 'not_analyzed'}, + }, + } + + es = get_es() + try: + es.create_index_if_missing(settings.ES_INDEX) + es.put_mapping(Package._meta.db_table, package_mapping, + settings.ES_INDEX) + except ElasticSearchException, e: + log.debug(e) + diff --git a/apps/search/models.py b/apps/search/models.py new file mode 100644 index 00000000..d74f0b2f --- /dev/null +++ b/apps/search/models.py @@ -0,0 +1,7 @@ +from elasticutils import S + +class SearchMixin(object): + + @classmethod + def search(cls): + return S(cls)