Django-Haystack与Solr包含搜索 [英] Django-Haystack with Solr contains search

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

问题描述

我使用 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,而是减少contains功能。例如,设置minGramSize =3将要求您的包含查询中至少包含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.

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

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