2016-01-18 104 views
5

請幫我找到一種機制來聚合以下域或證明它不存在於當前API中。在Elasticsearch中加入reverse_nested聚合

curl -XDELETE 127.0.0.1:9200/test_index 

    curl -XPUT 127.0.0.1:9200/test_index -d '{ 
     "mappings": { 
      "contact": { 
       "properties": { 
        "facebook_profile": { 
         "type": "nested", 
         "properties": { 
          "education": { 
           "type": "string" 
          }, 
          "year": { 
           "type": "integer" 
          } 
         } 
        }, 
        "google_profile": { 
         "type": "nested", 
         "properties": { 
          "education": { 
           "type": "string" 
          }, 
          "year": { 
           "type": "integer" 
          } 
         } 
        } 
       } 
      } 
     } 
    }' 

    curl -XPUT 127.0.0.1:9200/test_index/contact/contact1 -d '{ 
     "google_profile": { 
      "education": "stanford", "year": 1990 
     } 
    }' 

    curl -XPUT 127.0.0.1:9200/test_index/contact/contact2 -d ' 
    { 
     "facebook_profile": { 
      "education": "stanford", "year": 1990 
     } 
    }' 

如何查詢ES以查找有關從特定大學畢業的聯繫人數量的統計數據?

我發現了一種可能性,但它並沒有給我想要的結果,因爲它不能對這個問題對於接觸回答以上,而只是其特定的配置文件(嵌套文檔):

curl -XPOST '127.0.0.1:9200/test_index/_search?search_type=count&pretty=true' -d '{ 
     "aggs": { 
      "facebook_educations": { 
       "aggs": { 
        "field": { 
         "terms": { 
          "field": "contact.facebook_profile.education" 
         }, 
         "aggs": { 
          "reverse": { 
           "reverse_nested": { 
           } 
          } 
         } 
        } 
       }, 
       "nested": { 
        "path": "contact.facebook_profile" 
       } 
      }, 
      "google_educations": { 
       "aggs": { 
        "field": { 
         "terms": { 
          "field": "contact.google_profile.education" 
         }, 
         "aggs": { 
          "reverse": { 
           "reverse_nested": { 
           } 
          } 
         } 
        } 
       }, 
       "nested": { 
        "path": "contact.google_profile" 
       } 
      } 
     } 
    }' 

是什麼給了我:

"aggregations" : { 
     "facebook_educations" : { 
      "doc_count" : 1, 
      "field" : { 
      "doc_count_error_upper_bound" : 0, 
      "sum_other_doc_count" : 0, 
      "buckets" : [ { 
       "key" : "stanford", 
       "doc_count" : 1, 
       "reverse" : { 
       "doc_count" : 1 
       } 
      } ] 
      } 
     }, 
     "google_educations" : { 
      "doc_count" : 1, 
      "field" : { 
      "doc_count_error_upper_bound" : 0, 
      "sum_other_doc_count" : 0, 
      "buckets" : [ { 
       "key" : "stanford", 
       "doc_count" : 1, 
       "reverse" : { 
       "doc_count" : 1 
       } 
      } ] 
      } 
     } 
    } 

但在這裏我不能肯定,如果一個找到的聯繫人相同或不同的文檔(父),分別我不能回答我最初的問題。

謝謝你的任何建議。

回答

0

這聽起來像你正在嘗試aggregate on multiple fields。這不是Elasticsearch直接支持的,但有辦法解決這個問題,並得到你正在尋找的結果。

看一看discussion on Github,還有documentation

如果我的理解正確無誤,「斯坦福」是否出現在facebook_profile.educationgoogle_profile.education中,您希望contact在聚合中只計算一次。

你應該能夠做到這一點的方式有兩種:

  1. 使用腳本來連接存儲在字段中的值:

    { 
        "aggs": { 
        "by_education": { 
         "terms": { 
         "script": "doc['contact.facebook_profile.education'].values + doc['contact.google_profile.education'].values" 
         } 
        } 
        } 
    } 
    
  2. 您可以創建創建一個新的在索引時使用copy_to選項包含來自兩個字段的值的專用字段。然後聚合在單個字段上。例如,您可以將兩個字段的內容複製到名爲education_combined的新字段。

    { 
        "mappings":{ 
        "contact":{ 
         "properties":{ 
         "facebook_profile":{ 
          "type":"nested", 
          "properties":{ 
          "education":{ 
           "type":"string", 
           "copy_to":"education_combined" 
          }, 
          "year":{ 
           "type":"integer" 
          } 
          } 
         }, 
         "google_profile":{ 
          "type":"nested", 
          "properties":{ 
          "education":{ 
           "type":"string", 
           "copy_to":"education_combined" 
          }, 
          "year":{ 
           "type":"integer" 
          } 
          } 
         }, 
         "education_combined":{ 
          "type":"string" 
         } 
         } 
        } 
        } 
    } 
    

    然後,只需彙總在education_combined

    { 
        "aggs": { 
        "by_education": { 
         "terms": { "field": "education_combined" } 
        } 
        } 
    } 
    
+0

你的第一個建議根本不起作用,因爲嵌套的文檔,你必須訪問_source領域。第二個選項可能會工作,但不幸的是不在我的情況下,因爲我已經將文檔嵌套到子文檔中,我嘗試執行某種has_parent聚合。這是我現在得到的http://stackoverflow.com/questions/35061945/match-query-inside-script-elasticsearch。不管怎樣,謝謝你 – Serj