elasticsearch - 如何在Elasticsearch中获得嵌套字段的不同值?

标签 elasticsearch nested kibana elasticsearch-dsl

我在elasticsearch中具有以下文档结构:

   root
|-- userid: string (nullable = true)
|-- name: string (nullable = true)
|-- applications: array (nullable = true)
|    |-- element: struct (containsNull = true)
|    |    |-- applicationid: string (nullable = true)
|    |    |-- createdat: string (nullable = true)
|    |    |-- source_name: string (nullable = true)
|    |    |-- accounts: array (nullable = true)
|    |    |    |-- element: struct (containsNull = true)
|    |    |    |    |-- applicationcreditreportaccountid: string 
                                                   (nullable = true)
|    |    |    |    |-- account_type: integer (nullable = true)
|    |    |    |    |-- account_department: string (nullable = true)

以下是我的索引的映射:
{
  "bureau_data" : {
    "mappings" : {
      "dynamic_date_formats" : [
        "yyyy-MM-dd"
      ],
      "dynamic_templates" : [
        {
          "objects" : {
            "match_mapping_type" : "object",
            "mapping" : {
              "type" : "nested"
            }
          }
        }
      ],
      "properties" : {
        "raw_derived" : {
          "type" : "nested",
          "properties" : {
            "applications" : {
              "type" : "nested",
              "properties" : {
                "accounts" : {
                  "type" : "nested",
                  "properties" : {
                    "account_type_name" : {
                      "type" : "text",
                      "fields" : {
                        "keyword" : {
                          "type" : "keyword",
                          "ignore_above" : 256
                        }
                      }
                    },
                    "accounttypeid" : {
                      "type" : "text",
                      "fields" : {
                        "keyword" : {
                          "type" : "keyword",
                          "ignore_above" : 256
                        }
                      }
                    },
                    "applicationcreditreportaccountid" : {
                      "type" : "text",
                      "fields" : {
                        "keyword" : {
                          "type" : "keyword",
                          "ignore_above" : 256
                        }
                      }
                    },
                    "currentbalance" : {
                      "type" : "long"
                    },
                    "dayspastdue" : {
                      "type" : "long"
                    },
                    "institution_name" : {
                      "type" : "text",
                      "fields" : {
                        "keyword" : {
                          "type" : "keyword",
                          "ignore_above" : 256
                        }
                      }
                    },
                    "institutionid" : {
                      "type" : "text",
                      "fields" : {
                        "keyword" : {
                          "type" : "keyword",
                          "ignore_above" : 256
                        }
                      }
                    }
                  }
                },
                "applicationcreditreportid" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "applicationid" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "createdat" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "creditbureautypeid" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "dateofbirth" : {
                  "type" : "date",
                  "format" : "yyyy-MM-dd"
                },
                "firstname" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "lastname" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "score" : {
                  "type" : "long"
                },
                "source_name" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "status" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "updatedat" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                }
              }
            },
            "dob" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "firstname" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "lastname" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "middlename" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "mobilephone" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            },
            "source" : {
              "type" : "text",
              "fields" : {
                "keyword" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        },
        "userid" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          },
          "fielddata" : true
        }
      }
    }
  }
}

我想要account_type字段的不同值,这是一个嵌套字段。我试过查询,只给我不同的计数。
GET /my_index/_search?size=0
{
  "aggs": {
    "nested_path": {
      "nested": {
        "path": "raw_derived.applications.accounts"
      },
      "aggs": {
        "distinct_values": {
          "cardinality": {
            "field": "raw_derived.applications.accounts.account_type.keyword"
          }
        }
      }
    }
  }
}

我期望输出具有不同的account_type值,但输出仅是计数。以下是我的输出代码段:
  "hits" : {
    "total" : {
      "value" : 50,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "nested_path" : {
      "doc_count" : 828,
      "distinct_values" : {
        "value" : 70
      }
    }
  }
}

以下是我尝试过的查询及其工作方式:
GET /bureau_data/_search?size=0
{
 "_source": "{aggregations}", 
 "aggs": {
   "unique": {
     "nested": {
       "path": "raw_derived.applications"
     },
         "aggs": {
           "score_unq": {
             "terms": {
               "field": "raw_derived.applications.source_name.keyword"
             }
           }
         }
       }
     }
   }

任何建议都会有帮助

最佳答案

从官方文档-
基数汇总:-
单值指标聚合,用于计算不同值的近似计数。可以从文档中的特定字段中提取值,也可以通过脚本生成值。

与其按“基数”进行汇总,不如尝试以下术语汇总:

{
"size":0,
"aggregations": {
    "distinct_values": {
      "terms": {
        "field": "raw_derived.applications.accounts.account_type.keyword",
        "size": 1000,
        "min_doc_count": 1,
        "order": [
          {
            "_count": "desc"
          },
          {
            "_key": "asc"
          }
        ]
      }
    }
}

关于elasticsearch - 如何在Elasticsearch中获得嵌套字段的不同值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57802720/

相关文章:

elasticsearch - 如何在elasticsearch中搜索单个值

nested - SNMP 嵌套表定义在父表下一级

sql - SQL 中嵌套 NOT EXIST

elasticsearch - 弹性监视基础结构-度量标准中的空白字段

elasticsearch - 如何使用 StormCrawler 将网站内容存储在状态索引中?

elasticsearch - 在 `term`中组合 `match`和 `should`

elasticsearch - 带有不同字段的Elasticsearch查询

elasticsearch - Hibernate Search Mass Indexer不会删除映射

templates - 如何用两组数据修复golang嵌套模板

docker - kibana opendistro 无法连接到 Docker 上的 ElasticSearch open distro 容器