2015-02-23 140 views
2

我有一個嵌套聚合和過濾器的問題,基本上沒有過濾器它返回總和爲全局範圍但嵌套doc_count是好的,但總是0,這裏是我試圖運行的查詢:ElasticSearch中的嵌套聚合和過濾器

{ 
    "query": { 
     "nested": { 
      "path": "skills.tree", 
      "query": { 
      "bool" : { 
       "must" : [ 
       {"match": {"leaf0": "Management"}}, 
       {"match": {"leaf1": "Financial"}} 
       ] 
      } 
      } 
     } 
     }, 
    "aggs": { 
     "by_org": { 
       "terms": { 
       "field": "org" 
       }, 
       "aggs": { 
       "sum_weight0-filtered": { 
        "filter": { 
        "nested": { 
         "path": "skills.tree", 
         "query": { 
         "bool" : { 
          "must" : [ 
          {"match": {"leaf0": "Management"}}, 
          {"match": {"leaf1": "Financial"}} 
          ] 
         } 
         } 
        } 
        }, 
        "aggs":{ 
        "sum0":{ 
         "sum": { 
         "field": "skills.tree.weight0" 
         } 
        }, 
        "sum1":{ 
         "sum": { 
         "field": "skills.tree.weight1" 
         } 
        } 
        } 
       } 
       } 
     } 
    } 
} 

和下面是一個示例輸出:

{ 
    "took": 978, 
    "timed_out": false, 
    "_shards": { 
     "total": 50, 
     "successful": 50, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 11337, 
     "max_score": 0, 
     "hits": [] 
    }, 
    "aggregations": { 
     "by_org": { 
     "buckets": [ 
      { 
       "key": "Aetna", 
       "doc_count": 1888, 
       "sum_weight0-filtered": { 
        "doc_count": 1888, 
        "sum0": { 
        "value": 0 
        }, 
        "sum1": { 
        "value": 0 
        } 
       } 
      }, 
      { 
       "key": "AECOM", 
       "doc_count": 1085, 
       "sum_weight0-filtered": { 
        "doc_count": 1085, 
        "sum0": { 
        "value": 0 
        }, 
        "sum1": { 
        "value": 0 
        } 
       } 
      } 
.... 

,這裏是部分的模式:

'skills'  => array(
           'properties' => array(
            'tree' => array(
             'type' => 'nested', 
             'properties' => array(
              'leaf0' => array(
               "type"  => "multi_field", 
               "fields" => array(
                "leaf0"=> array(
                 "type" => "string", 
                 "index" => "not_analyzed" 
                ), 
                "search"  => array(
                 "type" => "string", 
                 "index" => "analyzed" 
                ) 
               ) 
              ), 
              'leaf1' => array(
               "type"  => "multi_field", 
               "fields" => array(
                "leaf1"=> array(
                 "type" => "string", 
                 "index" => "not_analyzed" 
                ), 
                "search"  => array(
                 "type" => "string", 
                 "index" => "analyzed" 
                ) 
               ) 
              ), 
              'leaf2' => array(
               "type"  => "multi_field", 
               "fields" => array(
                "leaf2"=> array(
                 "type" => "string", 
                 "index" => "not_analyzed" 
                ), 
                "search"  => array(
                 "type" => "string", 
                 "index" => "analyzed" 
                ) 
               ) 
              ), 
              'leaf3' => array(
               "type"  => "multi_field", 
               "fields" => array(
                "leaf3"=> array(
                 "type" => "string", 
                 "index" => "not_analyzed" 
                ), 
                "search"  => array(
                 "type" => "string", 
                 "index" => "analyzed" 
                ) 
               ) 
              ), 
              'leaf4' => array(
               "type"  => "multi_field", 
               "fields" => array(
                "leaf4"=> array(
                 "type" => "string", 
                 "index" => "not_analyzed" 
                ), 
                "search"  => array(
                 "type" => "string", 
                 "index" => "analyzed" 
                ) 
               ) 
              ), 
              'leaf5' => array(
               "type"  => "multi_field", 
               "fields" => array(
                "leaf5"=> array(
                 "type" => "string", 
                 "index" => "not_analyzed" 
                ), 
                "search"  => array(
                 "type" => "string", 
                 "index" => "analyzed" 
                ) 
               ) 
              ), 
              'weight1' => array(
               'type'  => 'integer', 
              ), 
              'weight2' => array(
               'type'  => 'integer', 
              ), 
              'weight3' => array(
               'type'  => 'integer', 
              ), 
              'weight4' => array(
               'type'  => 'integer', 
              ), 
              'weight5' => array(
               'type'  => 'integer', 
              ) 
             ) 
            ) 

問題在於sum0和sum1它們都返回0,儘管數值在那裏(它適用於更高範圍(無過濾器))。我在這裏做錯了什麼?

回答

1

您應用的嵌套過濾器僅適用於條件,而不適用於聚合將在後續聚合中查找值的位置。這意味着,和值出現在嵌套的對象,而不是在父文件,因此你有0.1 現在,如果你使用嵌套聚合問ES做嵌套對象上的聚集,它應該工作 -

{ 
    "query": { 
    "nested": { 
     "path": "skills.tree", 
     "query": { 
     "bool": { 
      "must": [ 
      { 
       "match": { 
       "leaf0": "Management" 
       } 
      }, 
      { 
       "match": { 
       "leaf1": "Financial" 
       } 
      } 
      ] 
     } 
     } 
    } 
    }, 
    "aggs": { 
    "by_org": { 
     "terms": { 
     "field": "org" 
     }, 
     "aggs": { 
     "sum_weight0-filtered": { 
      "filter": { 
      "nested": { 
       "path": "skills.tree", 
       "query": { 
       "bool": { 
        "must": [ 
        { 
         "match": { 
         "leaf0": "Management" 
         } 
        }, 
        { 
         "match": { 
         "leaf1": "Financial" 
         } 
        } 
        ] 
       } 
       } 
      } 
      }, 
      "aggs": { 
      "nestedAgg": { 
       "nested": { 
       "path": "skills.tree" 
       }, 
       "aggs": { 
       "sum0": { 
        "sum": { 
        "field": "skills.tree.weight0" 
        } 
       }, 
       "sum1": { 
        "sum": { 
        "field": "skills.tree.weight1" 
        } 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 
+0

謝謝,但對於初學者你有語法錯誤,nestedAgg應該有1個更多的層次稱爲「嵌套」,然後包裝所有下面說,它仍然無法正常工作,我的意思是總結所有數字,但從全球範圍,所以它忽略「領域」:「組織」的範圍,而不是在每個組織內部總結出去,這是我的原始問題。 – Marcin 2015-02-24 11:58:52

+0

請現在試試。所做的更改 – 2015-02-24 13:37:24

+0

還是不對的,基本上聚集針對完整的索引上運行,而不是水桶: ' 「聚合」:{ 「by_org」:{ 「桶」: { 「鑰匙」: 「本公司」, 「 doc_count 「:3171, 」sum_weight0過濾「:{ 」doc_count「:3171, 」nestedAgg「:{ 」doc_count「:60117, 」SUM0「:{ 」值「:343885 }, 」 sum1「:{ 」value「:93162 } }' – Marcin 2015-02-24 16:59:33

1

問題可能只是您如何訪問嵌套字段,具體而言,您必須根據您的映射定義,針對leaf0leaf1search子字段指示那些match語句,這些子字段是實際分析的子字段。考慮到這一點,請嘗試以下操作:

{ 
    "query": { 
     "nested": { 
      "path": "skills.tree", 
      "query": { 
      "bool" : { 
       "must" : [ 
       {"match": {"tree.leaf0.search": "Management"}}, 
       {"match": {"tree.leaf1.search": "Financial"}} 
       ] 
      } 
      } 
     } 
     }, 
    "aggs": { 
     "by_org": { 
       "terms": { 
       "field": "org" 
       }, 
       "aggs": { 
       "sum_weight0-filtered": { 
        "filter": { 
        "nested": { 
         "path": "skills.tree", 
         "query": { 
         "bool" : { 
          "must" : [ 
          {"match": {"tree.leaf0.search": "Management"}}, 
          {"match": {"tree.leaf1.search": "Financial"}} 
          ] 
         } 
         } 
        } 
        }, 
        "aggs":{ 
        "tree" : { 
         "nested" : {"path" : "skills.tree"}, 
         "aggs" : { 
         "sum0" : { 
          "sum": { 
          "field": "tree.weight0" 
          } 
         }, 
         "sum1": { 
          "sum": { 
          "field": "tree.weight1" 
          } 
         } 
         } 
        } 
        } 
       } 
       } 
      } 
    } 
} 

我這一個微小的人爲測試數據集的工作 - 這可能是值得一提的是,我針對索引的查詢在逃,而不是針對特定的文檔類型(因爲在您最初發布的查詢中,嵌套路徑似乎是「完全」限定的)。

+0

謝謝,但與另一個答案相同的問題,樹聚合doesn' t尊重過濾器,並且針對完整索引運行而不是僅在桶內運行 – Marcin 2015-02-24 17:03:10

+0

這裏是一個屏幕快照,您可以看到我的意思是完整索引 - http://postimg.org/image/4006mqtu9/ doc_count在樹下是不正確的 – Marcin 2015-02-24 17:05:49