擅长:python、mysql、java
<p>您可以使用函数得分在搜索请求中实现此行为,其思想是,每个其他文档的得分为1(默认值),而文档2的得分较低,然后按“_得分”、“id”排序。这是DSL,请尝试从python API构造查询:</p>
<pre><code>{
"_source": ["id"],
"query": {
"function_score": {
"query": {
"bool": {
//add your query here
"must": [
{
"terms": {
"id": [1, 2, 3, 70]
}
}
]
}
},
"functions": [
{
"filter": {
"term": {
"id": 2
}
},
"weight": 0.5
}
]
}
},
"sort": [
"_score",
{
"id": {
"order": "asc"
}
}
]
}
</code></pre>
<p>同样,正如Yifeng在评论部分所述,您可以在从ES查询后对结果重新排序</p>