Skip to content

Instantly share code, notes, and snippets.

@TomonoriSoejima
Last active July 15, 2017 05:39
Show Gist options
  • Save TomonoriSoejima/7858795a54afc5f18b5ade77a8be18c8 to your computer and use it in GitHub Desktop.
Save TomonoriSoejima/7858795a54afc5f18b5ade77a8be18c8 to your computer and use it in GitHub Desktop.
painless timestamp update sample
GET hotel2/_search
{
"_source": [
"created",
"created2"
],
"script_fields": {
"original_date": {
"script": {
"lang": "painless",
"inline": "new SimpleDateFormat('yyyy/MM/dd').format(new Date(doc['created'].value))"
}
},
"new_date": {
"script": {
"lang": "painless",
"inline": "new SimpleDateFormat('yyyy/MM/dd').format(new Date(doc['created'].value - 24*60*60*1000))"
}
},
"today": {
"script": {
"lang": "painless",
"inline": "new SimpleDateFormat('yyyy/MM/dd').format(new Date())"
}
}
,
"test": {
"script": {
"lang": "painless",
"inline": "DayOfWeek.MONDAY.plus(4)"
}
}
}
}
PUT test/test/1
{"created":"2017-05-13T07:03:52.000Z"}
GET /test/_search
{
"size" : 4,
"query" : {
"match_all": {}
},
"script_fields" : {
"original_date" : {
"script" : {
"lang": "painless",
"inline": "new SimpleDateFormat('yyyy/MM/dd').format(new Date(doc['created'].value))"
}
},
"new_date" : {
"script" : {
"lang": "painless",
"inline": "new SimpleDateFormat('yyyy/MM/dd').format(new Date(doc['created'].value - 24*60*60*1000))"
}
}
}
}
GET hotel2/_search
{
"_source": [
"created",
"created2"
],
"script_fields": {
"original_date": {
"script": {
"lang": "painless",
"inline": "new SimpleDateFormat('yyyy/MM/dd').format(new Date(doc['created'].value))"
}
},
"new_date": {
"script": {
"lang": "painless",
"inline": "new SimpleDateFormat('yyyy/MM/dd').format(new Date(doc['created'].value - 24*60*60*1000))"
}
}
}
}
GET hotel2/_search
{
"size": 40,
"_source": [
"created",
"created2"
],
"script_fields": {
"original_date": {
"script": {
"lang": "painless",
"inline": "new SimpleDateFormat('yyyy/MM/dd').format(new Date(doc['created'].value))"
}
},
"new_date": {
"script": {
"lang": "painless",
"inline": "new SimpleDateFormat('yyyy/MM/dd').format(new Date(doc['created2'].value - 24*60*60*1000))"
}
},
"today": {
"script": {
"lang": "painless",
"inline": "new SimpleDateFormat('yyyy/MM/dd').format(new Date())"
}
},
"test": {
"script": {
"lang": "painless",
"inline": "DayOfWeek.MONDAY.plus(4)"
}
},
"diff": {
"script": {
"lang": "painless",
"inline": "def today = new SimpleDateFormat('yyyy-MM-dd').format(new Date()); def date1 = new SimpleDateFormat('yyyy-MM-dd').format(new Date(doc['created'].value - 24*60*60*1000)); def kyou = LocalDate.parse(today); def asu = LocalDate.parse(date1).plusDays(5); ChronoUnit.DAYS.between(kyou, asu); "
}
},
"today2": {
"script": {
"lang": "painless",
"inline": "doc['country.keyword'].value"
}
}
}
,"sort": {
"_script": {
"type": "string",
"script": {
"lang": "painless",
"inline": "def today = new SimpleDateFormat('yyyy-MM-dd').format(new Date()); def date1 = new SimpleDateFormat('yyyy-MM-dd').format(new Date(doc['created'].value)); def kyou = LocalDate.parse(today); def asu = LocalDate.parse(date1); ChronoUnit.DAYS.between(kyou, asu); "
}
}
}
}
GET hotel2/_search
{
"size": 10,
"_source": [
"created",
"created2"
],
"script_fields": {
"today": {
"script": {
"lang": "painless",
"inline": "new SimpleDateFormat('yyyy/MM/dd').format(new Date())"
}
}
},
"sort": {
"_script": {
"type": "number",
"order": "desc",
"script": {
"lang": "painless",
"inline": "def today = new SimpleDateFormat('yyyy-MM-dd').format(new Date()); def date1 = new SimpleDateFormat('yyyy-MM-dd').format(new Date(doc['created'].value)); def kyou = LocalDate.parse(today); def asu = LocalDate.parse(date1); def diff = ChronoUnit.DAYS.between(kyou, asu); diff <= 6 ? diff : 0"
}
}
}
}
GET hotel2/_search
{
"size": 50,
"_source": [
"created",
"created2"
],
"script_fields": {
"today": {
"script": {
"lang": "painless",
"inline": "new SimpleDateFormat('yyyy/MM/dd').format(new Date())"
}
},
"diff": {
"script": {
"lang": "painless",
"inline": "def today = new SimpleDateFormat('yyyy-MM-dd').format(new Date()); def date1 = new SimpleDateFormat('yyyy-MM-dd').format(new Date(doc['created'].value)); def kyou = LocalDate.parse(today); def asu = LocalDate.parse(date1).minusDays(9); def diff = ChronoUnit.DAYS.between(kyou, asu); diff"
}
}
},
"sort": {
"_script": {
"type": "number",
"order": "desc",
"script": {
"lang": "painless",
"inline": "def today = new SimpleDateFormat('yyyy-MM-dd').format(new Date()); def date1 = new SimpleDateFormat('yyyy-MM-dd').format(new Date(doc['created'].value)); def kyou = LocalDate.parse(today); def asu = LocalDate.parse(date1).minusDays(9); def diff = ChronoUnit.DAYS.between(kyou, asu); (diff >= -6 && diff <= 6 ) ? diff : Integer.MAX_VALUE"
}
}
}
}
result
======
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 100,
"max_score": null,
"hits": [
{
"_index": "hotel2",
"_type": "type1",
"_id": "29",
"_score": null,
"_source": {
"created": "2017-07-21",
"created2": "2017-07-19"
},
"fields": {
"diff": [
6
],
"today": [
"2017/07/06"
]
},
"sort": [
6
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "4",
"_score": null,
"_source": {
"created": "2017-07-21",
"created2": "2017-07-10"
},
"fields": {
"diff": [
6
],
"today": [
"2017/07/06"
]
},
"sort": [
6
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "35",
"_score": null,
"_source": {
"created": "2017-07-21",
"created2": "2017-07-22"
},
"fields": {
"diff": [
6
],
"today": [
"2017/07/06"
]
},
"sort": [
6
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "80",
"_score": null,
"_source": {
"created": "2017-07-21",
"created2": "2017-07-24"
},
"fields": {
"diff": [
6
],
"today": [
"2017/07/06"
]
},
"sort": [
6
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "71",
"_score": null,
"_source": {
"created": "2017-07-21",
"created2": "2017-07-16"
},
"fields": {
"diff": [
6
],
"today": [
"2017/07/06"
]
},
"sort": [
6
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "77",
"_score": null,
"_source": {
"created": "2017-07-21",
"created2": "2017-07-20"
},
"fields": {
"diff": [
6
],
"today": [
"2017/07/06"
]
},
"sort": [
6
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "69",
"_score": null,
"_source": {
"created": "2017-07-21",
"created2": "2017-07-29"
},
"fields": {
"diff": [
6
],
"today": [
"2017/07/06"
]
},
"sort": [
6
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "40",
"_score": null,
"_source": {
"created": "2017-07-20",
"created2": "2017-07-09"
},
"fields": {
"diff": [
5
],
"today": [
"2017/07/06"
]
},
"sort": [
5
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "6",
"_score": null,
"_source": {
"created": "2017-07-20",
"created2": "2017-07-29"
},
"fields": {
"diff": [
5
],
"today": [
"2017/07/06"
]
},
"sort": [
5
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "55",
"_score": null,
"_source": {
"created": "2017-07-20",
"created2": "2017-07-10"
},
"fields": {
"diff": [
5
],
"today": [
"2017/07/06"
]
},
"sort": [
5
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "33",
"_score": null,
"_source": {
"created": "2017-07-19",
"created2": "2017-07-30"
},
"fields": {
"diff": [
4
],
"today": [
"2017/07/06"
]
},
"sort": [
4
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "51",
"_score": null,
"_source": {
"created": "2017-07-19",
"created2": "2017-07-09"
},
"fields": {
"diff": [
4
],
"today": [
"2017/07/06"
]
},
"sort": [
4
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "86",
"_score": null,
"_source": {
"created": "2017-07-19",
"created2": "2017-07-21"
},
"fields": {
"diff": [
4
],
"today": [
"2017/07/06"
]
},
"sort": [
4
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "89",
"_score": null,
"_source": {
"created": "2017-07-18",
"created2": "2017-07-29"
},
"fields": {
"diff": [
3
],
"today": [
"2017/07/06"
]
},
"sort": [
3
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "78",
"_score": null,
"_source": {
"created": "2017-07-18",
"created2": "2017-07-22"
},
"fields": {
"diff": [
3
],
"today": [
"2017/07/06"
]
},
"sort": [
3
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "41",
"_score": null,
"_source": {
"created": "2017-07-17",
"created2": "2017-07-12"
},
"fields": {
"diff": [
2
],
"today": [
"2017/07/06"
]
},
"sort": [
2
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "82",
"_score": null,
"_source": {
"created": "2017-07-17",
"created2": "2017-07-18"
},
"fields": {
"diff": [
2
],
"today": [
"2017/07/06"
]
},
"sort": [
2
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "57",
"_score": null,
"_source": {
"created": "2017-07-17",
"created2": "2017-07-28"
},
"fields": {
"diff": [
2
],
"today": [
"2017/07/06"
]
},
"sort": [
2
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "46",
"_score": null,
"_source": {
"created": "2017-07-16",
"created2": "2017-07-09"
},
"fields": {
"diff": [
1
],
"today": [
"2017/07/06"
]
},
"sort": [
1
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "75",
"_score": null,
"_source": {
"created": "2017-07-16",
"created2": "2017-07-18"
},
"fields": {
"diff": [
1
],
"today": [
"2017/07/06"
]
},
"sort": [
1
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "43",
"_score": null,
"_source": {
"created": "2017-07-15",
"created2": "2017-07-23"
},
"fields": {
"diff": [
0
],
"today": [
"2017/07/06"
]
},
"sort": [
0
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "18",
"_score": null,
"_source": {
"created": "2017-07-15",
"created2": "2017-08-01"
},
"fields": {
"diff": [
0
],
"today": [
"2017/07/06"
]
},
"sort": [
0
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "17",
"_score": null,
"_source": {
"created": "2017-07-15",
"created2": "2017-07-09"
},
"fields": {
"diff": [
0
],
"today": [
"2017/07/06"
]
},
"sort": [
0
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "56",
"_score": null,
"_source": {
"created": "2017-07-15",
"created2": "2017-07-12"
},
"fields": {
"diff": [
0
],
"today": [
"2017/07/06"
]
},
"sort": [
0
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "63",
"_score": null,
"_source": {
"created": "2017-07-15",
"created2": "2017-07-24"
},
"fields": {
"diff": [
0
],
"today": [
"2017/07/06"
]
},
"sort": [
0
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "15",
"_score": null,
"_source": {
"created": "2017-07-14",
"created2": "2017-07-17"
},
"fields": {
"diff": [
-1
],
"today": [
"2017/07/06"
]
},
"sort": [
-1
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "47",
"_score": null,
"_source": {
"created": "2017-07-14",
"created2": "2017-08-03"
},
"fields": {
"diff": [
-1
],
"today": [
"2017/07/06"
]
},
"sort": [
-1
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "21",
"_score": null,
"_source": {
"created": "2017-07-13",
"created2": "2017-07-29"
},
"fields": {
"diff": [
-2
],
"today": [
"2017/07/06"
]
},
"sort": [
-2
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "76",
"_score": null,
"_source": {
"created": "2017-07-13",
"created2": "2017-07-16"
},
"fields": {
"diff": [
-2
],
"today": [
"2017/07/06"
]
},
"sort": [
-2
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "5",
"_score": null,
"_source": {
"created": "2017-07-12",
"created2": "2017-07-28"
},
"fields": {
"diff": [
-3
],
"today": [
"2017/07/06"
]
},
"sort": [
-3
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "13",
"_score": null,
"_source": {
"created": "2017-07-12",
"created2": "2017-08-03"
},
"fields": {
"diff": [
-3
],
"today": [
"2017/07/06"
]
},
"sort": [
-3
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "28",
"_score": null,
"_source": {
"created": "2017-07-12",
"created2": "2017-07-29"
},
"fields": {
"diff": [
-3
],
"today": [
"2017/07/06"
]
},
"sort": [
-3
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "53",
"_score": null,
"_source": {
"created": "2017-07-12",
"created2": "2017-07-28"
},
"fields": {
"diff": [
-3
],
"today": [
"2017/07/06"
]
},
"sort": [
-3
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "64",
"_score": null,
"_source": {
"created": "2017-07-12",
"created2": "2017-07-16"
},
"fields": {
"diff": [
-3
],
"today": [
"2017/07/06"
]
},
"sort": [
-3
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "27",
"_score": null,
"_source": {
"created": "2017-07-11",
"created2": "2017-07-18"
},
"fields": {
"diff": [
-4
],
"today": [
"2017/07/06"
]
},
"sort": [
-4
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "61",
"_score": null,
"_source": {
"created": "2017-07-11",
"created2": "2017-07-15"
},
"fields": {
"diff": [
-4
],
"today": [
"2017/07/06"
]
},
"sort": [
-4
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "24",
"_score": null,
"_source": {
"created": "2017-07-10",
"created2": "2017-08-02"
},
"fields": {
"diff": [
-5
],
"today": [
"2017/07/06"
]
},
"sort": [
-5
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "65",
"_score": null,
"_source": {
"created": "2017-07-10",
"created2": "2017-07-21"
},
"fields": {
"diff": [
-5
],
"today": [
"2017/07/06"
]
},
"sort": [
-5
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "70",
"_score": null,
"_source": {
"created": "2017-07-10",
"created2": "2017-07-25"
},
"fields": {
"diff": [
-5
],
"today": [
"2017/07/06"
]
},
"sort": [
-5
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "100",
"_score": null,
"_source": {
"created": "2017-07-10",
"created2": "2017-07-10"
},
"fields": {
"diff": [
-5
],
"today": [
"2017/07/06"
]
},
"sort": [
-5
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "14",
"_score": null,
"_source": {
"created": "2017-07-09",
"created2": "2017-07-15"
},
"fields": {
"diff": [
-6
],
"today": [
"2017/07/06"
]
},
"sort": [
-6
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "19",
"_score": null,
"_source": {
"created": "2017-07-25",
"created2": "2017-07-21"
},
"fields": {
"diff": [
10
],
"today": [
"2017/07/06"
]
},
"sort": [
-9999999
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "22",
"_score": null,
"_source": {
"created": "2017-07-23",
"created2": "2017-07-21"
},
"fields": {
"diff": [
8
],
"today": [
"2017/07/06"
]
},
"sort": [
-9999999
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "25",
"_score": null,
"_source": {
"created": "2017-07-25",
"created2": "2017-07-06"
},
"fields": {
"diff": [
10
],
"today": [
"2017/07/06"
]
},
"sort": [
-9999999
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "26",
"_score": null,
"_source": {
"created": "2017-07-29",
"created2": "2017-07-24"
},
"fields": {
"diff": [
14
],
"today": [
"2017/07/06"
]
},
"sort": [
-9999999
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "44",
"_score": null,
"_source": {
"created": "2017-08-01",
"created2": "2017-07-18"
},
"fields": {
"diff": [
17
],
"today": [
"2017/07/06"
]
},
"sort": [
-9999999
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "48",
"_score": null,
"_source": {
"created": "2017-07-27",
"created2": "2017-08-01"
},
"fields": {
"diff": [
12
],
"today": [
"2017/07/06"
]
},
"sort": [
-9999999
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "52",
"_score": null,
"_source": {
"created": "2017-08-01",
"created2": "2017-07-22"
},
"fields": {
"diff": [
17
],
"today": [
"2017/07/06"
]
},
"sort": [
-9999999
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "60",
"_score": null,
"_source": {
"created": "2017-07-06",
"created2": "2017-07-23"
},
"fields": {
"diff": [
-9
],
"today": [
"2017/07/06"
]
},
"sort": [
-9999999
]
},
{
"_index": "hotel2",
"_type": "type1",
"_id": "73",
"_score": null,
"_source": {
"created": "2017-07-05",
"created2": "2017-07-19"
},
"fields": {
"diff": [
-10
],
"today": [
"2017/07/06"
]
},
"sort": [
-9999999
]
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment