adding SearchMixin, and changing Package _type in ES to use 'jetpack_package' instead of 'addon' and 'library'

This commit is contained in:
Sean McArthur 2011-08-05 22:29:29 -07:00
Родитель f46bb9953b
Коммит a2485980df
3 изменённых файлов: 36 добавлений и 3 удалений

Просмотреть файл

@ -36,6 +36,7 @@ from jetpack.errors import (SelfDependencyException, FilenameExistException,
UpdateDeniedException, SingletonCopyException, UpdateDeniedException, SingletonCopyException,
DependencyException, AttachmentWriteException) DependencyException, AttachmentWriteException)
from jetpack.managers import PackageManager from jetpack.managers import PackageManager
from search.models import SearchMixin
from utils import validator from utils import validator
from utils.exceptions import SimpleException from utils.exceptions import SimpleException
from utils.helpers import pathify, alphanum, alphanum_plus from utils.helpers import pathify, alphanum, alphanum_plus
@ -1268,7 +1269,7 @@ class PackageRevision(BaseModel):
return self.pk == self.package.latest.pk return self.pk == self.package.latest.pk
class Package(BaseModel): class Package(BaseModel, SearchMixin):
""" """
Holds the meta data shared across all PackageRevisions Holds the meta data shared across all PackageRevisions
""" """
@ -1623,7 +1624,7 @@ class Package(BaseModel):
pass pass
try: 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) bulk=bulk)
except Exception, e: except Exception, e:
log.error("ElasticSearch errored for addon (%s): %s" % (self, e)) log.error("ElasticSearch errored for addon (%s): %s" % (self, e))
@ -1641,7 +1642,7 @@ class Package(BaseModel):
@es_required @es_required
def remove_from_index(self, es, bulk=False): def remove_from_index(self, es, bulk=False):
try: 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) bulk=bulk)
except PyesNotFoundException: except PyesNotFoundException:
log.debug('Package %d tried to remove from index but was not found.' log.debug('Package %d tried to remove from index but was not found.'

Просмотреть файл

@ -1,7 +1,11 @@
import commonware.log import commonware.log
import cronjobs import cronjobs
from pyes.exceptions import ElasticSearchException
from celery.messaging import establish_connection from celery.messaging import establish_connection
from celeryutils import chunked from celeryutils import chunked
from elasticutils import get_es
from django.conf import settings
from jetpack.models import Package from jetpack.models import Package
from search import tasks from search import tasks
@ -17,3 +21,24 @@ def index_all():
with establish_connection() as conn: with establish_connection() as conn:
for chunk in chunked(ids, 100): for chunk in chunked(ids, 100):
tasks.index_all.apply_async(args=[chunk], connection=conn) 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)

7
apps/search/models.py Normal file
Просмотреть файл

@ -0,0 +1,7 @@
from elasticutils import S
class SearchMixin(object):
@classmethod
def search(cls):
return S(cls)