django-simplesearch /

Filename Size Date modified Message
411 B
36 B
423 B
2.8 KB
3.4 KB
217 B
1.0 KB

django-simplesearch

This is a simple search app. It's not a goal to make another google or to manage gazillion of documents in the database, but to have a easy to configure and intuitive to use search app for your django project.

Installation

Install django-simplesearch via pip:

$ pip install django-simplesearch

Configuration

Add the sub-apps from simplesearch to your INSTALLED_APPS in the settings.py:

INSTALLED_APPS = (
# ...
'simplesearch.index',
'simplesearch.query',
# ...
)

If you want to enable the standard search and don't want to use a custom search form, add one line to your urls.py:

urlpattern = pattern('',
# ...
include(r'^search', include('simplesearch.query.urls')),
# ...
)

Indexing

Hence you use the out of the box indexer, I will explain now how to fed it. Maybe you want to do:

  • Easily fead all instances of a model.
  • Exclude some fields.
  • Exclude some instances.

In your root-urlconf, activate the indexer:

from simplesearch import index
index.autodiscover()

In the file you've specified in the INDEXER_CONF variable in your settings.py (the default is indexer), you have to initialize the index:

from simplesearch import index

The easiest way to build the index is to add the whole project:

index.index_project()

What happens now? All models from all installed apps are examined, so that all indexable fields are used for the index. There's no single field or model-instance which is excluded of the search results.

For example TextFields are indexable, BooleanFields are not.

The more fine granular step is do index a whole app:

index.index_app('django.contrib.comments')

or just:

index.index_app('comments')

In both cases, all models are indexed. Even more fine granular is to index a very model of an app. Therefore just write:

from myapp.models import MyModel
index.index_model(MyModel)

All indexable fields of MyModel are indexed. Even yet more fine granular is to index only a subset of fields:

from myapp.models import MyModel
index.index_model(MyModel, 'my_text_field', 'my_char_field')

After all, you can tell the index not to index something:

index.index_app('django.contrib.comments', exlude=True)

or a model:

index.index_model(MyModel, exclude=True)

or some fields:

index.index_model(MyModel, 'my_text_field', exclude=True)

Note: Not indexing something outweighs indexing something!

For example, if you want to index a whole project, but don't want to index comments of the django.contrib.comments-app, just do:

index.index_project()
index.index_app('django.contrib.comments', exclude=True)

That's it about indexing.

Search Results

When I'm sure about indexing, I will take care about the query part.

Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.