简体   繁体   English

Elasticsearch查询按唯一字段过滤

[英]Elasticsearch query filter by unique field

Using Elasticsearch, I have employees mapped, each employee belonging to some team. 使用Elasticsearch,我可以映射员工,每个员工都属于某个团队。 Every team should have a team leader and I need to validate that. 每个团队都应该有一个团队负责人,我需要对此进行验证。 Is there a way to search for every team on its own, but in one query, to verify it has an employee with the field "is_leader": true? 有没有一种方法可以自行搜索每个团队,但是在一个查询中,以确认它是否具有字段为“ is_leader”的员工:true? I would like to get results of all teams lacking a team leader. 我想获得所有缺少团队领导者的团队的成果。

{
{
            "_type": "employee",
            "name": "Jade",
            "team": "A",
            "is_leader": false,
},
{
            "_type": "employee",
            "name": "Jack",
            "team": "A",
            "is_leader": false,
},
{           "_type": "employee",
            "name": "Sarah",
            "team": "A",
            "is_leader": true,
},
{           "_type": "employee",
            "name": "Jim",
            "team": "B",
            "is_leader": false,
},
{           "_type": "employee",
            "name": "Don",
            "team": "B",
            "is_leader": false,
},
{           "_type": "employee",
            "name": "Jess",
            "team": "B",
            "is_leader": false,
},
}

You can use a terms aggregation on the team field to separate all teams, and then use another terms sub-aggregation on the is_leader field. 您可以在team字段上使用terms汇总来分隔所有团队,然后在is_leader字段上使用另一个terms子汇总。 That way you'll get the count for leader vs not-leader for each team. 这样,您将获得每个团队的领导者与非领导者的计数。 The teams with 0 leader will stand out: 领导者为0的团队将脱颖而出:

{
  "size": 0,
  "aggs": {
    "teams": {
      "terms": {
        "field": "team"
      },
      "aggs": {
        "leaders": {
          "terms": {
            "field": "is_leader"
          }
        }
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM