Django Haystack - 索引单个字段

kie*_*ran 2 python django search django-haystack

我正在使用 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()
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

我想把它想象成一个 SQL 查询:

SELECT * FROM artists WHERE title LIKE '%{search_term}%'
Run Code Online (Sandbox Code Playgroud)

更新

按照删除 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
Run Code Online (Sandbox Code Playgroud)

但我也有同样的问题。(已尝试过 python manage.py rebuild_index)

这是我的 Haystack 设置(如果有什么不同的话):

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.simple_backend.SimpleEngine',
    },
}
Run Code Online (Sandbox Code Playgroud)

knb*_*nbk 5

model_attr并且 use_template不要一起工作。在这种情况下,当您查询单个模型属性时,无需使用模板。搜索索引中的模板纯粹是为了对数据进行分组。

因此,你最终会得到:

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

    def get_model(self):
        return Artist
Run Code Online (Sandbox Code Playgroud)