Created
June 25, 2012 09:20
-
-
Save muratcorlu/2987596 to your computer and use it in GitHub Desktop.
Django haystack - elasticsearch kod örnekleri http://muratcorlu.com/post/djangoda-haystack-ve-elasticsearch-ile-arama/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SearchQuerySet().filter(user__iexact="murat").filter(content__startswith=request.GET.get('q'))[:10] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% literal %}{{ object.title }} | |
{{ object.author }}{% endliteral %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import haystack | |
haystack.autodiscover() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
python manage.py update_index blog.BlogPost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
urlpatterns = patterns('', | |
url(r'^blog/search/$', 'blog.search'), | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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