我目前正在學習 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
}
]
}
提前致謝 :)
uj5u.com熱心網友回復:
您可以將搜索查詢與聚合結合使用
您需要使用布爾查詢找到所有匹配“tour” = 1 和“election” = 1974 的檔案,然后使用sum 聚合來查找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
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/425464.html
