我的数据库中有一些对象: 对象 1、对象 2、对象 3、..、对象 n
现在我正在做这样的过滤
MyDocument.search().filter("match", is_active=True).sort('id').execute()
输出:
searchDocumentobject 1, searchDocumentobject 2,searchDocumentobject 3, ....
现在我需要在列表的最后搜索Documentobject 2。
需要这样的输出:
searchDocumentobject 1,searchDocumentobject 3, .... , searchDocumentobject 2
谢谢
最佳答案
在MyModel中,添加一个新方法,如果你想最后保留该文档,则返回0,否则返回1。
class MyModel(models.Model):
# Add new method here
def get_rank(self):
if self.id == 2: # your condition here
return 0 # return 0, if you want to keep it at last
return 1
现在,您可以在 MyDocument 中使用此方法。在 MyDocument 中添加一个新字段,我们将使用该字段进行排序。
class MyDocument(Document):
# Add new field here
rank = fields.IntegerField(attr='get_rank')
现在,你可以这样查询,
MyDocument.search().filter("match", is_active=True).sort('-rank', 'id').execute()
关于python - 具有特定输出的 Elasticsearch DSL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61074041/