为什么Elasticsearch不会按降序对我的记录进行排序?

时间:2016-10-12 14:15:03

标签: ruby-on-rails ruby elasticsearch

我有一个包含purchased_at文档的索引。它的映射是:

"purchased_at" : {
  "format" : "strict_date_optional_time||epoch_millis",
  "type" : "date"
}

文档包含以下格式的字段值:"2013-06-21T11:07:07.000Z"The docs说排序:

  

_score未计算,因为它未用于排序。

如果使用了sort参数,这是一个非常好的指标。我在版本2.0.0中使用elasticsearch gem。什么工作是按升序排序:

MyModel.search("original_id:#{uniq_original_id}", sort: 'purchased_at')

什么不起作用:

# syntax documented here:
# https://github.com/elastic/elasticsearch-rails/blob/15761247f3e99654bda946a178e50b5365414b59/elasticsearch-model/test/integration/active_record_pagination_test.rb#L106
MyModel.search("original_id:#{uniq_original_id}", sort: [{purchased_at: 'desc'}])

# syntax used here:
# https://github.com/elastic/elasticsearch-rails/blob/15761247f3e99654bda946a178e50b5365414b59/elasticsearch-persistence/examples/notes/application.rb#L114
MyModel.search("original_id:#{uniq_original_id}", sort: [{purchased_at: {order: 'desc'}}])

# syntax used here:
# https://github.com/elastic/elasticsearch-rails/blob/15761247f3e99654bda946a178e50b5365414b59/elasticsearch-rails/lib/rails/templates/searchable.rb#L132
MyModel.search("original_id:#{uniq_original_id}", sort: {purchased_at: 'desc'})

毋庸置疑,我很困惑。如何按降序对字段进行排序?

1 个答案:

答案 0 :(得分:0)

你试过吗? “排序”:{   “purchased_at”:   {“order”:“desc”} }

PS。我不能发表评论,这就是我将此作为答案发布的原因