按日期排序不适用于日期类型字段

时间:2014-07-31 01:52:43

标签: sorting elasticsearch

我想在搜索结果上按日期进行简单的排序。但它似乎没有用。请帮我确定一下我错过了什么。

映射 -

{"document" : {
    "properties" : {
      "content" : {
        "type" : "string"
      },
      "modifiedDate" : {
        "type" : "date"
      }
    }
  }
}

日期按以下格式编制索引 -

"modifiedDate": [
                  "2014-02-03T13:17:55.000Z"
               ]

搜索查询非常大,所以只粘贴下面查询的排序部分 -

"sort": [
     {
       "modifiedDate": {
         "order": "desc" , "missing" : "_last" , "ignore_unmapped" : true
       }
     }
   ]

尝试过,只是命令parm -

"sort": {"modifiedDate": {"order": "desc"}}

但结果未排序。

在搜索结果结果中,我看到排序下面的文字,我期待毫秒 -

"sort": [
               "P\u0002\u0000\u0000"
        ]

请告知我错过了什么,非常感谢你的帮助!!

1 个答案:

答案 0 :(得分:0)

根据您提供的信息,有些事情并不明显。完整的卷曲重新创建(https://www.elasticsearch.org/help/)将真正帮助指出问题。以下是我使用1.3.0版本浏览您的场景时所获得的内容:

curl -XPOST "http://localhost:9200/testindex" -d'
{
  "mappings": {
    "document": {
      "properties": {
        "content": {
          "type": "string"
        },
        "modifiedDate": {
          "type": "date"
        }
      }
    }
  }
}'


curl -XPOST "http://localhost:9200/testindex/document/1" -d'
{
  "modifiedDate": [
                  "2014-02-03T13:17:55.000Z"
               ]
}'

curl -XPOST "http://localhost:9200/testindex/document/2" -d'
{
  "modifiedDate": [
                  "2014-02-04T13:17:55.000Z"
               ]
}'

然后运行搜索:

curl -XGET "http://localhost:9200/testindex/_search" -d'
{
  "sort": [
    {
      "modifiedDate": {
        "order": "desc",
        "missing" : "_last", 
        "ignore_unmapped" : true
      }
    }
  ]
}'

以下是回复:

{
   "took": 4,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 2,
      "max_score": null,
      "hits": [
         {
            "_index": "testindex",
            "_type": "document",
            "_id": "2",
            "_score": null,
            "_source": {
               "modifiedDate": [
                  "2014-02-04T13:17:55.000Z"
               ]
            },
            "sort": [
               1391519875000
            ]
         },
         {
            "_index": "testindex",
            "_type": "document",
            "_id": "1",
            "_score": null,
            "_source": {
               "modifiedDate": [
                  "2014-02-03T13:17:55.000Z"
               ]
            },
            "sort": [
               1391433475000
            ]
         }
      ]
   }
}