如何建立索引,以便文本搜索和排序使用索引? [英] How to index so that text search and sort use the index?
问题描述
我有一个mongoengine文档,需要进行文本字段搜索然后进行排序.我设法创建了文本索引,但是排序操作非常慢.我需要一个建议来如何完全索引我的查询:
I have a mongoengine document that needs to have a text field search then be sorted. I managed to create the text index, but the sorting operation is comming by very slow. I need a suggestion how to fully index my query:
class MyDoc(Document):
meta = {
'collection': 'my_doc',
'index_background': True,
'indexes': [
'-sort_field',
{
'fields': ['$text_field'],
'default_language': 'portuguese',
}
],
}
text_field = StringField()
sort_field = DateTimeField()
MyDoc.objects.search_text('some text').sort(['-sort_field'])
推荐答案
您不能将文本索引与另一个索引一起用于排序.您可以使用 {"field":1,"t":"text"}
这样的复合索引,并将该索引用于 field
上的相等匹配和文本搜索,但是否则,文本索引不能作为复合索引的一部分.为什么要进行文本搜索然后在另一个字段上排序?这是一个不寻常的用例.返回少量文档(例如返回按日期排序的匹配结果页面)应该很好.
You can't use a text index with another index for the sort. You can have a compound index like { "field" : 1, "t" : "text" }
and use the index for equality matches on field
and text searches, but otherwise text indexes don't work as part of a compound index. Why do you want to do a text search and then sort on another field? It's a slightly unusual use case. It should be fine for returning small numbers of documents, like for returning a page of matching results sorted by date.
这篇关于如何建立索引,以便文本搜索和排序使用索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!