【发布时间】:2013-08-14 09:56:57
【问题描述】:
我正在使用 Django Haystack 进行搜索。
我只想在搜索结果时定位我的模型的 title 字段。
但是,目前,如果搜索词在我的模型中的任何字段中,它都会返回结果。
例如:搜索 xyz 会得到 xyz 在 bio 字段中的结果。
这不应该发生,我只想返回 xyz 在 title 字段中的结果。完全忽略除Artist.title 之外的所有其他字段进行搜索。
artists/models.py:
class Artist(models.Model):
title = models.CharField(max_length=255)
slug = models.SlugField(max_length=100)
strapline = models.CharField(max_length=255)
image = models.ImageField(upload_to=get_file_path, storage=s3, max_length=500)
bio = models.TextField()
artists/search_indexes.py
from haystack import indexes
from app.artists.models import Artist
class ArtistIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True, model_attr='title')
def get_model(self):
return Artist
我猜想它就像一个 SQL 查询:
SELECT * FROM artists WHERE title LIKE '%{search_term}%'
更新
根据删除 use_template=True 的建议,我的 search_indexes.py 现在看起来像:
from haystack import indexes
from app.artists.models import Artist
class ArtistIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, model_attr='title')
title = indexes.CharField(model_attr='title')
def get_model(self):
return Artist
但是我遇到了同样的问题。 (试过python manage.py rebuild_index)
这是我的 Haystack 设置,如果这有什么不同的话:
HAYSTACK_CONNECTIONS = {
'default': {
'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
},
}
【问题讨论】:
标签: python django search django-haystack