Skip to content

Commit

Permalink
Add search
Browse files Browse the repository at this point in the history
  • Loading branch information
norm committed Jul 24, 2015
1 parent 98fbda2 commit 832eb2d
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
*.pyc *.pyc
xapian_index
23 changes: 23 additions & 0 deletions README.markdown
Expand Up @@ -22,6 +22,29 @@ Setup the database tables:


python manage.py migrate python manage.py migrate


Search uses xapian, so that needs to be installed:

* On ubuntu:

sudo apt-get install python-xapian

* On OSX:

First install xapian.

brew install xapian

Then in the virtualenv, install the python xapian bindings for the
version of xapian you have installed.

XAPIAN_VERSION=1.2.19
wget http://oligarchy.co.uk/xapian/${XAPIAN_VERSION}/xapian-bindings-${XAPIAN_VERSION}.tar.xz
tar xJf xapian-bindings-${XAPIAN_VERSION}.tar.xz
cd xapian-bindings-${XAPIAN_VERSION}
./configure --with-python
make
sudo make install

Run the app: Run the app:


honcho start honcho start
Expand Down
11 changes: 11 additions & 0 deletions apps/bookmarks/search_indexes.py
@@ -0,0 +1,11 @@
from haystack import indexes

from .models import Bookmark


class BookmarkIndex(indexes.SearchIndex, indexes.Indexable):
title = indexes.CharField(model_attr='title')
text = indexes.CharField(document=True, use_template=True)

def get_model(self):
return Bookmark
8 changes: 8 additions & 0 deletions evocation/settings.py
Expand Up @@ -41,6 +41,7 @@
'apps.bookmarks', 'apps.bookmarks',


'taggit', 'taggit',
'haystack',
) )


MIDDLEWARE_CLASSES = ( MIDDLEWARE_CLASSES = (
Expand Down Expand Up @@ -110,3 +111,10 @@
STATIC_URL = '/static/' STATIC_URL = '/static/'


TAGGIT_CASE_INSENSITIVE = True TAGGIT_CASE_INSENSITIVE = True

HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'xapian_backend.XapianEngine',
'PATH': os.path.join(os.path.dirname(__file__), 'xapian_index'),
},
}
2 changes: 2 additions & 0 deletions evocation/urls.py
Expand Up @@ -83,4 +83,6 @@
TaggedList.as_view(), TaggedList.as_view(),
name='tagged-list', name='tagged-list',
), ),

url(r'^search/', include('haystack.urls')),
] ]
2 changes: 2 additions & 0 deletions requirements.txt
Expand Up @@ -2,3 +2,5 @@ Django==1.8.3
honcho==0.6.6 honcho==0.6.6
psycopg2==2.6.1 psycopg2==2.6.1
django-taggit==0.16.2 django-taggit==0.16.2
django-haystack==2.4.0
git+https://github.com/notanumber/xapian-haystack.git
3 changes: 3 additions & 0 deletions templates/search/indexes/bookmarks/bookmark_text.txt
@@ -0,0 +1,3 @@
{{object.title}}
{{object.description}}
{{object.url}}
35 changes: 35 additions & 0 deletions templates/search/search.html
@@ -0,0 +1,35 @@
<h2>Search</h2>

<form method="get" action=".">
<table>
{{ form.as_table }}
<tr>
<td>&nbsp;</td>
<td>
<input type="submit" value="Search">
</td>
</tr>
</table>

{% if query %}
<h3>Results</h3>

{% for result in page.object_list %}
<p>
<a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
</p>
{% empty %}
<p>No results found.</p>
{% endfor %}

{% if page.has_previous or page.has_next %}
<div>
{% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
|
{% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
</div>
{% endif %}
{% else %}
{# Show some example queries to run, maybe query syntax, something else? #}
{% endif %}
</form>

0 comments on commit 832eb2d

Please sign in to comment.