Search code examples
elasticsearchdjango-haystack

django-haystack can not find indexed post


Hi I'm using elasticsearch@1.7 + django-haystack in macOS

I'm making a practice web site using postgrepql

model

User 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 Model

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 Model

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

I want indexing ItemPost and when client search book title(or some word)

posts will show my web page

here is my search_indexes, itempost_text.txt and template

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

I think everything is Ok

when I command

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)<- does not appear

2 objects

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

but when I search post using keyword 'Java' or 'python'

any post doesn't appear

enter image description here

I think it is problem of django-haystack not elasticsearch but I don't know why

please somebody help me


Solution

  • f**king terrible mistake

    itempost_text.txt

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

    NOT

    itempost_text.txt

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