2012-04-24 90 views
2

我正在研究人名的搜索功能,该功能可以由他们开展业务的州进行筛选。有些人在多个地区开展业务,所以我的问题是,如何在域中存储一个状态列表,以便我可以对该域中的任何状态进行精确搜索?Django Haystack/Solr:根据包含状态列表的字段筛选结果

示例数据:

 
| 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的魔法一样词干/模糊匹配/等...

这似乎是一个基本要求,我没有看到什么?

谢谢!

回答

2
state = indexes.MultiValueField() 

    def prepare_state(self, obj): 
     return [g.pk for g in obj.state_set.all()] 
+0

完美,谢谢你的帮助! – 2012-04-24 21:54:22

+0

不用客气 – bmihelac 2012-04-25 05:43:55