Elastic Search 6嵌套查询聚合 [英] Elastic Search 6 Nested Query Aggregations

查看:83
本文介绍了Elastic Search 6嵌套查询聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是弹性搜索查询和聚合的新手.我有一个具有以下映射的嵌套文档

  PUT/公司{映射":{`数据":{特性": {"deptId":{"type":关键字"},"deptName":{"type":关键字"},员工": {"type":嵌套",特性": {"empId":{"type":关键字"},"empName":{"type":文字"},薪水": {"type":"float"}}}}}}} 

我已按如下所示插入示例数据

  PUT公司/数据/1{"deptId":"1","deptName":"HR",员工": [{"empId":"1","empName":"John",工资":"1000"},{"empId":"2","empName":将",薪水":"2000"}]}PUT公司/数据/3{"deptId":"3","deptName":"FINANCE",员工": [{"empId":"1","empName":"John",工资":"1000"},{"empId":"2","empName":将",薪水":"2000"},{"empId":"3","empName":标记",薪水":"4000"}]} 

如何为以下内容构建查询DSL

  1. 员工人数最多的部门
  2. 大多数部门的员工

我正在使用Elastic Search 6.2.4

解决方案

您的第一个问题答案在此链接中

这回答了您的第二个问题,同时还在阅读附件中的链接.

  GET/my_index/blogpost/_search{大小":0,"aggs":{员工": {嵌套":{路径":雇员"},"aggs":{按名字": {条款":{字段":"employee.empName"}}}}}} 

阅读嵌套的Agg

我希望这能为您提供所需的东西.

i am new to elastic search Query and aggregation. I have a nested document with the following mapping

PUT /company
{
 "mappings": {
 `"data": {
  "properties": {
    "deptId": {
      "type": "keyword"
    },
     "deptName": {
      "type": "keyword"
    },
    "employee": {
      "type": "nested",
      "properties": {
        "empId": {
          "type": "keyword"
        },
        "empName": {
          "type": "text"
        },
        "salary": {
          "type": "float"
        }
       }}}}}}

I have inserted Sample Data as follows

PUT company/data/1
{
"deptId":"1",
"deptName":"HR",
 "employee": [
  {
    "empId": "1",
    "empName": "John",
    "salary":"1000"
  },
   {
    "empId": "2",
    "empName": "Will",
    "salary":"2000"
  }
 ]}

PUT company/data/3
{
  "deptId":"3",
  "deptName":"FINANCE",
   "employee": [
      {
        "empId": "1",
        "empName": "John",
        "salary":"1000"
      },
       {
        "empId": "2",
        "empName": "Will",
        "salary":"2000"
      },
       {
        "empId": "3",
        "empName": "Mark",
        "salary":"4000"
      }]
     }

How can i Construct a Query DSL for the following

  1. Department with the maximum Employees
  2. Employee that is present in most departments

I am using Elastic Search 6.2.4

解决方案

Your First Questions answer is in this link nested inner doc count Which Stats

POST test/_search
{
  "query": {
    "nested": {
      "path": "employee",
      "inner_hits": {} 
    }
  }
}

This Answers your Second Question there is also reading the link attached.

GET /my_index/blogpost/_search
{
  "size" : 0,
  "aggs": {
    "employee": { 
      "nested": {
        "path": "employee"
      },
      "aggs": {
        "by_name": {
          "terms": { 
            "field":    "employee.empName"
          }
        }
      }
    }
  }
}

Read Nested Agg

I hope this gives you what you need.

这篇关于Elastic Search 6嵌套查询聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆