from django.conf import settings

from haystack import indexes

from pombola.core import models as core_models

# TODO - currently this uses the realtime search index - which is possibly a bad
# idea if the site gets heavy load. Switch to a queue as suggested in the docs:
#   http://docs.haystacksearch.org/dev/best_practices.html#use-of-a-queue-for-a-better-user-experience

# TODO - all the search result html could be cached to save db access when
# displaying results: Not done initially as the templates will keep changing
# until the design is stable.
#   http://docs.haystacksearch.org/dev/best_practices.html#avoid-hitting-the-database

# Note - these indexes could be specified in the individual apps, which might
# well be cleaner.

class BaseIndex(indexes.SearchIndex):
    text = indexes.CharField(document=True, use_template=True)

class PersonIndex(BaseIndex, indexes.Indexable):
    name_auto = indexes.EdgeNgramField(model_attr='name')
    hidden = indexes.BooleanField(model_attr='hidden')

    def get_model(self):
        return core_models.Person

class PlaceIndex(BaseIndex, indexes.Indexable):
    name_auto = indexes.EdgeNgramField(model_attr='name')

    def get_model(self):
        return core_models.Place

class OrganisationIndex(BaseIndex, indexes.Indexable):
    name_auto = indexes.EdgeNgramField(model_attr='name')

    def get_model(self):
        return core_models.Organisation

class PositionTitleIndex(BaseIndex, indexes.Indexable):
    name_auto = indexes.EdgeNgramField(model_attr='name')

    def get_model(self):
        return core_models.PositionTitle

if settings.ENABLED_FEATURES['hansard']:
    from pombola.hansard import models as hansard_models

    class HansardEntryIndex(BaseIndex, indexes.Indexable):
        def get_model(self):
            return hansard_models.Entry

@@ -1817,8 +1817,266 @@
 s.Entry%0A
+%0Aif 'pombola.info' in settings.INSTALLED_APPS:%0A    from pombola.info.models import InfoPage%0A%0A    class InfoPageIndex(BaseIndex, indexes.Indexable):%0A        kind = indexes.CharField(model_attr='kind')%0A%0A        def get_model(self):%0A            return InfoPage%0A

Make sure info pages and blog posts are indexed in Elasticsearch