-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpainless.js
52 lines (49 loc) · 1.15 KB
/
painless.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Replace field with literal string
{
"query": {
"match": {
"city": "Aesmsterdam"
}
},
"script": {
"inline": "ctx._source.city = \"Amsterdam\"",
"lang": "painless"
}
}
// Multi-statement script
{
"query": {
"match": {
"city": "Aesmsterdam"
}
},
"script": {
"inline": "ctx._source.city = \"Amsterdam\"; ctx._source.zip = \"1070 HL\";",
"lang": "painless"
}
}
// Extract sub-string from field using regex and setting new field with the extracted value
{
"script": {
"lang": "painless",
"source": "Matcher match = /.*\"job_id\": \"([^\"]+)\".*/.matcher(ctx._source.text_field); if (match.matches()) { ctx._source.extracted_field = match.group(1); } else { throw new Exception(ctx._source.text_field); }"
}
}
// Find duplicates by aggregating on calculated field
{
"size": 0,
"aggs": {
"duplicateCount": {
"terms": {
"script": "doc['first_name'] + doc['last_name']",
"min_doc_count": 2,
"size": 10000
},
"aggs": {
"duplicateDocuments": {
"top_hits": {}
}
}
}
}
}