我试图在django中为一个模型制作自动完成api。第一次使用带有弹性搜索的haystack。尝试获取自动填充结果时出错。
Failed to query Elasticsearch using 'content_auto:(Jitney)': TransportError(400, u'parsing_exception', u'[query_string] query does not support [fuzzy_min_sim]')
models.py
from __future__ import unicode_literals
from django.db import models
class Movie(models.Model):
title = models.CharField(max_length=250)
year = models.CharField(max_length=250)
location = models.CharField(max_length=250)
fun_fact = models.CharField(max_length=250, null=True)
production_company = models.CharField(max_length=250)
director = models.CharField(max_length=250)
actor1 = models.CharField(max_length=250)
actor2 = models.CharField(max_length=250, null=True)
actor3 = models.CharField(max_length=250, null=True)
longitude = models.DecimalField(max_digits=9, decimal_places=6)
latitude = models.DecimalField(max_digits=9, decimal_places=6)
class Meta:
db_table = "sf_movies"
def __str__(self):
return self.title
search.py
from haystack import indexes
from models import Movie
class MovieIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
year = indexes.CharField(model_attr='year')
content_auto = indexes.EdgeNgramField(model_attr='title')
def get_model(self):
return Movie
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
views.py
from django.shortcuts import render
# Create your views here.
import json
from django.http import HttpResponse
from haystack.query import SearchQuerySet
def autocomplete(request):
sqs = SearchQuerySet().autocomplete(content_auto=request.GET.get('q', ''))[:5]
suggestions = [result.title for result in sqs]
# Make sure you return a JSON object, not a bare list.
# Otherwise, you could be vulnerable to an XSS attack.
the_data = json.dumps({
'results': suggestions
})
return HttpResponse(the_data, content_type='application/json')
目录结构
.
── app
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── __init__.py
│ ├── models.py
│ ├── search.py
│ ├── template
│ │ └── search
│ │ └── indexes
│ │ └── app
│ │ └── movie_text.txt
│ ├── tests.py
│ ├── views.py
├── db.sqlite3
├── manage.py
└── uber_assignment_SF_movies
├── __init__.py
├── settings.py
├── settings.pyc
├── urls.py
├── wsgi.py
movie_text.txt
{{ object.title }}
{{ object.year }}
{{ object.production_company }}
{{ object.director }}
当我运行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.
~
我错过了什么,无法在这里找到问题。
答案 0 :(得分:1)
搜索文件名出错。我将文件命名为“search.py”而不是“search_indexes.py”