簡體   English   中英

如何在Elasticsearch上使用嵌套聚合?

[英]How to use Nested Aggregation on Elasticsearch?

我是Elasticsearch的新手。 我正在使用聚合編寫嵌套的dsl。

輸入文檔的結構是這樣的:

   {
        "_source": {
           "id": 1234,
           "oid": 6,

            "education": [
              {
                 "school_name": "Harvard",
                 "city" : "Boston",
                 "year": 1965,
                 "degree": "Undergrad"
              },
              {
                 "school_name": "Harvard",
                 "city" : "Boston",
                 "year": 1975,
                 "degree": "Masters"
              },
              {
                 "school_name": "Harvard",
                 "city" : "Boston",
                 "year": 1958,
                 "degree": "BA"
              }  
           ],
        }
     },

----其他記錄...等等

*以上顯示的文件符合一項記錄。

目標:我想找出所有在波士頓學習過的學生。 因此,理想情況下,如果我只有上述文檔,那么我應該只會得到1條記錄。

在下面編寫的嵌套聚合查詢中,我得到3作為波士頓的計數

GET cluster_test/index_test/_search
{
"query": {
 "bool": {
  "must": [
    {
      "term": {
        "oid": {
          "value": "6"
        }
      }
    }
  ]
}
},
 "aggs": {
    "education": {
      "nested": {
        "path": "education"
      },
      "aggs": {
        "edu": {
          "terms": {
            "field": "education.city",
            "size": 0
          }
        }
      }
    }
  }
}         

如果有人可以指出我出了問題的地方,或者哪種類型的查詢更好。 任何幫助表示贊賞。

您不應該使用匯總,因為您想過濾在所需城市學習的學生。 使用下面的過濾器應該會有所幫助。

GET cluster_test/index_test/students/_search
{
"filtered" : {
    "query" : { "match_all" : {} },
    "filter" : {
        "nested" : {
            "path" : "education",
            "filter" : {
                "bool" : {
                    "must" : [
                        {
                            "term" : {"education.city" : "Boston"}
                        }
                    ]
                }
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM