在弹性搜索查询中获取无效的数字异常

时间:2017-07-18 20:32:25

标签: java elasticsearch

我正在执行此查询:

{
  "query" : {
    "match" : {
      "studyID" : {
        "query" : 1,
        "type" : "boolean"
      }
    }
  },
  "aggregations" : {
    "25-34" : {
      "date_range" : {
        "field" : "timestamp",
        "ranges" : [ {
          "from" : "1992",
          "to" : "1983"
        } ],
        "format" : "yyyy"
      },
      "aggregations" : {
        "activityType" : {
          "terms" : {
            "field" : "activityType"
          }
        }
      }
    },
    "84-*" : {
      "date_range" : {
        "field" : "timestamp",
        "ranges" : [ {
          "from" : "1933"
        } ],
        "format" : "yyyy"
      },
      "aggregations" : {
        "activityType" : {
          "terms" : {
            "field" : "activityType"
          }
        }
      }
    },
    "18-24" : {
      "date_range" : {
        "field" : "timestamp",
        "ranges" : [ {
          "from" : "1999",
          "to" : "1993"
        } ],
        "format" : "yyyy"
      },
      "aggregations" : {
        "activityType" : {
          "terms" : {
            "field" : "activityType"
          }
        }
      }
    },
    "75-84" : {
      "date_range" : {
        "field" : "timestamp",
        "ranges" : [ {
          "from" : "1942",
          "to" : "1933"
        } ],
        "format" : "yyyy"
      },
      "aggregations" : {
        "activityType" : {
          "terms" : {
            "field" : "activityType"
          }
        }
      }
    },
    "0-17" : {
      "date_range" : {
        "field" : "timestamp",
        "ranges" : [ {
          "from" : "2017",
          "to" : "2000"
        } ],
        "format" : "yyyy"
      },
      "aggregations" : {
        "activityType" : {
          "terms" : {
            "field" : "activityType"
          }
        }
      }
    },
    "55-64" : {
      "date_range" : {
        "field" : "timestamp",
        "ranges" : [ {
          "from" : "1962",
          "to" : "1953"
        } ],
        "format" : "yyyy"
      },
      "aggregations" : {
        "activityType" : {
          "terms" : {
            "field" : "activityType"
          }
        }
      }
    },
    "65-74" : {
      "date_range" : {
        "field" : "timestamp",
        "ranges" : [ {
          "from" : "1952",
          "to" : "1943"
        } ],
        "format" : "yyyy"
      },
      "aggregations" : {
        "activityType" : {
          "terms" : {
            "field" : "activityType"
          }
        }
      }
    },
    "35-44" : {
      "date_range" : {
        "field" : "timestamp",
        "ranges" : [ {
          "from" : "1982",
          "to" : "1973"
        } ],
        "format" : "yyyy"
      },
      "aggregations" : {
        "activityType" : {
          "terms" : {
            "field" : "activityType"
          }
        }
      }
    },
    "45-54" : {
      "date_range" : {
        "field" : "timestamp",
        "ranges" : [ {
          "from" : "1972",
          "to" : "1963"
        } ],
        "format" : "yyyy"
      },
      "aggregations" : {
        "activityType" : {
          "terms" : {
            "field" : "activityType"
          }
        }
      }
    }
  }
}

我只想根据日期范围聚合活动,然后按活动类型对这些范围进行子聚合,但弹性搜索给我这个例外:

{
    "error": {
        "root_cause": [
            {
                "type": "aggregation_execution_exception",
                "reason": "Invalid number format [yyyy#]"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "study",
                "node": "MWkXAAOCSYuM-ubdkulNnw",
                "reason": {
                    "type": "aggregation_execution_exception",
                    "reason": "Invalid number format [yyyy#]"
                }
            }
        ]
    },
    "status": 500
}

任何想法我错过了什么?

1 个答案:

答案 0 :(得分:0)

format参数用于指定响应中应返回日期的格式,而不是请求中指定的格式。因此,根据您在映射类型中指定的timestamp字段的日期格式,您的请求需要包含完整的日期,如下所示:

"25-34" : {
  "date_range" : {
    "field" : "timestamp",
    "ranges" : [ {
      "from" : "1992-01-01T00:00:00.000Z",
      "to" : "1983-12-31T23:59.59.999Z"
    } ],
    "format" : "yyyy"
  },
  "aggregations" : {
    "activityType" : {
      "terms" : {
        "field" : "activityType"
      }
    }
  }
},