Django-Haystack文本字段返回空行“\ n \ n”

时间:2017-08-30 21:41:15

标签: django solr django-haystack

我尝试使用django-haystack(2.6.0)和solr(4.10.4)来实现两个字段搜索。我想通过ship名称搜索我的模型或发送country(返回给定国家/地区的船只列表)但是如果我使用document=True定义一个字段,我会遇到问题没有结果。 我拒绝了documentationanswer,看起来相当容易。

我创建了以下search_indexes.py

class ShipListIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)

def get_model(self):
    return ShipList

def index_queryset(self, using=None):
    return self.get_model().objects.all()

我在我的应用程序中创建了一个新模板,指定了搜索中使用的所有字段:

search/indexes/myapp/shiplist_text.txt

{{ objects.ship }}
{{ objects.country }}

所以这是我的模特:

class ShipList(models.Model):
    ship = models.CharField(max_length=200)
    country = models.CharField(max_length=200)
    region = models.CharField(max_length=250, blank=True)
    city = models.CharField(max_length=250, blank=True)
    ship_class = models.CharField(max_length=200, blank=True)
    ship_type = models.CharField(max_length=200, blank=True)
    remarks = models.CharField(max_length=200, blank=True)
    url = models.URLField(max_length=200)
    slug = models.SlugField()

和视图:

def ship_search(request):
   results = None
   cd = None
   form = SearchForm()
   if 'query' in request.GET:
       form = SearchForm(request.GET)
       if form.is_valid():
           cd = form.cleaned_data
           results = SearchQuerySet().models(ShipList).filter(content=cd['query']).load_all()

   return render(request,
                'core/draft/search.html',
                {'form': form,
                'results': results,
                'cd': cd})

简单模板

{% block search %}
{% if "query" in request.GET %}
{% for result in results %}
    {% with ship=result.object %}
        <p>{{ ship.ship }}</p>
        <p><a href="{{ ship.get_absolute_url }}">{{ ship.ship }}</a></p>
    {% endwith %}
{% empty %}
    <p>There are no results for your query.</p>
{% endfor %}

{% else %}
<h1>Search for posts</h1>
<form action="." method="get">
    {{ form.as_p }}
    <input type="submit" value="Search">
</form>
{% endif %}
{% endblock %}

然后我检查了solr接口http://127.0.0.1:8983/solr/#/myapp/query以获取JSON响应:

{
  "responseHeader": {
    "status": 0,
    "QTime": 2
  },
  "response": {
    "numFound": 11,
    "start": 0,
    "docs": [
      {
        "django_ct": "core.shiplist",
        "id": "core.shiplist.1",
        "django_id": "1",
        "text": "\n\n"
      },
      {
        "django_ct": "core.shiplist",
        "id": "core.shiplist.2",
        "django_id": "2",
        "text": "\n\n"
      },
      {
        "django_ct": "core.shiplist",
        "id": "core.shiplist.3",
        "django_id": "3",
        "text": "\n\n"
    ]
  }
}

所以这是搜索索引中为每艘船存储的数据 我发现文本字段包含"\n\n

我猜这里的问题可能应该是这样的:

"text": "USS Arizona\nUnited States"

到目前为止,我尝试创建没有use_template的search_indexs.py:

class ShipListIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, model_attr='ship')

    def get_model(self):
        return ShipList

它工作正常,我可以按船名搜索,查询输出是正确的。

     {
        "id": "core.shiplist.5",
        "django_ct": "core.shiplist",
        "django_id": "5",
        "text": "USS Arizona"
      },

最后我的solrconfig.xml

<?xml version="1.0" encoding="utf-8" ?>
<config>
 <luceneMatchVersion>LUCENE_36</luceneMatchVersion>
 <requestHandler name="/select" class="solr.StandardRequestHandler"
default="true" />
 <requestHandler name="/update" class="solr.UpdateRequestHandler" />
 <requestHandler name="/admin" class="solr.admin.AdminHandlers" />
 <requestHandler name="/admin/ping" class="solr.PingRequestHandler">
 <lst name="invariants">
 <str name="qt">search</str>
 <str name="q">*:*</str>
 </lst>
 </requestHandler>
</config>

显然我错过了一些东西,或者我不明白它是如何工作的?

1 个答案:

答案 0 :(得分:0)

在模板中,应该如何形成搜索字段:

search/indexes/myapp/shiplist_text.txt

{{ objects.ship }}
{{ objects.country }}

..你引用的是objects(最后是s)。正确的名称为object,没有额外的s

{{ object.ship }}
{{ object.country }}

您看到的额外内容是文件末尾的换行符。保存它没有它,你将避免在你的价值的最后有一个额外的\ n。但是,它对于可搜索性并不重要。