响应中未保留agg名称的排序

zpgglvta  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(274)

我正在执行一个弹性查询,并使用rest调用读取java代码中的响应。当我读取响应时,字段的顺序-200、204、4xx、5xx没有按照响应中的顺序返回。
找到下面的申请样本

GET appl-activity*/_search
{
  "size": 0,
  "aggs": {
    "group_by_daterange": {
      "range": {
        "field": "Date",
        "ranges": [
          {
            "from": "Fri Oct 23 02:54:26 2020 -0400",
            "to": "Mon Oct 26 05:54:26 2020 -0400"
          }
        ]
      },
      "aggs": {
        "byapplication": {
          "terms": {
            "field": "application.keyword",
            "size": 1000
          },
          "aggs": {
            "by200": {
              "sum": {
                "field": "200"
              }
            },
            "by204": {
              "sum": {
                "field": "204"
              }
            },
            "by4xx": {
              "sum": {
                "field": "4xx"
              }
            },
            "by5xx": {
              "sum": {
                "field": "5xx"
              }
            }
          }
        }
      }
    }
  }
}

返回的响应:-

{
  "took" : 35,
  "timed_out" : false,
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1173,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "group_by_daterange" : {
      "buckets" : [
        {
          "key" : "Fri Oct 23 06:54:26 2020 +0000-Mon Oct 26 09:54:26 2020 +0000",
          "from" : 1.603436066E12,
          "from_as_string" : "Fri Oct 23 06:54:26 2020 +0000",
          "to" : 1.603706066E12,
          "to_as_string" : "Mon Oct 26 09:54:26 2020 +0000",
          "doc_count" : 30,
          "byapplication" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "SITE",
                "doc_count" : 20,
                "by4xx" : {
                  "value" : 1.0
                },
                "by5xx" : {
                  "value" : 0.0
                },
                "by204" : {
                  "value" : 0.0
                },
                "by200" : {
                  "value" : 5342.0
                }
              },
              {
                "key" : "MOBILE",
                "doc_count" : 10,
                "by4xx" : {
                  "value" : 0.0
                },
                "by5xx" : {
                  "value" : 0.0
                },
                "by204" : {
                  "value" : 0.0
                },
                "by200" : {
                  "value" : 5635.0
                }
              }
            ]
          }
        }
      ]
    }
  }
}

我希望响应代码的顺序与请求中的顺序相同。请帮帮我??

{
  "key": "MOBILE",
  "doc_count": 10,
  "by200": {
    "value": 5635
  },
  "by204": {
    "value": 0
  },
  "by4xx": {
    "value": 0
  },
  "by5xx": {
    "value": 0
  }
}
tzdcorbm

tzdcorbm1#

与数组不同,json字典不保证任何顺序。这意味着没有任何规范会强制elasticsearch或任何其他json-in/json-out接口保留输入顺序。
不幸的是,有些系统(例如stripe)在返回客户机之前,会额外地按字母顺序对响应键进行排序。
话虽如此,我还是习惯于根据我想要的顺序,在agg key name前面加一个字母数字字符组合来解决这个问题:

{
  "1__myAggName": { ... },
  "2__myAggName": { ... },
   ...
}

然后在客户端,我会对agg键进行简单的排序,并去掉前缀。
但是现在我倾向于使用聚合元数据,在其中我将顺序和其他信息放在一起。这大大提高了我所有后期处理步骤的透明度和可读性:

{
  ...
  "byapplication": {
    "terms": {
      "field": "application.keyword",
      "size": 1000
    },
    "aggs": {
      "by200": {
        "meta": {       <--
          "index": 0
        },
        "sum": {
          "field": "200"
        }
      },
      "by204": {
        "meta": {      <--
          "index": 1
        },
        "sum": {
          "field": "204"
        }
      },
      "by4xx": {
        "meta": {
          "index": 2
        },
        "sum": {
          "field": "4xx"
        }
      },
      "by5xx": {
        "meta": {
          "index": 3
        },
        "sum": {
          "field": "5xx"
        }
      }
    }
  }
}

相关问题