Django Haystack - 使用SearchQuerySet()过滤字段的子字符串 [英] Django Haystack - Filter by substring of a field using SearchQuerySet ()

查看:1068
本文介绍了Django Haystack - 使用SearchQuerySet()过滤字段的子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用SOLR进行索引的Django项目。



我正在尝试一个 子串搜索 使用Haystack的 SearchQuerySet 类。



例如,当用户搜索strongear em> ,它应该返回具有值为 搜索 的字段的条目。
如您所见, ear 搜索 的SUBSTRING。 (显然:))



换句话说,在一个完美的Django世界中,我想要的是:

  SearchQuerySet()。all()。filter(some_field__contains_substring ='ear')

SearchQuerySet 的干草堆文档中( https://django-haystack.readthedocs.org/en/latest/searchqueryset_api.html#field-lookups ),
它表示只支持以下FIELD LOOKUP类型:


  • 包含

  • 确切

  • gt,gte,lt,


  • startwith

  • 范围



我尝试使用 __ 包含,但它的行为类似于 __ exact ,它会查找确切的单词(整个单词)在一个句子中,而不是一个单词的子字符串。



我很困惑,因为这样的功能是非常基本的,我不知道我是否缺少某些东西,或者还有另一种方法可以解决这个问题(使用Regex或某些东西?)



谢谢

解决方案

可以使用 EdgeNgramField 字段:

  some_field = indexes .EdgeNgramField()#也为此字段准备值,或使用model_attr 

然后进行部分匹配: p>

  SearchQuerySet()。all()。filter(some_field ='ear')
/ pre>

I have a Django project that uses SOLR for indexing.

I'm trying to do a substring search using Haystack's SearchQuerySet class.

For example, when a user searches for the term "ear", it should return the entry that has a field with the value: "Search". As you can see, "ear" is a SUBSTRING of "Search". (obviously :))

In other words, in a perfect Django world I would like something like:

SearchQuerySet().all().filter(some_field__contains_substring='ear')

In the haystack documentation for SearchQuerySet (https://django-haystack.readthedocs.org/en/latest/searchqueryset_api.html#field-lookups), it says that only the following FIELD LOOKUP types are supported:

  • contains
  • exact
  • gt, gte, lt, lte
  • in
  • startswith
  • range

I tried using __contains, but it behaves exactly like __exact, which looks up the exact word (the whole word) in a sentence, not a substring of a word.

I am confused, because such a functionality is pretty basic, and I'm not sure if I'm missing something, or there is another way to approach this problem (using Regex or something?).

Thanks

解决方案

That could be done using EdgeNgramField field:

some_field = indexes.EdgeNgramField() # also prepare value for this field or use model_attr

Then for partial match:

SearchQuerySet().all().filter(some_field='ear')

这篇关于Django Haystack - 使用SearchQuerySet()过滤字段的子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆