1

集計を計算したい製品のカタログがあります。問題は、ネストされたフィールドと親フィールドの両方を含むフィルターを使用してネストされた集計を実行しようとすると発生します。カウントが間違っているか、ヒット数が 0 です。これが私の製品オブジェクト マッピングのサンプルです。

"Products": {
        "properties": {
           "ProductID": {
              "type": "long"
           },
           "ProductType": {
              "type": "long"
           },
           "ProductName": {
              "type": "string",
              "fields": {
                 "raw": {
                    "type": "string",
                    "index": "not_analyzed"
                 }
              }
           },
           "Prices": {
              "type": "nested",
              "properties": {
                 "CurrencyType": {
                    "type": "integer"
                 },
                 "Cost": {
                    "type": "double"                        
                 }
              }
          } 
      }
  }

エラスティックで複製しようとしている SQL クエリの例を次に示します。

SELECT PRODPR.Cost AS PRODPR_Cost 
    ,COUNT(PROD.ProdcutID) AS PROD_ProductID_Count
FROM Products PROD WITH (NOLOCK)
LEFT OUTER JOIN Prices PRODPR WITH (NOLOCK) ON (PRODPR.objectid = PROD.objectid)
WHERE PRODPR.CurrencyType = 4
    AND PROD.ProductType IN (
        11273
        ,11293
        ,11294
        )
GROUP BY PRODPR.Cost

私が思いついたElastic Searchクエリ:最初のもの(次のクエリは、フィルターとしてCurrencyTypeだけで正しいカウントを返しますが、ProductTypeフィルターを追加すると、間違ったカウントが返されます)

GET /IndexName/Products/_search
{
  "aggs": {
    "price_agg": {
      "filter": {
        "bool": {
          **"must": [
            {
              "nested": {
                "path": "Prices",
                    "filter": {
                      "term": {
                        "Prices.CurrencyType": "8"
                      }
                }
              }
            },
            {
              "terms": {
                "ProductType": [
                  "11273",
                  "11293",
                  "11294"
                ]
              }
            }
          ]**
        }
      },
      "aggs": {
        "price_nested_agg": {
          "nested": {
            "path": "Prices"
          },
          "aggs": {
            "59316518_group_agg": {
              "terms": {
                "field": "Prices.Cost",
                "size": 0
              },
              "aggs": {
                "product_count": {
                    "reverse_nested": { },
                    "aggs": {
                        "ProductID_count_agg": {
                            "value_count": {
                                "field": "ProductID"
                            }
                        }
                    }
                }
              }
            }
          }
        }
      }
    }
  },
  "size": 0
}

2 つ目 (次のクエリは、フィルターとして CurrencyType のみを使用して正しいカウントを返しますが、ProductType フィルターを追加すると、0 ヒットになります):

GET /IndexName/Prodcuts/_search
{
  "aggs": {
    "price_agg": {
      "nested": {
        "path": "Prices"
      },
      "aggs": {
        "currency_filter": {
          "filter": {
              "bool": {
                  "must": [
                     {
                         "term": {
                            "Prices.CurrrencyType": "4"
                         }
                     },
                     {
                         "terms": {
                            "ProductType": [
                               "11273",
                               "11293"
                            ]
                         }
                     }
                  ]
              }
          },
          "aggs": {
            "59316518_group_agg": {
              "terms": {
                "field": "Prices.Cost",
                "size": 0
              },
              "aggs": {
                "product_count": {
                  "reverse_nested": {},
                  "aggs": {
                    "ProductID_count_agg": {
                      "value_count": {
                        "field": "ProductID"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "size": 0
}

さらにいくつかのクエリを試しましたが、上記の 2 つは私が思いついた最も近いものです。このユースケースに遭遇した人はいますか?私は何を間違っていますか?どんな助けでも大歓迎です。ありがとう!

4

0 に答える 0