# Numeric が出るはずのbuckets\_pathにおけるgot: \[Object\[\]\]エラーについて

**URL:** <https://discuss.elastic.co/t/numeric-buckets-path-got-object/233699>\
**Category:** 日本語による質問・議論はこちら\
**Created:** [May 21, 2020, 8:58am UTC](https://discuss.elastic.co/t/numeric-buckets-path-got-object/233699 "2020-05-21T08:58:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![111341](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111341/32/68802_2.png) [@111341](https://discuss.elastic.co/u/111341)\
**Post date:** [May 21, 2020, 8:58am UTC](https://discuss.elastic.co/t/numeric-buckets-path-got-object/233699/1 "2020-05-21T08:58:28Z")

</div>

お世話になります。質問させていただきます。  
現在、aggsで出力された中央値を条件に使おうとしています。

サンプルクエリでは、中央値の出力までは出来ていますが、計算に使おうとすると、

```auto
"type": "aggregation_execution_exception",
      "reason": "buckets_path must reference either a number value or a single value numeric metric aggregation, got: [Object[]] at aggregation [10day]"

```

となります。  
percentilesではなく、sumも試してみましたが、このエラーメッセージに変化はありませんでした。

そこで  
・このエラーはなぜ起きているのか  
・Objectが何なのか調べる方法はあるか  
という質問となります。  
ご協力いただけますと幸いです。

request

```auto
{
  "size": 0,
  "aggs": {
    "category": {
      "terms": {
        "field": "category_id",
        "size": 1
      },
      "aggs": {
        "10day": {
          "date_range": {
            "field": "end_at",
            "format": "yyyy-MM-dd HH:mm:ss",
            "ranges": [
              {
                "from": "now-10d",
                "to": "now"
              }
            ]
          },
          "aggs": {
            "median_price": {
              "percentiles": {
                "field": "end_price",
                "percents": [
                  50
                ]
              }
            }
          }
        },
        "30day": {
          "date_range": {
            "field": "end_at",
            "format": "yyyy-MM-dd HH:mm:ss",
            "ranges": [
              {
                "from": "now-30d",
                "to": "now"
              }
            ]
          },
          "aggs": {
            "median_price": {
              "percentiles": {
                "field": "end_price",
                "percents": [
                  50
                ]
              }
            }
          }
        },
        "calculation": {
          "bucket_script": {
            "buckets_path": {
              "a": "10day>median_price[50.0]",
              "b": "30day>median_price[50.0]"
            },
            "script": "params.a / params.b"
          }
        },
        "return_filter": {
          "bucket_selector": {
            "buckets_path": {
              "increase_ratio": "calculation"
            },
            "script": "params.increase_ratio <= 1.3"
          }
        }
      }
    }
  }
}

```

response

```auto
{
  "error": {
    "root_cause": [],
    "type": "search_phase_execution_exception",
    "reason": "",
    "phase": "fetch",
    "grouped": true,
    "failed_shards": [],
    "caused_by": {
      "type": "aggregation_execution_exception",
      "reason": "buckets_path must reference either a number value or a single value numeric metric aggregation, got: [Object[]] at aggregation [10day]"
    }
  },
  "status": 500
}

```

中央値の例

```auto
{
  "took" : 10180,
  "timed_out" : false,
  "_shards" : {
    "total" : 40,
    "successful" : 40,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "category" : {
      "doc_count_error_upper_bound" : 451394,
      "sum_other_doc_count" : 197182706,
      "buckets" : [
        {
          "key" : 20068,
          "doc_count" : 950429,
          "10day" : {
            "buckets" : [
              {
                "key" : "2020-05-11 08:36:13-2020-05-21 08:36:13",
                "from" : 1.589186173556E12,
                "from_as_string" : "2020-05-11 08:36:13",
                "to" : 1.590050173556E12,
                "to_as_string" : "2020-05-21 08:36:13",
                "doc_count" : 19633,
                "median_price" : {
                  "values" : {
                    "50.0" : 800.0
                  }
                }
              }
            ]
          },
          "30day" : {
            "buckets" : [
              {
                "key" : "2020-04-21 08:36:13-2020-05-21 08:36:13",
                "from" : 1.587458173556E12,
                "from_as_string" : "2020-04-21 08:36:13",
                "to" : 1.590050173556E12,
                "to_as_string" : "2020-05-21 08:36:13",
                "doc_count" : 73913,
                "median_price" : {
                  "values" : {
                    "50.0" : 800.0
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [May 21, 2020, 12:25pm UTC](https://discuss.elastic.co/t/numeric-buckets-path-got-object/233699/2 "2020-05-21T12:25:55Z")

</div>

少しややこしいですよね、ログ。。。

> [@111341](#):
>
> "type": "aggregation\_execution\_exception", "reason": "buckets\_path must reference either a number value or a single value numeric metric aggregation, got: [Object] at aggregation [10day]"

このエラーメッセージは`「10day」`のところが配列で返ってきてる(`got: [Object[]] at aggregation [10day]`)ので、`buckets_path`どれを指しているのかがわからないので、1つの値を取るように指定してくれということです。

実際、`data_range`の`ranges`が配列なので、複数返る可能性があるaggregationになっています。

解決策としては、`10day`のところのどの値なのかを知らせてやれば解決します。

解決案としては2つあって、

1. `date_range`のバケットに名前をつけて、名前で`buckets_path`の指定を行う
2. `date_range`の代わりに`filter`+`range`クエリで1つのbucketが返るようにする

って感じでしょうか。

1の名前を付ける方法は[Keyed Response](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html#_keyed_response_4)です。keyを指定しない場合は、日付の文字列が返ってきてしまいますが、動かしている時間によって、文字が変わってしまいます。なので、`to`の行の後に、`"key": "hoge"`のように戻ってくるバケットに名前をつけて、`buckets_path`で`10day["hoge"]`のように指定する方法です。

2の案は[filter aggregation](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html)あたりを参考にやってみてください。

---

<div class="post-metadata">

**Author:** ![111341](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111341/32/68802_2.png) [@111341](https://discuss.elastic.co/u/111341)\
**Post date:** [May 21, 2020, 2:37pm UTC](https://discuss.elastic.co/t/numeric-buckets-path-got-object/233699/3 "2020-05-21T14:37:47Z")

</div>

素早いお返事、ありがとうございます。  
ご指摘の改善点で問題が解決しました。そういうことなのですね。

現状、1.3の変動を持つものを抽出する  
`return_filter`  
が動作していませんが、  
もしよろしければこちらも動作させるのに参考のページはありますでしょうか？

```auto
{
  "size": 0,
  "aggs": {
    "category": {
      "terms": {
        "field": "category_id",
        "size": 1
      },
      "aggs": {
        "10day": {
          "filter": {
            "range": {
              "end_at": {
                "format": "yyyy-MM-dd HH:mm:ss",
                "gte": "now-10d/d",
                "lte": "now/d"
              }
            }
          },
          "aggs": {
            "median_price": {
              "percentiles": {
                "field": "end_price",
                "percents": [
                  50
                ]
              }
            }
          }
        },
        "30day": {
          "filter": {
            "range": {
              "end_at": {
                "format": "yyyy-MM-dd HH:mm:ss",
                "gte": "now-30d/d",
                "lte": "now/d"
              }
            }
          },
          "aggs": {
            "median_price": {
              "percentiles": {
                "field": "end_price",
                "percents": [
                  50
                ]
              }
            }
          }
        },
        "calculation": {
          "bucket_script": {
            "buckets_path": {
              "a": "10day>median_price[50.0]",
              "b": "30day>median_price[50.0]"
            },
            "script": "params.a / params.b"
          }
        },
        "return_filter": {
          "bucket_selector": {
            "buckets_path": {
              "increase_ratio": "calculation"
            },
            "script": "params.increase_ratio <= 1.3"
          }
        }
      }
    }
  }
}

```

```auto
{
  "took" : 6678,
  "timed_out" : false,
  "_shards" : {
    "total" : 40,
    "successful" : 40,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : []
  },
  "aggregations" : {
    "category" : {
      "meta" : { },
      "doc_count_error_upper_bound" : 451718,
      "sum_other_doc_count" : 197354248,
      "buckets" : [
        {
          "key" : 20068,
          "doc_count" : 951932,
          "10day" : {
            "meta" : { },
            "doc_count" : 21364,
            "median_price" : {
              "values" : {
                "50.0" : 800.0
              }
            }
          },
          "30day" : {
            "meta" : { },
            "doc_count" : 75620,
            "median_price" : {
              "values" : {
                "50.0" : 800.0
              }
            }
          },
          "calculation" : {
            "value" : 1.0
          }
        }
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [May 21, 2020, 3:33pm UTC](https://discuss.elastic.co/t/numeric-buckets-path-got-object/233699/4 "2020-05-21T15:33:02Z")

</div>

1.3以上のものを返したいのであれば、条件が逆向きですね。  
参考 : [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-selector-aggregation.html)

---

<div class="post-metadata">

**Author:** ![111341](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111341/32/68802_2.png) [@111341](https://discuss.elastic.co/u/111341)\
**Post date:** [May 22, 2020, 1:26am UTC](https://discuss.elastic.co/t/numeric-buckets-path-got-object/233699/5 "2020-05-22T01:26:20Z")

</div>

すいません、ありがとうございます。  
トライアンドエラーの一つのコードを張り、大変間の抜けた質問になってしまいました。  
実際お尋ねしたかったのは  
numeric的なものが返ってくるはずが、返ってくるはずがない(null)ような値が返ってきて起こした話で、以下のようになります。  
こちらも参考になるドキュメントの箇所などお教えいただけますと幸いです。

大部分は同一で、sizeを2にしたものを実行すると・・・

```auto
{
  "size": 0,
  "aggs": {
    "category": {
      "terms": {
        "field": "category_id", //int等ではなく、文字列です
        "size": 2
      },

```

中央値でnullが返ってくるkey:23316が取得できました。  
これだけですと期間内にデータが無いのかなと思ってしまいますが・・・

```auto
        ...
        {
          "key" : 23316,
          "doc_count" : 815619,
          "10day" : {
            "meta" : { },
            "doc_count" : 0,
            "median_price" : {
              "values" : {
                "50.0" : null
              }
            }
          },
          "30day" : {
            "meta" : { },
            "doc_count" : 0,
            "median_price" : {
              "values" : {
                "50.0" : null
              }
            }
          }
        }

```

23316を指定したbool mustを追加してやると・・・

```auto
"query" : {
    "bool":{
      "must":[
        {
          "match" : 
          {
            "category_id" : "23316"
          }
        }
      ]
    }
  },
  "aggs": {
    "category": {
      "terms": {
        "field": "category_id",
        "size": 2
      },

```

ちゃんと、中央値が出てきます。

```auto
{
          "key" : 23316,
          "doc_count" : 842259,
          "10day" : {
            "meta" : { },
            "doc_count" : 4099,
            "median_price" : {
              "values" : {
                "50.0" : 1199.2535940195514
              }
            }
          },
          "30day" : {
            "meta" : { },
            "doc_count" : 16233,
            "median_price" : {
              "values" : {
                "50.0" : 1301.5371428571432
              }
            }
          },
          "calculation" : {
            "value" : 0.9214132693800361
          }
        }

```

termsの理解が出来ていないのだとは思いますが  
イメージと異なる動作で戸惑っております・・・

---

<div class="post-metadata">

**Author:** ![111341](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111341/32/68802_2.png) [@111341](https://discuss.elastic.co/u/111341)\
**Post date:** [May 22, 2020, 1:34am UTC](https://discuss.elastic.co/t/numeric-buckets-path-got-object/233699/6 "2020-05-22T01:34:25Z")

</div>

追記です。怪しいのはこのあたりかなとは思います。

中央値が出ないもの

```auto
      "doc_count_error_upper_bound" : 422594,
      "sum_other_doc_count" : 196593107,

```

中央値が出るもの

```auto
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,

```

---

<div class="post-metadata">

**Author:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [May 22, 2020, 1:58am UTC](https://discuss.elastic.co/t/numeric-buckets-path-got-object/233699/7 "2020-05-22T01:58:06Z")

</div>

> [@111341](#):
>
> `doc_count_error_upper_bound`

このスレッドが参考になるかもしれないです。

> [@Aggregations sum の doc\_count\_error\_upper\_bound について](https://discuss.elastic.co/t/aggregations-sum-doc-count-error-upper-bound/156222):
>
> Aggregationsでtermsでグループ化した上でsumを使用するとデータ数によりdoc\_count\_error\_upper\_boundが0件にならず正常にsumの値を取得できない場合があります。 "terms": {"field": "category.value", "size": 200} sizeを指定すれば取得できるようになりますが、データ数によっては処理に時間がかかったり指定したsize以上のデータが存在する可能性もあります。 確実にdoc\_count\_error\_upper\_boundを0に抑える方法はあるのでしょうか？

---

<div class="post-metadata">

**Author:** ![111341](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/111341/32/68802_2.png) [@111341](https://discuss.elastic.co/u/111341)\
**Post date:** [May 22, 2020, 7:19am UTC](https://discuss.elastic.co/t/numeric-buckets-path-got-object/233699/8 "2020-05-22T07:19:39Z")

</div>

無事意図したものが返ってきました。

また落ち着いたらどこかでお会いさせてください。  
どうもありがとうございました。

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 19, 2020, 7:19am UTC](https://discuss.elastic.co/t/numeric-buckets-path-got-object/233699/9 "2020-06-19T07:19:43Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
