我有下面的文档和搜索查询,当我执行查询时,弹性正在获取不匹配的文档。场景是针对匹配的例外。它应该进行聚合,但它正在获取所有文档,聚合也不起作用。
1( 项目
2( 位置
3( 运输模式
4( 源位置
5( 发货日期
6( 到达日期
映射:
{
"mappings": {
"recommendations": {
"properties": {
"recommendations": {
"type": "nested"
}
}
}
}
}
文件:
{
"recommendations": [
{
"id": "578bd845-3ca9-495f-bc13-dbcc48e8f415",
"tenantId": "0d474f0a-f060-4828-b2ec-a81c4b27fa2e",
"clusterId": "1",
"eventId": "1",
"exceptionId": "1",
"eventType": "Delayed",
"item": "Item1",
"location": "DC1",
"dueDate": "2019-01-10T05:30:00.000+0530",
"exceptionQuantity": 100,
"recommendationType": "stockTransfer",
"customerName": "Walmart",
"primaryRecommendation": true,
"priority": 1,
"sourceLocation": "DC2",
"transferQuantity": 100,
"shipDate": "2019-01-09T05:30:00.000+0530",
"arrivalDate": "2019-01-10T05:30:00.000+0530",
"transportMode": "Road",
"transferCost": 500,
"maxQtyAvailableForTransfer": 100,
"totalQtyAtSource": 100,
"operation": "Road-Item1-from-DC2-to-DC1",
"peggedStockDemandId": "1",
"revenueRecovered": "20000.0"
},
{
"id": "578bd845-3ca9-495f-bc13-dbcc48e8f415",
"tenantId": "0d474f0a-f060-4828-b2ec-a81c4b27fa2e",
"clusterId": "1",
"eventId": "1",
"exceptionId": "1",
"eventType": "Delayed",
"item": "Item1",
"location": "DC1",
"dueDate": "2019-01-10T05:30:00.000+0530",
"exceptionQuantity": 100,
"recommendationType": "stockTransfer",
"customerName": "Walmart",
"primaryRecommendation": true,
"priority": 1,
"sourceLocation": "DC2",
"transferQuantity": 100,
"shipDate": "2019-01-09T05:30:00.000+0530",
"arrivalDate": "2019-01-10T05:30:00.000+0530",
"transportMode": "Road",
"transferCost": 500,
"maxQtyAvailableForTransfer": 100,
"totalQtyAtSource": 100,
"operation": "Road-Item1-from-DC2-to-DC1",
"peggedStockDemandId": "1",
"revenueRecovered": "20000.0"
},
{
"id": "578bd845-3ca9-495f-bc13-dbcc48e8f415",
"tenantId": "0d474f0a-f060-4828-b2ec-a81c4b27fa2e",
"clusterId": "1",
"eventId": "1",
"exceptionId": "2",
"eventType": "Delayed",
"item": "Item1",
"location": "DC1",
"dueDate": "2019-01-10T05:30:00.000+0530",
"exceptionQuantity": 100,
"recommendationType": "stockTransfer",
"customerName": "Walmart",
"primaryRecommendation": true,
"priority": 1,
"sourceLocation": "DC2",
"transferQuantity": 100,
"shipDate": "2019-01-09T05:30:00.000+0530",
"arrivalDate": "2019-01-10T05:30:00.000+0530",
"transportMode": "Road",
"transferCost": 500,
"maxQtyAvailableForTransfer": 100,
"totalQtyAtSource": 100,
"operation": "Road-Item1-from-DC2-to-DC1",
"peggedStockDemandId": "1",
"revenueRecovered": "20000.0"
}
]
}
查询:
{
"aggregations": {
"exceptionIds": {
"filter": {
"terms": {
"exceptionId": [
"1"
],
"boost": 1
}
},
"aggregations": {
"by_exceptionId": {
"terms": {
"field": "recommendations.exceptionId.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_item": {
"terms": {
"field": "recommendations.item.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_destination": {
"terms": {
"field": "recommendations.location.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_trans": {
"terms": {
"field": "recommendations.transportMode.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_sourcelocation": {
"terms": {
"field": "recommendations.sourceLocation.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_shipdate": {
"terms": {
"field": "recommendations.shipDate.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_arrival": {
"terms": {
"field": "recommendations.arrivalDate.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"quantity": {
"sum": {
"field": "recommendations.stockTransfer.transferQuantity"
}
},
"transfercost": {
"sum": {
"field": "recommendations.stockTransfer.transferCost"
}
},
"revenueRecovered": {
"sum": {
"field": "recommendations.stockTransfer.revenueRecovered"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
回应:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "testing",
"_type": "recommendations",
"_id": "1",
"_score": 1.0,
"_source": {
"recommendations": [
{
"id": "578bd845-3ca9-495f-bc13-dbcc48e8f415",
"tenantId": "0d474f0a-f060-4828-b2ec-a81c4b27fa2e",
"clusterId": "1",
"eventId": "1",
"exceptionId": "1",
"eventType": "Delayed",
"item": "Item1",
"location": "DC1",
"dueDate": "2019-01-10T05:30:00.000+0530",
"exceptionQuantity": 100,
"recommendationType": "stockTransfer",
"customerName": "Walmart",
"primaryRecommendation": true,
"priority": 1,
"sourceLocation": "DC2",
"transferQuantity": 100,
"shipDate": "2019-01-09T05:30:00.000+0530",
"arrivalDate": "2019-01-10T05:30:00.000+0530",
"transportMode": "Road",
"transferCost": 500,
"maxQtyAvailableForTransfer": 100,
"totalQtyAtSource": 100,
"operation": "Road-Item1-from-DC2-to-DC1",
"peggedStockDemandId": "1",
"revenueRecovered": "20000.0"
},
{
"id": "578bd845-3ca9-495f-bc13-dbcc48e8f415",
"tenantId": "0d474f0a-f060-4828-b2ec-a81c4b27fa2e",
"clusterId": "1",
"eventId": "1",
"exceptionId": "2",
"eventType": "Delayed",
"item": "Item1",
"location": "DC1",
"dueDate": "2019-01-10T05:30:00.000+0530",
"exceptionQuantity": 100,
"recommendationType": "stockTransfer",
"customerName": "Walmart",
"primaryRecommendation": true,
"priority": 1,
"sourceLocation": "DC2",
"transferQuantity": 100,
"shipDate": "2019-01-09T05:30:00.000+0530",
"arrivalDate": "2019-01-10T05:30:00.000+0530",
"transportMode": "Road",
"transferCost": 500,
"maxQtyAvailableForTransfer": 100,
"totalQtyAtSource": 100,
"operation": "Road-Item1-from-DC2-to-DC1",
"peggedStockDemandId": "1",
"revenueRecovered": "20000.0"
}
]
}
}
]
},
"aggregations": {
"exceptionIds": {
"doc_count": 0,
"by_exceptionId": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": []
}
}
}
}
使用映射并创建"嵌套"类型的推荐。您需要在聚合中使用嵌套筛选器。使用嵌套类型,您可以将数组的每个对象视为一个单独的类型,否则它们将被展平。
示例。
文件1:"id":"1"建议":[{"eventId":"1","exceptionId":"1"},{"eventId":"2","exceptionId":"2"}]
文件2:
"id":"1"建议":[{"eventId":"2","exceptionId":"1"}]
考虑以上2个文件,并将推荐映射为对象。如果您将筛选eventId:2和exceptionId:1的文档,它将返回这两个文档,因为对象类型被展平,并且对象的字段之间的关系没有得到维护,它被视为单个文档。
当推荐映射为"嵌套类型"时,将返回文档2。即使您已经在查询中使用嵌套类型筛选了文档。在聚合中,它必须再次被使用,因为数组的所有对象都由查询返回。
映射
{
"index10" : {
"mappings" : {
"properties" : {
"recommendations" : {
"type" : "nested",
"properties" : {
"arrivalDate" : {
"type" : "date"
},
"clusterId" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"customerName" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"dueDate" : {
"type" : "date"
},
"eventId" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"eventType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"exceptionId" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"exceptionQuantity" : {
"type" : "long"
},
"id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"item" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"location" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"maxQtyAvailableForTransfer" : {
"type" : "long"
},
"operation" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"peggedStockDemandId" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"primaryRecommendation" : {
"type" : "boolean"
},
"priority" : {
"type" : "long"
},
"recommendationType" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"revenueRecovered" : {
"type" : "float"
},
"shipDate" : {
"type" : "date"
},
"sourceLocation" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"tenantId" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"totalQtyAtSource" : {
"type" : "long"
},
"transferCost" : {
"type" : "long"
},
"transferQuantity" : {
"type" : "long"
},
"transportMode" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
}
}
查询:
{
"size": 0, --> hits will not be returned
"aggregations": {
"exceptionIds": {
"nested": {
"path": "recommendations" --> nested agrregation on nested
},
"aggs": {
"recomm": {
"filter": {
"terms": {
"recommendations.exceptionId.keyword": [ --> filter on exceptionId
"1"
]
}
},
"aggs": {
"revenueRecovered": {
"sum": {
"field": "recommendations.revenueRecovered"
}
},
"transfercost": {
"sum": {
"field": "recommendations.transferCost"
}
},
"transferQuantity": {
"sum": {
"field": "recommendations.transferQuantity"
}
}
}
}
}
}
}
}
结果:
"aggregations" : {
"exceptionIds" : {
"doc_count" : 2,
"recomm" : {
"doc_count" : 1,
"transferQuantity" : {
"value" : 100.0
},
"transfercost" : {
"value" : 500.0
},
"revenueRecovered" : {
"value" : 20000.0
}
}
}
}