Skip to content

Instantly share code, notes, and snippets.

@muratcorlu muratcorlu/filter.py
Created Jun 25, 2012

Embed
What would you like to do?
Django haystack - elasticsearch kod örnekleri http://muratcorlu.com/post/djangoda-haystack-ve-elasticsearch-ile-arama/
SearchQuerySet().filter(user__iexact="murat").filter(content__startswith=request.GET.get('q'))[:10]
from django.db import models
from django.contrib.auth.models import User
class BlogPost(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(User)
content = models.TextField()
create_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
{% literal %}{{ object.title }}
{{ object.author }}{% endliteral %}
from haystack import indexes
from blog.models import BlogPost
class PostIndex(indexes.RealTimeSearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
title = indexes.CharField(model_attr='title')
def get_model(self):
return BlogPost
import haystack
haystack.autodiscover()
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'haystack',
)
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': 'http://127.0.0.1:9200/',
'INDEX_NAME': 'blog',
},
}
python manage.py update_index blog.BlogPost
urlpatterns = patterns('',
url(r'^blog/search/$', 'blog.search'),
)
from django.http import HttpResponse
from haystack.query import SearchQuerySet
def search(request):
results = SearchQuerySet().filter(content__startswith=request.GET.get('q'))[:10]
results_text = '\n'.join([row.title for row in results])
return HttpResponse(results_text, content_type="text/plain")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.