Elasti c фильтр поисковых запросов и агрегация не работает - PullRequest
0 голосов
/ 03 мая 2020

У меня есть приведенный ниже документ и поисковый запрос, когда я выполняю запрос. Elasti c извлекает сценарий с несоответствующими документами для сопоставленного исключения. Он должен выполнять агрегацию, но он выбирает все документы, а также агрегация не работает.

1) item

2) location

3) transportMode

4) sourceLocation

5) shipDate

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 Ответ

1 голос
/ 03 мая 2020

Используя ваше отображение и создавая рекомендацию типа "вложенный". Вам нужно использовать вложенный фильтр в агрегации. Используя вложенный тип, вы можете рассматривать каждый объект массива как отдельный тип, в противном случае они будут выровнены.

Пример.

Do c 1: "id": "1" "рекомендации ": [{" eventId ":" 1 "," exceptionId ":" 1 "}, {" eventId ":" 2 "," exceptionId ":" 2 "}]

Do c 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
        }
      }
    }
  }
...