【问题标题】:Matching only some words in query using Django Haystack使用 Django Haystack 仅匹配查询中的某些单词
【发布时间】:2014-06-26 01:25:50
【问题描述】:

我目前正在使用以下 django-haystack 代码在我的网站上进行搜索:

def products_search_results(request):
    q = request.GET['q']
    from haystack.query import SearchQuerySet
    products = SearchQuerySet().models(Product).filter(text=q)

这使用q 作为搜索中的查询参数。这也使用q 中的所有单词来匹配结果。这可能是(?)预期的默认行为。这就是我所看到的。

我有一个标题为“Red Corvette”的Product

当然,搜索词“Red”、“Red Corvette”和“Corvette”都匹配,但搜索词“Red Corvette Convertible”或“Red Corvette T-top”将不匹配。我真的很希望那些匹配查询,特别是如果默认查询的结果不多。

我可以对我的 haystack 查询做些什么来获得这种行为吗?

【问题讨论】:

  • 当我安装 Haystack 时,您需要的是默认行为。也许它已被设置为最新 Haystack 的默认设置,因为您的问题是从 2014 年开始的。

标签: python django elasticsearch django-haystack


【解决方案1】:

例如在您的搜索索引文件中使用EdgeNgramField

class AppIndex(indexes.SearchIndex, indexes.Indexable):

    ngram_text = indexes.EdgeNgramField()

    def prepare(self, obj):
        """Add the content of text field from final prepared data into ngram_text field
        """
        prepared_data = super(AppIndex, self).prepare(obj)
        prepared_data['ngram_text'] = prepared_data['text']
        return prepared_data

然后对该字段进行查询(确保在进行上述更改后首先执行rebuild_indexupdate_index):

products = SearchQuerySet().models(Product).filter(ngram_text=q)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多