Django 全文搜索按相关性顺序

发布于 2024-09-15 02:06:07 字数 461 浏览 3 评论 0 原文

我正在使用 Django 查询过滤器 __search 来执行全文搜索,例如,

MyModel.objects.filter(title__search = 'some title')

如何按相关性对其进行排序,因为目前它似乎是按字母顺序排序的?

具体来说,我希望标题为some title的搜索结果首先出现在标题不同但包含some title的内容之前。

编辑:

我注意到,在 MyModel 的模型定义上,我有:

class Meta:
    ordering = ['title']

如果删除它,则排序将变得正确,即按相关性排序。那么有没有一种方法可以将其保留在模型定义中,因为它在其他地方有用,但然后在我的查询中告诉它忽略它?

I am using the Django query filter __search to perform a full text search e.g

MyModel.objects.filter(title__search = 'some title')

How do I get it to order by relevance, as currently it seems to be ordering alphabetically?

Specifically I would like search results where the title was some title to appear first before something that had the title a different but contains some title.

edit:

What I've noticed is that on the model definition for MyModel I have:

class Meta:
    ordering = ['title']

If I remove this then the ordering becomes correct i.e. sorted by relevance. So is there a way I can leave this in the model definition as its useful elsewhere but then on my query tell it to ignore it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挽袖吟 2024-09-22 02:06:07

正如此处所注意到的,搜索是布尔值

没有这样的用于排序的相关性系数。

一个愚蠢的想法可以是按标题长度排序,这是有道理的。

As noticed here, the search is Boolean.

There's no such relevance coefficient to use for ordering.

A stupid idea can be ordering by title length, which can make sense.

残龙傲雪 2024-09-22 02:06:07

在 Django 项目中获得良好的全文搜索的最简单方法是使用优秀的 Haystack 应用程序。设置起来非常简单,特别是如果您使用最简单的搜索引擎后端(Whoosh ,这是纯Python)。您可以在几分钟内设置内容的全文索引,并提供按相关性排序的结果和许多其他不错的功能。如果您的成长超出了 Whoosh 的性能/并发/功能限制,因为您使用 Haystack 来抽象搜索功能,您可以交换类似 Solr 随时用于 Whoosh。

The easiest way to get good fulltext search in a Django project is to use the excellent Haystack app. It's ridiculously easy to set up, especially if you use the simplest search engine backend (Whoosh, which is pure Python). You can set up fulltext indexing of your content, with relevance-ordered results and lots of other nice features besides, in a matter of minutes. And if you outgrow Whoosh's performance/concurrency/feature limitations, since you're using Haystack to abstract the search features you can swap in something like Solr for Whoosh anytime.

前事休说 2024-09-22 02:06:07

尝试: Model.objects.all().order_by().search() - 调用 不带任何参数的 order_by 根本不排序

除此之外:我会赞同 Carl 对 Haystack 的推荐,特别是因为它允许更复杂的事情,比如词干(“dance”将匹配“dances”、“dancers”和“dancing”)、faceting(“向我显示用户和号码”)每个搜索结果的点击次数”),获取与当前显示的对象相似的对象等。当我上次尝试 Whoosh 时,它不稳定(即在索引期间崩溃),但它花了相当短的时间来触发启动 Solr,这很棒。

Try: Model.objects.all().order_by().search() - calling order_by without any parameters does no ordering at all.

Beyond that: I'll second Carl's recommendation of Haystack, particularly since that allows more complicated things like stemming ("dance" would match "dances" ,"dancers", and "dancing"), faceting ("Show me user & number of hits for each search result"), getting objects which are similar to the one you're currently displaying, etc. When I last tried Whoosh it was unstable (i.e. crashed during indexing) but it took a rather short period of time to fire up Solr, which is great.

自由范儿 2024-09-22 02:06:07

对于按相关性排序的搜索结果,将需要更多的内置布尔搜索。这里有两种方法(Sphinxsearch 和 Whoosh):

For search result sorted by relevance, it will take a little more the the builtin boolean search. Here are two approaches (Sphinxsearch and Whoosh):

雪若未夕 2024-09-22 02:06:07

如果使用 Django >= 1.2,则可以通过使用带有 ORDER BY 子句的原始搜索来实现相关性,

File.objects.raw(query_string, params[x,y,z])

但 raw_querset 目前有其自身的缺点,例如不支持 count() 。

然而,它确实带回了模型,因此非常易于使用,尽管不像 __search 那么简单

You can achieve this by using a raw search with an ORDER BY clause for the relevance if using Django >= 1.2

File.objects.raw(query_string, params[x,y,z])

Though raw_querset has its own disadvantages at the moment in not supporting count() for instance.

It does however bring back models so is pretty easy to use, though not as simple as __search

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文