django_haystack_生成索引报错_haystack.exceptions.SearchBackendError: No fields were found in any search_in

问题描述:

虚拟环境版本:

aliyun-python-sdk-core==2.13.15
aliyun-python-sdk-core-v3==2.13.11
aliyun-python-sdk-kms==2.10.1
amqp==2.5.2
billiard==3.6.3.0
celery==4.4.1
certifi==2019.11.28
chardet==3.0.4
crcmod==1.7
Django==2.2.8
django-celery-results==1.0.4
django-haystack==2.8.1
django-js-asset==1.2.2
django-redis==4.11.0
django-tinymce==2.8.0
dnspython==1.16.0
eventlet==0.25.1
greenlet==0.4.15
idna==2.9
importlib-metadata==1.5.0
itsdangerous==1.1.0
Jinja2==2.11.1
jmespath==0.9.5
jsonfield==3.1.0
kombu==4.6.8
MarkupSafe==1.1.1
monotonic==1.5
oss2==2.9.1
Pillow==7.0.0
pycryptodome==3.9.7
PyMySQL==0.9.3
pytz==2019.3
redis==3.2.0
requests==2.23.0
six==1.14.0
sqlparse==0.3.1
urllib3==1.25.8
vine==1.3.0
Whoosh==2.7.4
zipp==3.1.0

使用haystack对某个表生成索引的时候报错,如下:

  File "manage.py", line 21, in 
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "F:\django_learn_project5\dailyfresh\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "F:\django_learn_project5\dailyfresh\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "F:\django_learn_project5\dailyfresh\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "F:\django_learn_project5\dailyfresh\venv\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "F:\django_learn_project5\dailyfresh\venv\lib\site-packages\haystack\management\commands\rebuild_index.py", line 41, in handle
    call_command('clear_index', **clear_options)
  File "F:\django_learn_project5\dailyfresh\venv\lib\site-packages\django\core\management\__init__.py", line 148, in call_command
    return command.execute(*args, **defaults)
  File "F:\django_learn_project5\dailyfresh\venv\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "F:\django_learn_project5\dailyfresh\venv\lib\site-packages\haystack\management\commands\clear_index.py", line 53, in handle
    backend.clear(commit=self.commit)
  File "F:\django_learn_project5\dailyfresh\venv\lib\site-packages\haystack\backends\whoosh_backend.py", line 232, in clear
    self.setup()
  File "F:\django_learn_project5\dailyfresh\venv\lib\site-packages\haystack\backends\whoosh_backend.py", line 119, in setup
    self.content_field_name, self.schema = self.build_schema(connections[self.connection_alias].get_unified_index().all_searchfields())
  File "F:\django_learn_project5\dailyfresh\venv\lib\site-packages\haystack\backends\whoosh_backend.py", line 172, in build_schema
    raise SearchBackendError("No fields were found in any search_indexes. Please correct this before attempting to search.")
haystack.exceptions.SearchBackendError: No fields were found in any search_indexes. Please correct this before attempting to search.

原因分析:

 这个原因是你的环境配置没有配置好。

1.在app下创建search_indexes.py文件(文件名称必须一致),文件中导入类是否正确

2.在templates下创建文件夹,在indexes下创建文件夹(文件夹名称为search_indexes.py所属app名)

 如果我想给app为student中的模型Family建立索引,步骤2中应该创建student文件夹

3. 在步骤2创建的文件下txt文件,文件名为步骤1中search_indexes.py文件导入的类的小写和'_text'的拼接,文件后缀为'.txt'

创建family_text.txt文件

再次执行如下代码建立索引

python manage.py rebuild_index
(venv) F:\django_learn_project5\dailyfresh>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 29 学生

 

你可能感兴趣的:(django,haystack)