I run the aggregation script in elastic dev tool:
GET copper_scan_username_index,fibre_scan_username_index/_search
{
"size": 0,
"aggs": {
"unique_username": {
"terms": {
"field": "username",
"min_doc_count": 2
},
"aggs": {
"top_events": {
"top_hits": {
"size": 10
}
}
}
}
}
}
Context of this script: it is to find out the overlap username between the two indexes by looking at if document with a username appeared on both side, then return those documents.
Got the results:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"unique_username" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "username1",
"doc_count" : 2,
"top_events" : {
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "copper_scan_username_index",
"_type" : "_doc",
"_id" : "dTXSY0KNxU08jKDnZcc-nbkAAAAAAAAA",
"_score" : 1.0,
"_source" : {
"count" : 3,
"copper_scan_count" : 3,
"username" : "username1"
}
},
{
"_index" : "fibre_scan_username_index",
"_type" : "_doc",
"_id" : "dTXSY0KNxU08jKDnZcc-nbkAAAAAAAAA",
"_score" : 1.0,
"_source" : {
"fibre_scan_count" : 1,
"count" : 1,
"username" : "username1"
}
}
]
}
}
}
]
}
}
}
This is what I want to save under a new index:
[
{
"_index" : "copper_scan_username_index",
"_type" : "_doc",
"_id" : "dTXSY0KNxU08jKDnZcc-nbkAAAAAAAAA",
"_score" : 1.0,
"_source" : {
"count" : 3,
"copper_scan_count" : 3,
"username" : "username1"
}
},
{
"_index" : "fibre_scan_username_index",
"_type" : "_doc",
"_id" : "dTXSY0KNxU08jKDnZcc-nbkAAAAAAAAA",
"_score" : 1.0,
"_source" : {
"fibre_scan_count" : 1,
"count" : 1,
"username" : "username1"
}
}
]
I tried reindex, but the result saved under the new index is not what is expected.
This is the reindex script:
POST /_reindex
{
"source": {
"index": ["copper_scan_username_index","fibre_scan_username_index"],
"aggs": {
"username_count": {
"terms": {
"field": "username",
"min_doc_count": 2
},
"aggs": {
"docs": {
"top_hits": {
"size": 10
}
},
"bucket_filter": {
"bucket_selector": {
"buckets_path": {
"count": "_count"
},
"script": "params.count >= 2"
}
}
}
}
}
},
"dest": {
"index": "new_index"
}
}
Can anyone help? Thanks a lot!