带有 Solr 的 Django-Haystack 包含搜索 [英] Django-Haystack with Solr contains search

查看:40
本文介绍了带有 Solr 的 Django-Haystack 包含搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 solr 作为后端的项目中使用 haystack.我希望能够执行包含搜索,类似于 Django .filter(something__contains="...")

I am using haystack within a project using solr as the backend. I want to be able to perform a contains search, similar to the Django .filter(something__contains="...")

__startswith 选项不适合我们的需要,顾名思义,它会查找以字符串开头的单词.

The __startswith option does not suit our needs as it, as the name suggests, looks for words that start with the string.

我尝试使用诸如 *keyword* 之类的东西,但 Solr 不允许将 * 用作第一个字符

I tried to use something like *keyword* but Solr does not allow the * to be used as the first character

谢谢.

推荐答案

要获得包含"功能,您可以使用:

To get "contains" functionallity you can use:

<tokenizer class="solr.WhitespaceTokenizerFactory"/>
<filter class="solr.EdgeNGramFilterFactory" minGramSize="1" maxGramSize="100" side="back"/>
<filter class="solr.LowerCaseFilterFactory" />

作为索引分析器.

这将为您领域中的每个空格分隔的单词创建 ngram.例如:

This will create ngrams for every whitespace separated word in your field. For example:

"Index this!" => x, ex, dex, ndex, index, !, s!, is!, his!, this!

如您所见,这将大大扩展您的索引,但如果您现在输入如下查询:

As you see this will expand your index greatly but if you now enter a query like:

"nde*"

它会匹配ndex"给你一个命中.

it will match "ndex" giving you a hit.

谨慎使用此方法以确保您的索引不会变得太大.如果增加 minGramSize 或减少 maxGramSize,它不会将索引扩展为 mutch,但会减少包含"功能.例如,设置 minGramSize="3" 将要求您的 contains 查询中至少有 3 个字符.

Use this approach carefully to make sure that your index doesn't get too large. If you increase minGramSize, or decrease maxGramSize it will not expand the index as mutch but reduce the "contains" functionallity. For instance setting minGramSize="3" will require that you have at least 3 characters in your contains query.

这篇关于带有 Solr 的 Django-Haystack 包含搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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