Elasticsearch语法,依次执行筛选、聚合、聚合结果筛选和排序

mkshixfv  于 2023-01-25  发布在  ElasticSearch
关注(0)|答案(1)|浏览(205)

假设一个团队有很多成员,其中成员和团队都有关联的数据。
1.根据用户条件(例如,经验年限)筛选成员
1.然后将一些成员属性聚合到团队级别(例如,筛选成员的平均目标数)
1.然后对聚合进行筛选(例如,仅筛选成员的平均目标数〉= 17的团队)
1.然后对球队的任何属性进行排序(例如,获胜次数降序)。
在SQL术语中,这相当于WHERE、GROUP BY、HAVING、ORDER BY
如果有用的话,请看下面的样本数据。具体来说,这里有一个我想问的问题:

  • 我希望所有球队的平均进球数至少有E年的经验的球员是〉G,按球队的胜利数降序排列。*

注意:因为用户可以过滤任何内容,所以在索引中保存预先聚合的数据不是一个选项。我需要搜索语法,使我能够动态地执行此操作。

模式

PUT sample
{
  "mappings": {
    "dynamic": "strict",
    "properties": {
      "teamId": { "type": "keyword", "index": true, "doc_values": true },
      "teamAge": { "type": "integer", "index": true, "doc_values": true },
      "wins": { "type": "integer", "index": true, "doc_values": true },
      "losses": { "type": "integer", "index": true, "doc_values": true },
      "memberId": { "type": "keyword", "index": true, "doc_values": true },
      "exp": { "type": "float", "index": true, "doc_values": true},
      "age": { "type": "integer", "index": true, "doc_values": true},
      "height": { "type": "integer", "index": true, "doc_values": true},
      "weight": { "type": "integer", "index": true, "doc_values": true},
      "goals": { "type": "integer", "index": true, "doc_values": true},
      "code": { "type": "keyword", "index": true, "doc_values": true}
    }
  }
}

样品数据

PUT sample/_doc/1  
{ "teamId" : "A", "teamAge":7 , "wins": 10, "losses": 5, "memberId" : "A1" , "exp": "1" , "age" : "21" , "code": "X", "goals": 12, "height": 72, "weight": 170 } 
PUT sample/_doc/2  
{ "teamId" : "A", "teamAge":7 , "wins": 10, "losses": 5, "memberId" : "A2" , "exp": "2" , "age" : "31" , "code": "Y", "goals": 15, "height": 75, "weight": 190 } 
PUT sample/_doc/3  
{ "teamId" : "A", "teamAge":7 , "wins": 10, "losses": 5, "memberId" : "A3" , "exp": "3" , "age" : "41" , "code": "Z", "goals": 20, "height": 80, "weight": 210 } 
PUT sample/_doc/4  
{ "teamId" : "B", "teamAge":6 , "wins": 12, "losses": 3, "memberId" : "B1" , "exp": "1" , "age" : "22" , "code": "Z", "goals": 20, "height": 80, "weight": 220 } 
PUT sample/_doc/5  
{ "teamId" : "B", "teamAge":6 , "wins": 12, "losses": 3, "memberId" : "B2" , "exp": "2" , "age" : "32" , "code": "X", "goals": 18, "height": 78, "weight": 200 } 
PUT sample/_doc/6  
{ "teamId" : "B", "teamAge":6 , "wins": 12, "losses": 3, "memberId" : "B3" , "exp": "3" , "age" : "42" , "code": "X", "goals": 16, "height": 76, "weight": 185 } 
PUT sample/_doc/7  
{ "teamId" : "B", "teamAge":6 , "wins": 12, "losses": 3, "memberId" : "B4" , "exp": "4" , "age" : "52" , "code": "Y", "goals": 12, "height": 72, "weight": 170 } 
PUT sample/_doc/8  
{ "teamId" : "C", "teamAge":5 , "wins": 11, "losses": 4, "memberId" : "C1" , "exp": "1" , "age" : "23" , "code": "Y", "goals": 16, "height": 76, "weight": 190 } 
PUT sample/_doc/9  
{ "teamId" : "C", "teamAge":5 , "wins": 11, "losses": 4, "memberId" : "C2" , "exp": "2" , "age" : "33" , "code": "X", "goals": 18, "height": 78, "weight": 195 } 
PUT sample/_doc/10 
{ "teamId" : "C", "teamAge":5 , "wins": 11, "losses": 4, "memberId" : "C3" , "exp": "2" , "age" : "43" , "code": "Z", "goals": 19, "height": 79, "weight": 225 }
9rygscc1

9rygscc11#

The following query gets all the teams with team members with at least 1 year of experience with an average number of goals greater than 10 sorted by the team total of wins descending.

POST sample/_search
{
  "query": {
    "bool": {
      "filter": [
        {
          "range": {
            "exp": {
              "gt": 1
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "teams": {
      "terms": {
        "field": "teamId",
        "size": 100,
        "order": {
          "numberOfWins": "desc"
        }
      },
      "aggs": {
        "numberOfWins": {
          "max": {
            "field": "wins"
          }
        },
        "averageGoals": {
          "avg": {
            "field": "goals"
          }
        },
        "averageGoals_filter": {
          "bucket_selector": {
            "buckets_path": {
              "total": "averageGoals"
            },
            "script": "params.total > 10"
          }
        }
      }
    }
  },
  "size": 0
}

相关问题