Skip to content

Commit

Permalink
mostly docs
Browse files Browse the repository at this point in the history
  • Loading branch information
benzkji committed Jun 12, 2024
1 parent 5a7a661 commit b52915c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
51 changes: 49 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@ in searchindex key and kwargs.
Example, hopefully self explaining.

```python
import html

from django.utils.html import strip_tags
from postgres_searchindex.base import IndexSource / MultiLanguageIndexSource
from postgres_searchindex.source_pool import source_pool

from news.models import News


@source_pool.register
class NewsIndexSource(IndexSource / MultiLanguageIndexSource):
model = News
Expand All @@ -74,11 +75,12 @@ class NewsIndexSource(IndexSource / MultiLanguageIndexSource):
return strip_tags(obj.description)

def get_content(self, obj):
return strip_tags(obj.description)
return html.unescape(strip_tags(obj.description))

def get_queryset(self):
return self.model.objects.published()
```

Place this code in `index_sources.py` of your app, and it will be autodiscovered.

### Populate the index
Expand Down Expand Up @@ -142,6 +144,51 @@ There are ~~two~~ currently ~~one~~ none (not yet) builtin processors:
The async signal processor will require you to have celery configured.


## contrib.djangocms

A few tools to speed up indexing of django-cms sites. If you want to use the search app hook and/or index your
cms pages, you'll need to

### AppHook

Add `postgres_searchindex.contrib.djangocms` to `settings.INSTALLED_APPS`.
Configure one of your cms pages to use the app hook "Search Form (postgres_searchindex)". It will provide a very
basic search form, and you can override the template `postgres_searchindex/search.html` if you want.

### Indexing of cms pages

Add `postgres_searchindex.contrib.djangocms` to `settings.INSTALLED_APPS`.
And set `settings.POSTGRES_SEARCHINDEX_USE_CMS_INDEX = True` to have your django-cms pages indexed automagically (with the next call of
`./manage.py postgres_searchindex_rebuild`). See also

### Indexing models with a PlaceholderField

Example `Event` model, with a `PlaceholderField` called "content":

```python
import html

from django.utils.html import strip_tags
from postgres_searchindex.base import MultiLanguageIndexSource
from postgres_searchindex.contrib.djangocms.base import PlaceholderIndexSourceMixin

from .models import Event

@source_pool.register
class EventIndexSource(PlaceholderIndexSourceMixin, MultiLanguageIndexSource):
model = Event
placeholder_field_name = "content"

def get_content(self, obj):
c = strip_tags(obj.description) # prepend with preview/description
c += super().get_content(obj) # render placeholder
c = html.unescape(c) # convert & to "
return c

def get_queryset(self):
return self.model.objects.published()
```

## Inspired

I used django-haystack for a decade, and I really like the concept. Building my first index though,
Expand Down
2 changes: 1 addition & 1 deletion postgres_searchindex/contrib/djangocms/cms_apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

@apphook_pool.register
class SearchAppHook(CMSApp):
name = _("Search Form")
name = _("Search Form (postgres_searchindex)")
# menus = [CategoryMenu, ]

def get_urls(self, page=None, language=None, **kwargs):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
# commands/reindex_indexes.py
from django.core.management import BaseCommand

from ._utils import delete_indexes, update_indexes
from postgres_searchindex.management.indexing import delete_indexes, update_indexes


class Command(BaseCommand):
help = "Reindexing postgres "
help = "rebuild index (first delete, then update)"

def add_arguments(self, parser):
parser.add_argument(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from django.core.management import BaseCommand

from ._utils import update_indexes
from postgres_searchindex.management.indexing import update_indexes


class Command(BaseCommand):
help = "Update/build index"
help = "update index"

def handle(self, *args, **options):
update_indexes()
File renamed without changes.

0 comments on commit b52915c

Please sign in to comment.