2017-10-05 6 views
0

外のネストされたフィールドに、そのフィールドの集計私はこのマッピングを持っている:Elasticsearch - ネスティング

{ 
    "event": { 
     "properties": { 
      "visitor": { 
       "type": "keyword" 
      }, 
      "location": { 
       "type": "nested", 
       "properties": { 
        "country": { 
         "type": "keyword" 
        }, 
        "region": { 
         "type": "keyword" 
        }, 
        "city": { 
         "type": "keyword" 
        } 
       } 
      } 
     } 
    } 
} 

意図したようにこれら二つの集計が働く:

{ 
    "size": 0, 
    "aggs": { 
     "location": { 
     "nested": { 
      "path": "location" 
     }, 
     "aggs": { 
      "by_country": { 
       "terms": { 
        "field": "location.country" 
       } 
      } 
     } 
     } 
    } 
} 

{ 
    "size": 0, 
    "aggs": { 
     "visitor_count": { 
     "cardinality": { 
      "field": "visitor" 
     } 
     } 
    } 
} 

しかし、私はそれらを結合してみてくださいこのように、国の集計は正常に機能しますが、ビジター数はすべて0になりますが、これは間違っています。

私がやろうとしていることを達成する方法を教えてもらえますか? 問題は、visitorフィールドがネストされたlocationフィールドの一部ではないが、回避策が見つからないということです。 (もちろん、実際のマッピングはより複雑であり、私は実際にそれを変える避けたい)

答えて

0

[OK]を、魔法のキーワードは「reverse_nested」が判明しました。このコードは私のためのトリックでした:

{ 
    "size": 0, 
    "aggs": { 
     "location": { 
     "nested": { 
      "path": "location" 
     }, 
     "aggs": { 
      "by_country": { 
       "terms": { 
        "field": "location.country" 
       }, 
       "aggs": { 
        "reverse": { 
        "reverse_nested": {}, 
        "aggs": { 
         "visitor_count": { 
          "cardinality": { 
           "field": "visitor" 
          } 
         } 
        } 
        } 
       } 
      } 
     } 
     } 
    } 
} 
関連する問題