I am using the Django query filter __search to perform a full text search e.g
我正在使用Django查询过滤器__search来执行全文搜索,例如
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:
我注意到的是,在MyModel的模型定义中,我有:
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?
如果我删除它,那么排序变得正确,即按相关性排序。那么有没有一种方法可以将其留在模型定义中,因为它在其他地方很有用但是在我的查询中告诉它忽略它?
5 个解决方案
#1
0
Try: Model.objects.all().order_by().search()
- calling order_by without any parameters does no ordering at all.
尝试:Model.objects.all()。order_by()。search() - 调用order_by没有任何参数根本没有排序。
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.
除此之外:我将继续推荐卡尔对干草堆的推荐,特别是因为它允许更复杂的事情,如词干(“舞蹈”将匹配“舞蹈”,“舞者”和“舞蹈”),刻面(“显示用户和数量命中每个搜索结果“),获取与您当前正在显示的对象类似的对象等。当我上次尝试飞快移动时,它不稳定(即在索引期间崩溃)但是需要相当短的时间才能启动索尔,这很棒。
#2
#3
2
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.
在Django项目中获得良好全文搜索的最简单方法是使用优秀的Haystack应用程序。设置起来非常容易,特别是如果你使用最简单的搜索引擎后端(Whoosh,它是纯Python)。您可以在几分钟内设置内容的全文索引,具有相关性排序结果和许多其他不错的功能。如果你超过了Whoosh的性能/并发/功能限制,因为你使用Haystack来抽象搜索功能,你可以随时交换像Solr for Whoosh这样的东西。
#4
0
For search result sorted by relevance, it will take a little more the the builtin boolean search. Here are two approaches (Sphinxsearch and Whoosh):
对于按相关性排序的搜索结果,内置布尔搜索需要更多一些。这里有两种方法(Sphinxsearch和Whoosh):
#5
0
You can achieve this by using a raw search with an ORDER BY clause for the relevance if using Django >= 1.2
如果使用Django> = 1.2,则可以使用带有ORDER BY子句的原始搜索来实现相关性
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.
虽然raw_querset目前在不支持count()方面有其自身的缺点。
It does however bring back models so is pretty easy to use, though not as simple as __search
然而它确实带回了模型,因此非常容易使用,尽管不像__search那么简单
#1
0
Try: Model.objects.all().order_by().search()
- calling order_by without any parameters does no ordering at all.
尝试:Model.objects.all()。order_by()。search() - 调用order_by没有任何参数根本没有排序。
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.
除此之外:我将继续推荐卡尔对干草堆的推荐,特别是因为它允许更复杂的事情,如词干(“舞蹈”将匹配“舞蹈”,“舞者”和“舞蹈”),刻面(“显示用户和数量命中每个搜索结果“),获取与您当前正在显示的对象类似的对象等。当我上次尝试飞快移动时,它不稳定(即在索引期间崩溃)但是需要相当短的时间才能启动索尔,这很棒。
#2
3
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.
一个愚蠢的想法可以按标题长度排序,这是有道理的。
#3
2
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.
在Django项目中获得良好全文搜索的最简单方法是使用优秀的Haystack应用程序。设置起来非常容易,特别是如果你使用最简单的搜索引擎后端(Whoosh,它是纯Python)。您可以在几分钟内设置内容的全文索引,具有相关性排序结果和许多其他不错的功能。如果你超过了Whoosh的性能/并发/功能限制,因为你使用Haystack来抽象搜索功能,你可以随时交换像Solr for Whoosh这样的东西。
#4
0
For search result sorted by relevance, it will take a little more the the builtin boolean search. Here are two approaches (Sphinxsearch and Whoosh):
对于按相关性排序的搜索结果,内置布尔搜索需要更多一些。这里有两种方法(Sphinxsearch和Whoosh):
#5
0
You can achieve this by using a raw search with an ORDER BY clause for the relevance if using Django >= 1.2
如果使用Django> = 1.2,则可以使用带有ORDER BY子句的原始搜索来实现相关性
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.
虽然raw_querset目前在不支持count()方面有其自身的缺点。
It does however bring back models so is pretty easy to use, though not as simple as __search
然而它确实带回了模型,因此非常容易使用,尽管不像__search那么简单