【发布时间】:2021-10-21 10:14:36
【问题描述】:
我是 Elasticsearch 的新手。
我应该如何修改以下代码以使返回结果按“lastpost”时间降序排列?
我是否应该在“正文”中的“查询”下方添加“排序”
@app.route('/')
def index():
return render_template('index.html')
@app.route('/search')
def analyzer():
bc = BertClient(ip='bertserving', output_fmt='list')
client = Elasticsearch('elasticsearch:9200')
query = request.args.get('q')
query_vector = bc.encode([query])[0]
script_query = {
"script_score": {
"query": {"match_all": {}},
"script": {
"source": "cosineSimilarity(params.query_vector, doc['text_vector']) + 1.0",
"params": {"query_vector": query_vector}
}
}
}
response = client.search(
index=INDEX_NAME,
body={
"size": SEARCH_SIZE,
"query": script_query,
"_source": {"includes": ["title", "id","lastpost"]}
}
)
print(query)
pprint(response)
return jsonify(response)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
【问题讨论】:
-
是的,这就是我要放的地方。
"sort" : { "lastpost": "desc" }
标签: python elasticsearch