Django Haystack/Solr : Filter results based on a field containing a LIST of states

标签 django search solr django-haystack

我正在开发一个人名搜索功能,可以根据他们从事业务的州进行过滤。有些人在多个地区开展业务,所以我的问题是,如何存储某个字段中的州列表,以便我可以对该字段中的任何州进行精确搜索?

示例数据:

| 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/

相关文章:

django - 自上次使用 Django 和 Python 登录以来,如何获取新的数据库更改?

Django 1.8 和 MongoDB?

python - 具有异步任务和redis的django中的线程安全

django - 使用ElasticSearch在Haystack中进行多语言词源分析

带有双引号的实体查询中的 Solr 错误

Mysql全文搜索排除结果

c# - 如何在 C# 3.0 中的字符串数组中搜索特定字符串

java - 如何将 À, Á, Â, Ã, Ä, Å 映射到 A 以实现更高效的搜索?

Solr 搜索器变暖

xml - 删除 XML 中的所有 HTML