ElasticSearch - 邻接矩阵和求和
ElasticSearch - Adjacency matrix & Sum
我目前正在学习 Elastic,我创建了这个关于 1965 年至 2017 年法国总统选举的数据集,我想查询匹配“tour”= 1 和“election”= 1974 的所有文档的总和。
我已经这样做了,但是它不起作用,我做错了什么?
{
"size": 0,
"aggs": {
"somme_blancs_nuls": {
"sum": {
"field": "blancs_nuls",
"aggs": {
"interactions": {
"adjacency_matrix": {
"filters": {
"grpA": {
"match": {
"tour": 1
}
},
"grpB": {
"match": {
"election": 1974
}
}
}
}
}
}
}
}
}
}
这是我的映射和示例文档。
// Mapping
{
"properties": {
"election": {
"type": "integer"
},
"tour": {
"type": "integer"
},
"code": {
"type": "text"
},
"libelle": {
"type": "text",
"fields": {
"keyword": {
"type":"keyword"
}
}
},
"inscrits": {
"type": "long"
},
"votants": {
"type": "long"
},
"exprimes": {
"type": "long"
},
"abstentions": {
"type": "long"
},
"blancs_nuls": {
"type": "long"
},
"candidats": {
"type": "nested",
"properties": {
"nom": {
"type": "text",
"fields": {
"keyword": {
"type":"keyword"
}
}
},
"parti": {
"type": "text",
"fields": {
"keyword": {
"type":"keyword"
}
}
},
"votes": {
"type": "long"
}
}
}
}
}
// Sample
{
"election": 1965,
"tour": 1,
"code": "1",
"libelle": "AIN",
"inscrits": 206496,
"votants": 166986,
"exprimes": 165555,
"abstentions": 39510,
"blancs_nuls": 1431,
"candidats": [
{
"nom": "François MITTERRAND",
"parti": "CIR",
"votes": 50418
},
{
"nom": "Charles DE GAULLE",
"parti": "UNR",
"votes": 71246
},
{
"nom": "Jean LECANUET",
"parti": "MRP",
"votes": 30416
},
{
"nom": "Jean-Louis TIXIER-VIGNANCOUR",
"parti": "EXD",
"votes": 8317
},
{
"nom": "Pierre MARCILHACY",
"parti": "DVD",
"votes": 3006
},
{
"nom": "Marcel BARBU",
"parti": "DIV",
"votes": 2152
}
]
}
提前致谢:)
您可以将搜索查询与聚合结合使用
您需要使用 boolean query to find all the documents matching "tour" = 1 and "election" = 1974, and then use sum aggregation 来查找匹配文档中 blancs_nuls
字段的总和
{
"size":0,
"query": {
"bool": {
"must": [
{
"match": {
"tour": 1
}
},
{
"match": {
"election": 1974
}
}
]
}
},
"aggs": {
"balancs_sum": {
"sum": {
"field": "blancs_nuls"
}
}
}
}
搜索响应将是
"aggregations": {
"balancs_sum": {
"value": 2862.0
}
}
我目前正在学习 Elastic,我创建了这个关于 1965 年至 2017 年法国总统选举的数据集,我想查询匹配“tour”= 1 和“election”= 1974 的所有文档的总和。
我已经这样做了,但是它不起作用,我做错了什么?
{
"size": 0,
"aggs": {
"somme_blancs_nuls": {
"sum": {
"field": "blancs_nuls",
"aggs": {
"interactions": {
"adjacency_matrix": {
"filters": {
"grpA": {
"match": {
"tour": 1
}
},
"grpB": {
"match": {
"election": 1974
}
}
}
}
}
}
}
}
}
}
这是我的映射和示例文档。
// Mapping
{
"properties": {
"election": {
"type": "integer"
},
"tour": {
"type": "integer"
},
"code": {
"type": "text"
},
"libelle": {
"type": "text",
"fields": {
"keyword": {
"type":"keyword"
}
}
},
"inscrits": {
"type": "long"
},
"votants": {
"type": "long"
},
"exprimes": {
"type": "long"
},
"abstentions": {
"type": "long"
},
"blancs_nuls": {
"type": "long"
},
"candidats": {
"type": "nested",
"properties": {
"nom": {
"type": "text",
"fields": {
"keyword": {
"type":"keyword"
}
}
},
"parti": {
"type": "text",
"fields": {
"keyword": {
"type":"keyword"
}
}
},
"votes": {
"type": "long"
}
}
}
}
}
// Sample
{
"election": 1965,
"tour": 1,
"code": "1",
"libelle": "AIN",
"inscrits": 206496,
"votants": 166986,
"exprimes": 165555,
"abstentions": 39510,
"blancs_nuls": 1431,
"candidats": [
{
"nom": "François MITTERRAND",
"parti": "CIR",
"votes": 50418
},
{
"nom": "Charles DE GAULLE",
"parti": "UNR",
"votes": 71246
},
{
"nom": "Jean LECANUET",
"parti": "MRP",
"votes": 30416
},
{
"nom": "Jean-Louis TIXIER-VIGNANCOUR",
"parti": "EXD",
"votes": 8317
},
{
"nom": "Pierre MARCILHACY",
"parti": "DVD",
"votes": 3006
},
{
"nom": "Marcel BARBU",
"parti": "DIV",
"votes": 2152
}
]
}
提前致谢:)
您可以将搜索查询与聚合结合使用
您需要使用 boolean query to find all the documents matching "tour" = 1 and "election" = 1974, and then use sum aggregation 来查找匹配文档中 blancs_nuls
字段的总和
{
"size":0,
"query": {
"bool": {
"must": [
{
"match": {
"tour": 1
}
},
{
"match": {
"election": 1974
}
}
]
}
},
"aggs": {
"balancs_sum": {
"sum": {
"field": "blancs_nuls"
}
}
}
}
搜索响应将是
"aggregations": {
"balancs_sum": {
"value": 2862.0
}
}