建立索引报错

来源:11-2 Elasticsearch介绍和安装

鲨鱼辣椒辣

2021-04-13

建议索引报错
图片描述
图片描述

写回答

2回答

Jack

2021-04-13

图二的报错,再往下拉一点 还有信息没。我代码是这样

#!/usr/bin/python3
# -*- coding:utf-8 -*-
# __author__ = '__Jack__'


import datetime

from haystack import indexes

from zanhu.news.models import News
from zanhu.articles.models import Article
from zanhu.qa.models import Question
from django.contrib.auth import get_user_model
from taggit.models import Tag


class ArticleIndex(indexes.SearchIndex, indexes.Indexable):
   """对Article模型类中部分字段建立索引"""
   text = indexes.CharField(document=True, use_template=True,
                            template_name='search/articles_text.txt')

   def get_model(self):
       return Article

   def index_queryset(self, using=None):
       """当Article模型类中的索引有更新时调用"""
       return self.get_model().objects.filter(status="P", updated_at__lte=datetime.datetime.now())


class NewsIndex(indexes.SearchIndex, indexes.Indexable):
   """对News模型类中部分字段建立索引"""
   text = indexes.CharField(document=True, use_template=True,
                            template_name='search/news_text.txt')

   def get_model(self):
       return News

   def index_queryset(self, using=None):
       return self.get_model().objects.filter(reply=False, updated_at__lte=datetime.datetime.now())


class QuestionIndex(indexes.SearchIndex, indexes.Indexable):
   """对Question模型类中部分字段建立索引"""
   text = indexes.CharField(document=True, use_template=True, template_name='search/questions_text.txt')

   def get_model(self):
       return Question

   def index_queryset(self, using=None):
       return self.get_model().objects.filter(updated_at__lte=datetime.datetime.now())


class UserIndex(indexes.SearchIndex, indexes.Indexable):
   """对User模型类中部分字段建立索引"""
   text = indexes.CharField(document=True, use_template=True, template_name='search/users_text.txt')

   def get_model(self):
       return get_user_model()

   def index_queryset(self, using=None):
       return self.get_model().objects.filter(updated_at__lte=datetime.datetime.now())


class TagsIndex(indexes.SearchIndex, indexes.Indexable):
   """对Tags模型类中部分字段建立索引"""
   text = indexes.CharField(document=True, use_template=True, template_name='search/tags_text.txt')

   def get_model(self):
       return Tag

   def index_queryset(self, using=None):
       return self.get_model().objects.all()

0
4
Jack
回复
鲨鱼辣椒辣
天啦。。好吧 回复迟了 不好意思
2021-04-17
共4条回复

鲨鱼辣椒辣

提问者

2021-04-13

(zanhuenv) [root@localhost zanhu]# 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.
Indexing 2 标签
/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1421: RuntimeWarning: DateTimeField User.updated_at received a naive datetime (2021-04-13 17:50:17.945395) while time zone support is active.
  RuntimeWarning)
Indexing 3 用户
[ERROR/MainProcess] Error updating news using default 
Traceback (most recent call last):
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/haystack/management/commands/update_index.py", line 230, in handle
    self.update_backend(label, using)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/haystack/management/commands/update_index.py", line 254, in update_backend
    end_date=self.end_date)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/haystack/indexes.py", line 177, in build_queryset
    index_qs = self.index_queryset(using=using)
  File "/root/zanhu/zanhu/search/search_indexes.py", line 36, in index_queryset
    return self.get_model().objects.filter(updated_at__lte=datetime.datetime.now())
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/query.py", line 844, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/query.py", line 862, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1263, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1287, in _add_q
    split_subq=split_subq,
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1164, in build_filter
    lookups, parts, reffed_expression = self.solve_lookup_type(arg)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1028, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1389, in names_to_path
    "Choices are: %s" % (name, ", ".join(available)))
django.core.exceptions.FieldError: Cannot resolve keyword 'updated_at' into field. Choices are: content, created_at, liked, parent, parent_id, reply, thread, update_at, user, user_id, uuid_id
Traceback (most recent call last):
  File "manage.py", line 30, in <module>
    execute_from_command_line(sys.argv)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/haystack/management/commands/rebuild_index.py", line 42, in handle
    call_command('update_index', **update_options)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 148, in call_command
    return command.execute(*args, **defaults)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/haystack/management/commands/update_index.py", line 230, in handle
    self.update_backend(label, using)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/haystack/management/commands/update_index.py", line 254, in update_backend
    end_date=self.end_date)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/haystack/indexes.py", line 177, in build_queryset
    index_qs = self.index_queryset(using=using)
  File "/root/zanhu/zanhu/search/search_indexes.py", line 36, in index_queryset
    return self.get_model().objects.filter(updated_at__lte=datetime.datetime.now())
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/query.py", line 844, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/query.py", line 862, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1263, in add_q
    clause, _ = self._add_q(q_object, self.used_aliases)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1287, in _add_q
    split_subq=split_subq,
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1164, in build_filter
    lookups, parts, reffed_expression = self.solve_lookup_type(arg)
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1028, in solve_lookup_type
    _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())
  File "/root/.virtualenvs/zanhuenv/lib/python3.6/site-packages/django/db/models/sql/query.py", line 1389, in names_to_path
    "Choices are: %s" % (name, ", ".join(available)))
django.core.exceptions.FieldError: Cannot resolve keyword 'updated_at' into field. Choices are: content, created_at, liked, parent, parent_id, reply, thread, update_at, user, user_id, uuid_id


0
0

Django高级实战 开发企业级问答网站

融合Django高级用法/算法/设计模式/TestCase测试/云计算打造项目

900 学习 · 756 问题

查看课程