OK, I posted all my index mapping, JSON data, search script & the query output below. Thanks for helping me.
This is my index A mapping
{
"mapping": {
"sales_daily": {
"properties": {
"sales": {
"type": "double",
"coerce": false
},
"code": {
"type": "integer"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd || yyyy-MM-dd HH:mm:ss || epoch_millis"
}
"name": {
"type": "keyword",
"index": false,
"doc_values": false
}
}
}
}
}
This is my Index B mapping
{
"mapping": {
"sales_daily": {
"properties": {
"sales": {
"type": "double",
"coerce": false
},
"code": {
"type": "integer"
},
"date": {
"type": "date",
"format": "yyyy-MM-dd || yyyy-MM-dd HH:mm:ss || epoch_millis"
},
"name": {
"type": "keyword",
"index": false,
"doc_values": false
}
}
}
}
}
This is my JSON data from Index A and Index B in Kibana ( Both Index A & B got similar data, only the date time is difference )
{
"_index": "A",
"_type": "sales_daily",
"_id": "1LvEu2cBCkPfXhZI_QY3",
"_version": 1,
"_score": null,
"_source": {
"code": 2,
"name": "RedTB",
"date": "2009-12-31",
"sales": 87.85
},
"fields": {
"date": [
"2009-12-31T00:00:00.000Z"
]
},
"sort": [
1544745600000
]
}
{
"_index": "A",
"_type": "sales_daily",
"_id": "1LvEu2cBCkPfXhZI_QY3",
"_version": 1,
"_score": null,
"_source": {
"code": 1,
"name": "BlueTB",
"date": "2009-12-31",
"sales": 87.85
},
"fields": {
"date": [
"2009-12-31T00:00:00.000Z"
]
},
"sort": [
1544745600000
]
}
{
"_index": "B",
"_type": "sales_daily",
"_id": "1LvEu2cBCkPfXhZI_QY3",
"_version": 1,
"_score": null,
"_source": {
"code": 2,
"name": "RedTB",
"date": "2010-01-04",
"sales": 90.85
},
"fields": {
"date": [
"2010-01-04T00:00:00.000Z"
]
},
"sort": [
1544745600000
]
}
{
"_index": "B",
"_type": "sales_daily",
"_id": "1LvEu2cBCkPfXhZI_QY3",
"_version": 1,
"_score": null,
"_source": {
"code": 1,
"name": "BlueTB",
"date": "2010-01-04",
"sales": 80.85
},
"fields": {
"date": [
"2010-01-04T00:00:00.000Z"
]
},
"sort": [
1544745600000
]
}
This is my testing script
GET A,B/_search
{
"size": 0,
"query": {
"bool": {
"must": {
"range": {
"date": {
"gte": "2009-12-31 00:00:00",
"lt": "2010-01-05 00:00:00"
}
}
}
}
},
"aggs": {
"group_by_code": {
"terms": {
"field": "code"
},
"aggs": {
"group_by_date": {
"terms": {
"field": "date"
},
"aggs": {
"sales_value": {
"max": {
"field": "sales"
}
}
}
}
}
}
}
}
This is my testing script output
{
"took" : 29,
"timed_out" : false,
"_shards" : {
"total" : 6,
"successful" : 6,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 20,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"group_by_code" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 1,
"doc_count" : 2,
"group_by_date" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 1262217600000,
"key_as_string" : "2009-12-31",
"doc_count" : 1,
"sales_value" : {
"value" : 87.85
}
},
{
"key" : 1262563200000,
"key_as_string" : "2010-01-04",
"doc_count" : 1,
"sales_value" : {
"value" : 80.85
}
}
]
}
},
{
"key" : 2,
"doc_count" : 2,
"group_by_date" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 1262217600000,
"key_as_string" : "2009-12-31",
"doc_count" : 1,
"sales_value" : {
"value" : 87.85
}
},
{
"key" : 1262563200000,
"key_as_string" : "2010-01-04",
"doc_count" : 1,
"sales_value" : {
"value" : 90.85
}
}
]
}
}
]
}
}
}