{"id":188242,"date":"2013-07-29T19:50:05","date_gmt":"2013-07-29T15:50:05","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=188242"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=188242","title":{"rendered":"<span class=\"post_title\">\u041f\u043e\u0438\u0441\u043a \u0447\u0435\u0440\u0435\u0437 sphinx \u0432 django 1.6 admin<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t\u0417\u0430\u0434\u0430\u0447\u0430: \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0432 \u0430\u0434\u043c\u0438\u043d\u043a\u0435 django.<br \/>  \u041f\u0440\u0438\u043c\u0435\u0440 \u043c\u043e\u0434\u0435\u043b\u0438, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e\u0438\u0441\u043a:  <\/p>\n<pre><code class=\"python\">class Movie(models.Model):     title_en = models.CharField(max_length=255, null=True)     title_ru = models.CharField(max_length=255, null=True) <\/code><\/pre>\n<p>  <a name=\"habracut\"><\/a>  <\/p>\n<h4>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 sphinx<\/h4>\n<p>  \u0421\u0442\u0430\u0432\u0438\u043c sphinx \u0447\u0435\u0440\u0435\u0437 \u043b\u044e\u0431\u043e\u0439 \u043f\u0430\u043a\u0435\u0442\u043d\u044b\u0439 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440, \u0443 \u043c\u0435\u043d\u044f \u043f\u043e\u0434 \u0440\u0443\u043a\u043e\u0439 \u0442\u043e\u043b\u044c\u043a\u043e FreeBSD, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0441\u0442\u0430\u0432\u043b\u044e \u0438\u0437 \u043f\u043e\u0440\u0442\u043e\u0432.  <\/p>\n<pre><code class=\"bash\">cd \/usr\/ports\/textproc\/sphinxsearch make install clean <\/code><\/pre>\n<p>  \u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u0435\u0440\u0441\u0438\u044f 0.9.9, \u0441 \u043d\u0435\u0439 \u0442\u043e\u0447\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043f\u0430\u043a\u0435\u0442 django-sphinx, \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0441\u0438\u044f\u0445 \u043d\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u043b.<br \/>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u0438\u0433 \/usr\/local\/etc\/sphinx.conf  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0421\u043a\u0440\u044b\u0442\u044b\u0439 \u0442\u0435\u043a\u0441\u0442<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"bash\"># \u0418\u0441\u0442\u043e\u0447\u043d\u0438\u043a source movies_src {     type = pgsql # \u0438\u043b\u0438 mysql     sql_host = 127.0.0.1     sql_user = sphinx     sql_pass = sphinxpass     sql_db = dbname     sql_port = 5432     sql_query = select id, title_en, title_ru, year from movies_movie # \u0437\u0430\u043f\u0440\u043e\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u0430     sql_attr_uint = year }  # \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 index movies {     source = movies_src     path = \/var\/data\/search\/movies     docinfo = extern     charset_type = utf-8     morphology = stem_enru # \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0440\u0443\u0441\u0441\u043a\u0443\u044e \u0438 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0443\u044e \u043c\u043e\u0440\u0444\u043e\u043b\u043e\u0433\u0438\u044e }  # \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0434\u0435\u043c\u043e\u043d\u0430 searchd {     listen = 9312     log = \/var\/log\/searchd.log     query_log = \/var\/log\/searchd.query.log     read_timeout = 3     client_timeout = 10     max_children = 15     pid_file = \/var\/run\/sphinxsearch\/searchd.pid     max_matches = 1000     seamless_rotate = 1     preopen_indexes = 1     unlink_old = 1     mva_updates_pool = 1M     max_packet_size = 8M } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u043b\u043e\u0433-\u0444\u0430\u0439\u043b\u044b \u0438 \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u0438\u0437 \u043a\u043e\u043d\u0444\u0438\u0433\u0430, \u0434\u0435\u043b\u0430\u0435\u043c \u0438\u043c chown _sphinx. \u0415\u0441\u043b\u0438 \u0432\u044b \u0442\u043e\u0436\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 postgres \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u0447\u043a\u0443 \u0432 pg_hba.conf, \u0447\u0442\u043e\u0431\u044b \u0441\u0444\u0438\u043d\u043a\u0441 \u0441\u043c\u043e\u0433 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f. <br \/>  \u0412\u044b\u0434\u0430\u0435\u043c \u044e\u0437\u0435\u0440\u0443 \u043f\u0440\u0430\u0432\u0430 \u043d\u0430 \u0447\u0442\u0435\u043d\u0438\u0435 \u0438\u0437 \u043d\u0443\u0436\u043d\u043e\u0439 \u043d\u0430\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u044b. \u041f\u0440\u0438\u0432\u043e\u0436\u0443 \u043f\u0440\u0438\u043c\u0435\u0440 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0433\u0440\u0435\u0441\u0430, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0433\u0443\u0433\u043b\u0438\u0442\u044c \u043b\u0438\u0448\u043d\u0438\u0439 \u0440\u0430\u0437:  <\/p>\n<pre><code class=\"sql\">GRANT CONNECT ON DATABASE mydb TO sphinx; GRANT USAGE ON SCHEMA public TO sphinx; GRANT SELECT ON movies_movie TO sphinx; <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u0430\u0442\u043e\u0440 \u0438 \u0441\u0442\u0430\u0432\u0438\u043c \u0435\u0433\u043e \u0432 \u043a\u0440\u043e\u043d  <\/p>\n<pre><code class=\"bash\">\/usr\/local\/bin\/indexer --config \/usr\/local\/etc\/sphinx.conf  --rotate --all &gt;\/dev\/null 2&gt;&1 <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c sphinx  <\/p>\n<pre><code class=\"bash\">\/usr\/local\/etc\/rc.d\/sphinxsearch start <\/code><\/pre>\n<p>  <\/p>\n<h4>\u0418\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f \u0441 django<\/h4>\n<p>  \u0421\u0442\u0430\u0432\u0438\u043c \u043f\u0430\u043a\u0435\u0442 <a href=\"https:\/\/github.com\/dcramer\/django-sphinx\">django-sphinx<\/a>  <\/p>\n<pre><code class=\"bash\">pip install django-sphinx <\/code><\/pre>\n<p>  \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c &#8216;djangosphinx&#8217; \u0432 INSTALLED_APPS<br \/>  \u041f\u0440\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0432 settings.py  <\/p>\n<pre><code class=\"python\">SPHINX_API_VERSION = 0x116 # \u0434\u043b\u044f sphinx 0.9.9+ SPHINX_PORT = 9312 SPHINX_SERVER = '127.0.0.1' <\/code><\/pre>\n<p>  \u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u043c\u043e\u0434\u0435\u043b\u044c Sphinx-\u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440  <\/p>\n<pre><code class=\"python\">class Movie(models.Model):     search = SphinxSearch(         index='movies',         weights={             'title_en': 100,             'title_ru': 100,         }     ) <\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0447\u0442\u043e \u043f\u043e\u043b\u043d\u043e\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.  <\/p>\n<pre><code class=\"python\">&gt;&gt;&gt; Movie.search.query(u'\u0437\u0435\u043b\u0435\u043d\u044b\u0439').order_by('year')[0] &lt;Movie: The Green Mile (1999) - \u0417\u0435\u043b\u0435\u043d\u0430\u044f \u043c\u0438\u043b\u044f&gt; <\/code><\/pre>\n<p>  <\/p>\n<h4>\u041f\u043e\u0438\u0441\u043a \u0447\u0435\u0440\u0435\u0437 sphinx \u0432 \u0430\u0434\u043c\u0438\u043d\u043a\u0435<\/h4>\n<p>  \u0423 django-sphinx \u043a\u0430\u043a \u0431\u044b \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0439 \u043a\u043b\u0430\u0441\u0441 SphinxModelAdmin, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0444\u0438\u043d\u043a\u0441 \u0432 \u0448\u0442\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0438\u0441\u043a\u0435 \u0430\u0434\u043c\u0438\u043d\u043a\u0438.<br \/>  \u041d\u043e \u0432\u043e\u0442 \u0447\u0442\u043e \u043f\u0438\u0448\u0435\u0442 \u0441\u0430\u043c \u0430\u0432\u0442\u043e\u0440 \u043f\u0440\u043e \u044d\u0442\u0443 \u0444\u0438\u0447\u0443:  <\/p>\n<ul>\n<li>Only shows your max sphinx results (defaults to 1000)<\/li>\n<li>Filters currently don&#8217;t work.<\/li>\n<li>This is a huge hack, so it may or may not continue working when Django updates.<\/li>\n<\/ul>\n<p>  \u0422\u0430\u043a\u043e\u0435 \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442, \u0442\u0430\u043a \u0447\u0442\u043e \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0434\u043e\u043f\u0438\u043b\u0438\u0432\u0430\u0442\u044c.<br \/>  \u0412 django 1.6 \u0443 \u043a\u043b\u0430\u0441\u0441\u0430 ModelAdmin \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u043c\u0435\u0442\u043e\u0434 get_search_results, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043f\u043e\u0438\u0441\u043a\u0430.<br \/>  \u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f: <a href=\"https:\/\/docs.djangoproject.com\/en\/dev\/ref\/contrib\/admin\/#django.contrib.admin.ModelAdmin.get_search_results\">docs.djangoproject.com\/en\/dev\/ref\/contrib\/admin\/#django.contrib.admin.ModelAdmin.get_search_results<\/a><br \/>  \u0415\u0433\u043e \u0438 \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c.<br \/>  \u0423 \u043c\u0435\u043d\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u0442\u0430\u043a\u043e\u0439 \u043a\u043b\u0430\u0441\u0441:  <\/p>\n<pre><code class=\"python\">class SphinxModelAdmin(admin.ModelAdmin):     def get_search_results(self, request, queryset, search_term):         if search_term:             sphinx_queryset = self.model.search.query(search_term)             doc_ids = [doc.pk for doc in sphinx_queryset]             queryset = queryset.filter(pk__in=doc_ids)             return queryset, True         else:             return super(SphinxModelAdmin, self).get_search_results(                 request, queryset, search_term             ) <\/code><\/pre>\n<p>  \u042d\u0442\u043e\u0442 \u0441\u043f\u043e\u0441\u043e\u0431 \u043d\u0435 \u0442\u0435\u0440\u044f\u0435\u0442 \u0444\u0438\u043b\u044c\u0442\u0440\u044b \u0438 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0430\u0434\u043c\u0438\u043d\u043a\u0438, \u043c\u044b \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0443\u0436\u0430\u0435\u043c \u043e\u0431\u043b\u0430\u0441\u0442\u044c \u043f\u043e\u0438\u0441\u043a\u0430 \u0434\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0432 \u0441\u0444\u0438\u043d\u043a\u0441\u0435.<\/p>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"python\">class MovieAdmin(SphinxModelAdmin):     pass <\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0447\u0442\u043e \u043f\u043e\u0438\u0441\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<br \/>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage2\/bc9\/2b2\/48a\/bc92b248a6e39854bce1c250cc00d5a9.png\"\/><br \/>  <i>\u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0432\u0438\u0434 \u2014 django-grappelli<\/i><\/p>\n<p>  \u041c\u0438\u043d\u0443\u0441\u044b \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0440\u0435\u0448\u0435\u043d\u0438\u044f:<br \/>  Django 1.6 \u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0442\u043e\u043f\u0438\u043a\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0431\u0435\u0442\u0435.<\/p>\n<p>  \u0421\u0441\u044b\u043b\u043e\u0447\u043a\u0438:<br \/>  <a href=\"http:\/\/sphinxsearch.com\/\">sphinxsearch.com\/<\/a><br \/>  <a href=\"https:\/\/github.com\/dcramer\/django-sphinx\">github.com\/dcramer\/django-sphinx<\/a><br \/>  <a href=\"http:\/\/vostryakov.ru\/blog\/28-sphinx-11-django-postgresql\/\">vostryakov.ru\/blog\/28-sphinx-11-django-postgresql\/<\/a><br \/>  <a href=\"https:\/\/docs.djangoproject.com\/en\/dev\/ref\/contrib\/admin\/#django.contrib.admin.ModelAdmin.get_search_results\">docs.djangoproject.com\/en\/dev\/ref\/contrib\/admin\/#django.contrib.admin.ModelAdmin.get_search_results<\/a>    \t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/188242\/\"> http:\/\/habrahabr.ru\/post\/188242\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t\u0417\u0430\u0434\u0430\u0447\u0430: \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u043d\u043e\u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0432 \u0430\u0434\u043c\u0438\u043d\u043a\u0435 django.<br \/>  \u041f\u0440\u0438\u043c\u0435\u0440 \u043c\u043e\u0434\u0435\u043b\u0438, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e\u0438\u0441\u043a:  <\/p>\n<pre><code class=\"python\">class Movie(models.Model):     title_en = models.CharField(max_length=255, null=True)     title_ru = models.CharField(max_length=255, null=True) <\/code><\/pre>\n<p>  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-188242","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/188242","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=188242"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/188242\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=188242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=188242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=188242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}