代码之家  ›  专栏  ›  技术社区  ›  Jade Han

django haystack找不到索引文章

  •  0
  • Jade Han  · 技术社区  · 9 年前

    嗨,我在用 elasticsearch@1.7 + django-haystack 在马科斯

    我正在使用postgrepql制作一个练习网站

    model

    用户模型

    from django.contrib.auth.models import AbstractUser
    from django.db import models
    
    class User(AbstractUser):
        nickname = models.CharField(
            max_length=20,
            null=True,
            blank=True,
            )
        phone = models.CharField(
            max_length=20,
            null=True,
            blank=True,
            )
        certification_code = models.CharField(
            max_length=4,
            null=True,
            blank=True,
            )
        passed_certification = models.BooleanField(
            default=False,
            )
    
        class Meta:
            verbose_name = '유저'
            verbose_name_plural = verbose_name
    

    ItemPost模型

    from django.db import models
    from django.conf import settings
    from django.utils import timezone
    
    def localtime():
        return timezone.localtime(timezone.now())
    
    class ItemPost(models.Model):
        user = models.ForeignKey(
            settings.AUTH_USER_MODEL,
            )
    
        # it will used item admin
        def user_nickname_def(self):
            return self.user.nickname
    
        user_nickname = property(user_nickname_def)
    
        title = models.TextField()
        created_at = models.DateTimeField(
            default=localtime
            )
        updated_at = models.DateTimeField(
            default=localtime,
            )
        is_deleted = models.BooleanField(
            default=False,
            )
    
        def __str__(self):
            return self.title
    
        class Meta:
            verbose_name = '포스트'
            verbose_name_plural = verbose_name
    

    书单模型

    from django.db import models
    from .item_post import ItemPost
    
    
    class BookList(models.Model):
        # post + booknumber => will used primary key
    
        post = models.ForeignKey(
            "ItemPost",
            )
    
        booknumber = models.IntegerField(
            )
    
        bookname = models.CharField(
            max_length=200,
            )
    
        bookprice = models.CharField(
            max_length=200,
            )
    
        is_soldout = models.BooleanField(
            default=False,
            )
    
        def __str__(self):
            return self.bookname
    

    我想要索引 ItemPost 当客户搜索图书标题(或某些单词)时

    帖子将显示我的网页

    这是我的搜索索引,itempost_text。txt和模板

    搜索索引

    from haystack import indexes
    from items.models import ItemPost
    import datetime
    
    class ItemPostIndex(indexes.SearchIndex, indexes.Indexable):
    
        text = indexes.CharField(document=True, use_template=True)
        title = indexes.CharField(model_attr='title')
        updated_at = indexes.DateTimeField(model_attr='updated_at')
    
        def get_model(self):
            return ItemPost
    
        def index_queryset(self, using=None):
            """Used when the entire index for model is updated."""
            return self.get_model().objects.filter(updated_at__lte=datetime.datetime.now())
    

    itempost_text.txt

    {{ objects.title }}
    {{ objects.user.get_full_name }}
    {{ objects.body }}
    

    search.html

    {% extends "base.html" %}
    
    {% block title %}남서울 도서공유{% endblock %}
    
    {% block body %}
    <div class="container">
        <section class="col-md-2"></section>
        <section class="col-md-8">
    
      <h2>Search</h2>
    
        <form method="get" action=".">
            <table>
                {{ form.as_table }}
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <input type="submit" value="Search">
                    </td>
                </tr>
            </table>
    
        {% if query %}
            <h3>Results</h3>
    
            {% for result in page.object_list %}
                <p>
                    <a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
                </p>
            {% empty %}
                <p>No results found.</p>
            {% endfor %}
    
            {% if page.has_previous or page.has_next %}
                <div>
                    {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
                    |
                    {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
                </div>
            {% endif %}
        {% else %}
            {# Show some example queries to run, maybe query syntax, something else? #}
        {% endif %}
    </form>
    
    </section>
    <section class="col-md-2"></section>
    </div>
    
    {% endblock %}
    

    我认为一切都很好

    当我发号施令时

    python ./manage.py rebuild_index
    
    WARNING: This will irreparably remove EVERYTHING from your search index in connection 'default'.
    Your choices after this are to restore from backups or rebuild via the `rebuild_index` command.
    Are you sure you wish to continue? [y/N] y
    Removing all documents from your index because you said so.
    All documents removed.
    /Users/hanminsoo/.pyenv/versions/study_alone/lib/python3.5/site-packages/django/db/models/fields/__init__.py:1453: RuntimeWarning: DateTimeField ItemPost.updated_at received a naive datetime (2016-12-23 15:47:04.759121) while time zone support is active.
      RuntimeWarning)
    
    Indexing 2 포스트
    

    (GET/haystack/_mapping)<-没有出现

    2对象

    In [1]: ItemPost.objects.all()
    Out[1]: [<ItemPost: Java book>, <ItemPost: python>]
    

    但当我使用关键字“Java”或“python”搜索帖子时

    没有出现任何帖子

    enter image description here

    我认为这是django haystack的问题,而不是弹性搜索 但我不知道为什么

    求你了,谁来帮帮我

    1 回复  |  直到 9 年前
        1
  •  0
  •   Jade Han    9 年前

    这是一个可怕的错误

    itempost_text.txt

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

    itempost_text.txt

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