更新/插入后Elasticsearch排序失败

时间:2013-06-14 22:00:01

标签: python elasticsearch pyes

我正在将文档插入到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

我希望得到“林戈,约翰,保罗”的订购,而是订购“约翰,保罗,林戈”。这可能是什么原因?我看到这里有一个错误: https://github.com/elasticsearch/elasticsearch/issues/3078 但这似乎影响ES .90.0而我正在使用.90.1。

1 个答案:

答案 0 :(得分:1)

应该是:

sort={"_ts":"desc"}
相关问题