我正在将文档插入elasticsearch并尝试对所有文档中存在的给定字段进行排序。但是,每当我更新文档时,索引编制似乎都中断了,并且没有得到排序的顺序。我通过执行以下操作创建了索引:
self.conn = ES(server=url)
self.conn.create_index("test.test")
例如,我想对“_ts”字段进行排序。给定以下字典和代码:
def update_or_insert(doc):
doc_type = "string"
index = doc['ns']
doc['_id'] = str(doc['_id'])
doc_id = doc['_id']
self.conn.index(doc, index, doc_type, doc_id)
to_insert = [
{'_id': '4', 'name': 'John', '_ts': 3, 'ns':'test.test'},
{'_id': '5', 'name': 'Paul', '_ts': 2', ns':'test.test'},
{'_id': '6', 'name': 'George', '_ts': 1', ns':'test.test'},
{'_id': '6', 'name': 'Ringo', '_ts': 4, 'ns':'test.test'} ,
]
for x in to_insert:
update_or_insert(x)
result = self.conn.search(q, sort={'_ts:desc'})
for it in result:
print it
我希望获得“Ringo,John,Paul”的订单,但获得“John,Paul,Ringo”的订单。有什么原因可能是这种情况?我发现这里有一个错误:
https://github.com/elasticsearch/elasticsearch/issues/3078
但这似乎影响ES .90.0,而我使用的是0.90.1。
最佳答案
它应该是:
sort={"_ts":"desc"}
关于python - 更新/插入后,Elasticsearch排序失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17117881/