Skip to content

Instantly share code, notes, and snippets.

@muratcorlu muratcorlu/
Created Jun 25, 2012

What would you like to do?
Django haystack - elasticsearch kod örnekleri
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 }}
{{ }}{% 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
# Uncomment the next line to enable the admin:
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
'default': {
'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
'URL': '',
'INDEX_NAME': 'blog',
python update_index blog.BlogPost
urlpatterns = patterns('',
url(r'^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.