弹性搜索查询过滤器和聚合不起作用

Elastic search query filter and aggregation is not working

当我执行查询时,我有以下文档和搜索查询弹性正在获取不匹配的文档场景是针对匹配的 exceptionId 它应该进行聚合但它正在获取所有文档并且聚合也不起作用。

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": []
            }
        }
    }
}

使用您的映射并创建 "nested" 类型的推荐。您需要在聚合中使用嵌套过滤器。使用嵌套类型,您可以将数组的每个对象视为一个单独的类型,否则它们将被展平。

示例。

文档 1: "id":“1” "recommendations":[ { "eventId": "1", "exceptionId":“1” }, { "eventId": "2", "exceptionId":“2” } ]

文档 2:

"id":"1" "recommendations":[ { "eventId": "2", "exceptionId":“1” } ]

考虑以上 2 个将推荐映射为对象的文档。如果您将过滤具有 eventId:2 和 exceptionId:1 的文档,它将 return 两个文档作为对象类型被展平并且对象字段之间的关系不被维护,它被认为是单个文档。

当推荐被映射为“嵌套类型”时,文档 2 将被 returned。 即使您在查询中使用嵌套类型过滤了文档。在聚合中,必须再次使用它,因为数组的所有对象都由查询return编辑。

映射

{
  "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
        }
      }
    }
  }