簡體   English   中英

如何提高django haystack搜索的速度

[英]how to improve speed of django haystack search

我想在django環境中為簡單的數據結構創建一個搜索引擎:

| id         | comapany name    |
|:-----------|-----------------:|
| 12345678   | company A's name |
| 12345687   | peoples pizza a/s|
| 87654321   | sub's for pugs   |

大約有80萬家公司,我只想按名稱搜索。 找到名稱后,ID將以我的django返回。

我已經嘗試過各種與干草堆,飛快移動等類似的設置,但是當我從約500個測試數據集提高到80萬個時,搜索結果一直很慢。 搜索有時需要近一個小時

我正在使用Paas Heroku,所以我認為我會嘗試集成的付費服務(早期的elasticsearch實施)。 這有所幫助,但是當我到達大約80,000家公司時,它又開始變得非常緩慢。

已安裝的應用

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',

    # Added.
    'haystack',

    # Then your usual apps...
]

更多settings.py

import os
from urlparse import urlparse

es = urlparse(os.environ.get('SEARCHBOX_URL') or 'http://127.0.0.1:9200/')

port = es.port or 80

HAYSTACK_CONNECTIONS = {
   'default': {
       'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
       'URL': es.scheme + '://' + es.hostname + ':' + str(port),
       'INDEX_NAME': 'documents',
   },


if es.username:
   HAYSTACK_CONNECTIONS['default']['KWARGS'] = {"http_auth": es.username + ':' + es.password}

search_indexes.py

from haystack import indexes

from hello.models import Article


class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
    '''
    defines the model for the serach Engine.
    '''
    text = indexes.CharField(document=True, use_template=True)
    pub_date = indexes.DateTimeField(model_attr='pub_date')
    # pub_date line was commented out previously
    content_auto = indexes.EdgeNgramField(model_attr='title')

    def get_model(self):
        return Article

    def index_queryset(self, using=None):
        """Used when the entire index for model is updated."""
        return self.get_model().objects.all()

article_text.txt

{{ object.title }}
{{ object.user.get_full_name }}
{{ object.body }}

urls.py

url(r'^search/$', views.search_titles, name='search'),

views.py

def search_titles(request):
    txt = request.POST.get('search_text', '')
    if txt and len(txt) >= 4:
        articles = SearchQuerySet().autocomplete(content_auto=txt)
    # if the post request is empty, return nothing
    # this prevents internal server error with jquery
    else:
        articles = []
    return render_to_response('scripts/ajax_search.html',
                              {'articles': articles})

search.html

{% if articles.count > 0 %}
    <!-- simply prints the links to the cvr numbers-->
    <!-- for article in articles -->
    {% for article in "x"|rjust:"15" %} 
        <li><a href="{{ article.object.get_absolute_url }}">{{ article.object.title }}</a></li>
    {% endfor %}

{% else %}

    <li>Try again, or try CVR + &#x23ce;</li>

{% endif %}

index.html(我稱之為搜索引擎)

{% csrf_token %}
<input  type="text" id="search" name="search" />

<!-- This <ul> all company names end up-->
<ul id ="search-results"></ul>

我將ves.py的搜索方法更改為:

txt = request.POST.get('search_text', '')
articles = []
suggestedSearchTerm = ""
if txt and len(txt) >= 4:
    sqs = SearchQuerySet()
    sqs.query.set_limits(low=0, high=8)
    sqs = sqs.filter(content=txt)
    articles = sqs.query.get_results()
    suggestedSearchTerm = SearchQuerySet().spelling_suggestion(txt)
    if suggestedSearchTerm == txt:
        suggestedSearchTerm = ''
    else:
      suggestedSearchTerm = suggestedSearchTerm.lower()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM