我试图让部分单词匹配工作,我已经看过related question但由于某种原因,我的设置无法正常工作。我正在使用带有弹性搜索的haystack。有人可以告诉我我做错了吗?
class VendorIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.EdgeNgramField(document=True, use_template=True)
company_name = indexes.CharField(model_attr='company_name', null=True)
def get_model(self):
return Vendor
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
编辑:我通过使用自动完成功能完成了这项工作。但是,我觉得这是一个黑客。但这是我的代码:
class VendorIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True)
company_name = indexes.CharField(model_attr='company_name', null=True)
company_name_auto = indexes.NgramField(model_attr='company_name')
def get_model(self):
return Vendor
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
然后在视图中:
class SearchVendorView(generics.ListAPIView):
serializer_class = serializers.CustomerVendorSerializer
def get_queryset(self):
results = SearchQuerySet().models(Vendor).autocomplete(company_name_auto=self.request.GET.get('q', ''))
vendors = []
for vendor in [result.object for result in results]:
vendor.is_liked = Vendor.objects.from_request(self.request).get(pk=vendor.id).user_vote
vendors.append(vendor)
return vendors
答案 0 :(得分:1)
就我而言:django-haystack==2.5.1
+ elasticsearch==5.0.1
+ java elastic search daemon v2.3.1
这足以获得部分搜索:
from haystack import indexes
from authors.models import Author
class AuthorIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.EdgeNgramField(document=True, use_template=True)
last_name = indexes.CharField(model_attr='last_name')
first_name = indexes.CharField(model_attr='first_name')
def get_model(self):
return Author
def index_queryset(self, using=None):
"""Used when the entire index for model is updated."""
return self.get_model().objects.all()
然后python manage.py rebuild_index
您可以搜索:
from haystack.query import SearchQuerySet
SearchQuerySet().filter(content="alex")
找到Alexander
。