Haystack SearchQuerySet返回空对象数组

时间:2017-01-29 07:26:47

标签: django elasticsearch django-haystack

我试图在django中为一个模型制作自动完成api。第一次使用带有弹性搜索的haystack。当我点击api SearchQuerySet返回空对象数组时。

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)

    def __str__(self):
        return self.title

search_indexes.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().all()
    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')

movie_text.txt

{{ object.title }}
{{ object.year }}
{{ object.production_company }}
{{ object.director }}

下面的索引是我的索引数据

{
"took": 60,
"timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
},
"hits": {
    "total": 22,
    "max_score": 1,
    "hits": [
    {
        "_index": "haystack",
        "_type": "modelresult",
        "_id": "app.movie.7",
        "_score": 1,
        "_source": {
        "django_ct": "app.movie",
        "django_id": "7",
        "year": "1978",
        "text": "A Night Full of Rain\n1978\nLiberty Film\nLina Wertmuller\n",
        "id": "app.movie.7"
        }
    },
    {
        "_index": "haystack",
        "_type": "modelresult",
        "_id": "app.movie.9",
        "_score": 1,
        "_source": {
        "django_ct": "app.movie",
        "django_id": "9",
        "year": "1997",
        "text": "A Smile Like Yours\n1997\nParamount Pictures\nKeith Samples\n",
        "id": "app.movie.9"
        }
    },
    {
        "_index": "haystack",
        "_type": "modelresult",
        "_id": "app.movie.13",
        "_score": 1,
        "_source": {
        "django_ct": "app.movie",
        "django_id": "13",
        "year": "1985",
        "text": "A View to a Kill\n1985\nMetro-Goldwyn Mayer\nJohn Glen\n",
        "id": "app.movie.13"
        }
    },
    {
        "_index": "haystack",
        "_type": "modelresult",
        "_id": "app.movie.14",
        "_score": 1,
        "_source": {
        "django_ct": "app.movie",
        "django_id": "14",
        "year": "1985",
        "text": "A View to a Kill\n1985\nMetro-Goldwyn Mayer\nJohn Glen\n",
        "id": "app.movie.14"
        }
    },
    {
        "_index": "haystack",
        "_type": "modelresult",
        "_id": "app.movie.16",
        "_score": 1,
        "_source": {
        "django_ct": "app.movie",
        "django_id": "16",
        "year": "1985",
        "text": "A View to a Kill\n1985\nMetro-Goldwyn Mayer\nJohn Glen\n",
        "id": "app.movie.16"
        }
    },
    {
        "_index": "haystack",
        "_type": "modelresult",
        "_id": "app.movie.20",
        "_score": 1,
        "_source": {
        "django_ct": "app.movie",
        "django_id": "20",
        "year": "1936",
        "text": "After the Thin Man\n1936\nMetro-Goldwyn Mayer\nW.S. Van Dyke\n",
        "id": "app.movie.20"
        }
    },
    {
        "_index": "haystack",
        "_type": "modelresult",
        "_id": "app.movie.22",
        "_score": 1,
        "_source": {
        "django_ct": "app.movie",
        "django_id": "22",
        "year": "2015",
        "text": "Age of Adaline\n2015\nLionsgate / Sidney Kimmel Entertainment / Lakeshore Entertainment\nLee Toland Krieger\n",
        "id": "app.movie.22"
        }
    },
    {
        "_index": "haystack",
        "_type": "modelresult",
        "_id": "app.movie.15",
        "_score": 1,
        "_source": {
        "django_ct": "app.movie",
        "django_id": "15",
        "year": "1985",
        "text": "A View to a Kill\n1985\nMetro-Goldwyn Mayer\nJohn Glen\n",
        "id": "app.movie.15"
        }
    },
    {
        "_index": "haystack",
        "_type": "modelresult",
        "_id": "app.movie.17",
        "_score": 1,
        "_source": {
        "django_ct": "app.movie",
        "django_id": "17",
        "year": "1985",
        "text": "A View to a Kill\n1985\nMetro-Goldwyn Mayer\nJohn Glen\n",
        "id": "app.movie.17"
        }
    },
    {
        "_index": "haystack",
        "_type": "modelresult",
        "_id": "app.movie.1",
        "_score": 1,
        "_source": {
        "django_ct": "app.movie",
        "django_id": "1",
        "year": "2011",
        "text": "180\n2011\nSPI Cinemas\nJayendra\n",
        "id": "app.movie.1"
        }
    }
    ]
}
}

API的输出

{
"results": [
    null,
    null,
    null,
    null,
null]
}  

1 个答案:

答案 0 :(得分:1)

在确保索引更新时,请不要看到索引存在问题。在您的视图中,您既不会传递搜索查询,也不会使用自动填充字段进行查询。 请试试这个。

def autocomplete(request):
    search_query = request.GET.get("q",None)
    if not search_query:
        raise Http404
    autosuggest = SearchQuerySet().autocomplete(content_auto=search_query)[:5]
    try:
        autosuggest_results = map(lambda x: x.object.title, autosuggest)
        response = {"results": autosuggest_results}
        return JsonResponse(response)
    except Exception:
        raise Http404()