基于嵌套值的Elasticsearch文档排序

时间:2019-11-14 12:39:20

标签: sorting elasticsearch elasticsearch-aggregation elasticsearch-painless

我在Elasticsearch 7.4版中索引了以下数据。

[
  {
    "users": [
      {
        "age": "31",
        "dept": "IT",
        "city": "Chennai"
      },
      {
        "age": "25",
        "dept": "Accounts",
        "city": "Bengaluru"
      },
      {
        "age": "26",
        "dept": "Finance",
        "city": "Kochi"
      }
    ],
    "isActive": true
  },
  {
    "users": [
      {
        "age": "23",
        "dept": "IT",
        "city": "Chennai"
      },
      {
        "age": "22",
        "dept": "Finance",
        "city": "Kochi"
      },
      {
        "age": "21",
        "dept": "Accounts",
        "city": "Bengaluru"
      }
    ],
    "isActive": true
  },
  {
    "users": [
      {
        "age": "28",
        "dept": "IT",
        "city": "Chennai"
      },
      {
        "age": "22",
        "dept": "Finance",
        "city": "Kochi"
      },
      {
        "age": "21",
        "dept": "Accounts",
        "city": "Bengaluru"
      }
    ],
    "isActive": true
  },
  {
    "users": [
      {
        "age": "31",
        "dept": "IT",
        "city": "Kochi"
      },
      {
        "age": "25",
        "dept": "Finance",
        "city": "Bengaluru"
      },
      {
        "age": "26",
        "dept": "Accounts",
        "city": "Chennai"
      }
    ],
    "isActive": true
  },
  {
    "users": [
      {
        "age": "21",
        "dept": "IT",
        "city": "Kochi"
      },
      {
        "age": "45",
        "dept": "Accounts",
        "city": "Bengaluru"
      },
      {
        "age": "26",
        "dept": "Finance",
        "city": "Chennai"
      }
    ],
    "isActive": false
  }
]

用户是嵌套类型

我要根据以下条件对文档进行排序

isActive == true && users.dept == "IT" && users.city == "Chennai" with the age sorts in ascending

我期待以下结果

[
  {
    "users": [
      {
        "age": "23",
        "dept": "IT",
        "city": "Chennai"
      },
      {
        "age": "22",
        "dept": "Finance",
        "city": "Kochi"
      },
      {
        "age": "21",
        "dept": "Accounts",
        "city": "Bengaluru"
      }
    ],
    "isActive": true
  },
  {
    "users": [
      {
        "age": "28",
        "dept": "IT",
        "city": "Chennai"
      },
      {
        "age": "22",
        "dept": "Finance",
        "city": "Kochi"
      },
      {
        "age": "21",
        "dept": "Accounts",
        "city": "Bengaluru"
      }
    ],
    "isActive": true
  },
  {
    "users": [
      {
        "age": "31",
        "dept": "IT",
        "city": "Chennai"
      },
      {
        "age": "25",
        "dept": "Accounts",
        "city": "Bengaluru"
      },
      {
        "age": "26",
        "dept": "Finance",
        "city": "Kochi"
      }
    ],
    "isActive": true
  },
  {
    "users": [
      {
        "age": "31",
        "dept": "IT",
        "city": "Kochi"
      },
      {
        "age": "25",
        "dept": "Finance",
        "city": "Bengaluru"
      },
      {
        "age": "26",
        "dept": "Accounts",
        "city": "Chennai"
      }
    ],
    "isActive": true
  }
]

我尝试使用基于脚本的排序,但无法获得结果。 请帮我解决这个问题。

谢谢。

0 个答案:

没有答案