我正在开发一个人名搜索功能,可以根据他们从事业务的州进行过滤。有些人在多个地区开展业务,所以我的问题是,如何存储某个字段中的州列表,以便我可以对该字段中的任何州进行精确搜索?
示例数据:
| Person Name | States | |=============|============| | John Doe | CA, NV, AZ | | John Smith | NY, NJ | | ... | |=============|============|
Here's my code now: # 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 魔法,例如词干/模糊匹配/等...
这似乎是一个基本请求,我没有看到什么?
谢谢!
最佳答案
state = indexes.MultiValueField()
def prepare_state(self, obj):
return [g.pk for g in obj.state_set.all()]
关于Django Haystack/Solr : Filter results based on a field containing a LIST of states,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10305042/