django-haystack找不到索引帖子

时间:2016-12-23 06:55:23

标签: elasticsearch django-haystack

您好我在macOS中使用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

BookList模型

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以及客户搜索书名(或某个单词)

帖子将显示我的网页

这是我的search_indexes,itempost_text.txt和模板

search_indexes

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)&lt; - 未出现

2个对象

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

但是当我使用关键字'Java'或'python'

搜索帖子时

任何帖子都没有出现

enter image description here

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

请有人帮助我

1 个答案:

答案 0 :(得分:0)

f **国王可怕的错误

<强> 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 }}