我正在研究人名的搜索功能,这可以通过他们开展业务的州进行过滤。有些人在多个地区开展业务,所以我的问题是,如何在一个字段中存储状态列表,以便能够对该字段中的任何状态进行精确搜索?
示例数据:
| Person Name | States | |=============|============| | John Doe | CA, NV, AZ | | John Smith | NY, NJ | | ... | |=============|============|
现在是我的代码: #search_indexes.py
class ProfileIndex(RealTimeSearchIndex):
text = indexes.CharField(document=True) # the person's name
state = indexes.CharField(faceted=True)
def prepare_text(self, profile):
return profile.display_name
def prepare_state(self, profile):
return ???????
# search code
search_string = "John"
search_state = "CA"
search_results = SearchQuerySet().models(Profile) \
.filter(text=search_string, state=search_state) \
.load_all()
# Should only return John Doe because of the search_state constraint...
我希望solr能够完全解析状态名称,而不会像阻塞/模糊匹配等那样使用任何solr魔法......
这似乎是一个基本要求,我没有看到什么?
谢谢!
答案 0 :(得分:2)
state = indexes.MultiValueField()
def prepare_state(self, obj):
return [g.pk for g in obj.state_set.all()]