繁体   English   中英

自动完成无法使用具有弹性搜索的 Haystack

[英]Autocomplete is not working Haystack with elastic search

我正在尝试为 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]')

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

视图.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”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM