ElasticSeach - 日期排序 [英] ElasticSeach - Sorting on dates

查看:213
本文介绍了ElasticSeach - 日期排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个弹性搜索索引,我无法使用映射设置每个字段,所以日期将作为字符串...



有谁知道我会去关于在该字符串日期排序?



我看过_script

  {
查询:{
....
},
sort:{
_script:{
script :doc ['field_name']。value,
type:string,
order:asc
}
}
}

但由于分析字段...失败,因此

$ b

任何建议都会很棒!



谢谢

解决方案

p>如果日期的格式已知,您可以将该格式添加到dynamic_date_formats(查看此链接)设置。当您索引一个新的字符串字段时,它将被转换为可以按正常方式排序的日期类型。



示例:



创建没有属性的索引:

  curl -XPUT http:// localhost:9200 / dates -d '
{
日期:{
dynamic_date_formats:[yyyy-MM-dd,dd-MM-yyyy],
properties
}
}
}'

索引2文件: / p>

  curl -XPUT'http:// localhost:9200 / dates / dates / 1'-d'
{
任意日期:2013-01-01
}'

curl -XPUT'http:// localhost:9200 / dates / dates / 2'-d'
{
任意日期:2012-01-01
}'

如果您检查映射,您将看到字段不是字符串:

  curl -XGET'http: // localhost:9200 / dates / _mapping'

结果:

  {
dates:{
dates:{
properties:{
whateverDate日期,
格式:dateOptionalTime
}
}
}
}
}

现在您可以轻松排序:

  curl  - XGET'http:// localhost:9200 / dates / _search'-d'
{
query:{
match_all:{}
},
sort:[
{
whateverDate:{
order:asc
}
}
]
}'


I have an elastic search index which I cannot setup every field with a mapping so dates are going in as strings...

Does anyone know how I would go about sorting on that string date?

I have looked at _script

{
    "query" : {
        ....
    },
    "sort" : {
        "_script" : {
            "script" : "doc['field_name'].value",
            "type" : "string",
            "order" : "asc"
        }
    }
}

But this fails because its an analysed field...

Any suggestions would be great!

Thanks

解决方案

If the format of the date is known, you can add that format to the dynamic_date_formats (Check out this link) setting. When you index a new string field it will be converted to the date type which can be sorted in the normal way.

Example:

Create an index without properties:

curl -XPUT http://localhost:9200/dates -d '
{
    "dates" : {
        "dynamic_date_formats" : ["yyyy-MM-dd", "dd-MM-yyyy"],
        "properties" : {
        }
    }
}'

Index 2 documents:

curl -XPUT 'http://localhost:9200/dates/dates/1' -d '
{
    "arbitraryDate": "2013-01-01"
}'

curl -XPUT 'http://localhost:9200/dates/dates/2' -d '
{
    "arbitraryDate": "2012-01-01"
}'

If you check the mapping you will see that the field is not a string:

curl -XGET 'http://localhost:9200/dates/_mapping'

result:

{
  "dates": {
    "dates": {
      "properties": {
        "arbitraryDate": {
          "type": "date",
          "format": "dateOptionalTime"
        }
      }
    }
  }
}

Now you can sort easily:

curl -XGET 'http://localhost:9200/dates/_search' -d '
{
  "query": {
    "match_all": {}
  },
  "sort": [
    {
      "arbitraryDate": {
        "order": "asc"
      }
    }
  ]
}'

这篇关于ElasticSeach - 日期排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆