gpt4 book ai didi

python - 自动完成功能不适用于 Haystack 和 Elasticsearch

转载 作者:太空宇宙 更新时间:2023-11-03 15:44:09 25 4
gpt4 key购买 nike

我正在尝试为 django 中的一个模型制作自动完成 api。第一次使用 haystack 和 Elasticsearch 。尝试获取自动完成结果时出现错误。

Failed to query Elasticsearch using 'content_auto:(Jitney)': TransportError(400, u'parsing_exception', u'[query_string] query does not support [fuzzy_min_sim]')

模型.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

搜索.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()

View .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

电影文本.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.
~

我缺少什么,无法在这里找到问题。

最佳答案

搜索文件名有误。我将文件命名为“search.py​​”而不是“search_indexes.py”

关于python - 自动完成功能不适用于 Haystack 和 Elasticsearch ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41915822/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com