我正在尝试使用以下命令将 elasticsearch 嵌入到我的 Django 应用程序中django-haystack
。我正在尝试实现用户搜索。我的用户模型是这样的:
class MyUser(AbstractBaseUser):
username = models.CharField(max_length=255, unique=True)
name = models.CharField(max_length=63, blank=True)
email = models.EmailField(blank=True, unique=True)
status = models.CharField(max_length=255, blank=True, null=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
joined = models.DateTimeField(auto_now_add=True, null=True)
现在,我想搜索name
and username
字段。我创建了以下内容search_indexes.py
:
class UserIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.EdgeNgramField(document=True, model_attr='name')
username = indexes.CharField(model_attr='username')
def get_model(self):
return MyUser
def get_updated_field(self):
return "joined"
但是,当我执行搜索时,我只得到与name
场地。我在这里做错了什么?还有其他方法可以做到这一点吗?
提前致谢。
django-haystack 的工作方式,里面的数据document=True
字段用于一般搜索,任何其他字段用于单独过滤。因此,在您的情况下,搜索将仅使用name
场地。要解决此问题,您需要使用一个模板来指定搜索中要使用的所有字段。首先,使用:
text = indexes.EdgeNgramField(document=True, use_template=True)
然后,您需要在模板目录中创建一个名为的新模板search/indexes/myapp/myuser_text.txt
并将以下内容放入其中:
{{ object.username }}
{{ object.name }}
See http://django-haystack.readthedocs.org/en/latest/tutorial.html#handling-data http://django-haystack.readthedocs.org/en/latest/tutorial.html#handling-data供完整参考
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)