2016-12-19 7 views
0

Hier Beispiel Dokumente:Gruppe von Feld eines Elements eines Arrays

{ 
    "_id": "doc_1", 
    "play_count": 1, 
    "meta": { 
    "ancestors": [ 
     {"content_type": "Folder"}, 
     {"content_type": "SerieContainer", 
     "_id": "super_doc_1" 
     } 
    ] 
    } 
} 


{ 
    "_id": "doc_2", 
    "play_count": 10, 
    "meta": { 
    "ancestors": [ 
     {"content_type": "Folder"}, 
     {"content_type": "SerieContainer", 
     "_id": "super_doc_1" 
     } 
    ] 
    } 
} 


{ 
    "_id": "doc_3", 
    "play_count": 100, 
    "meta": { 
    "ancestors": [ 
     {"content_type": "Folder"}, 
     {"content_type": "SerieContainer", 
     "_id": "super_doc_1" 
     } 
    ] 
    } 
} 


{ 
    "_id": "doc_4", 
    "play_count": 500, 
    "meta": { 
    "ancestors": [ 
     {"content_type": "Folder"}, 
     {"content_type": "SerieContainer", 
     "_id": "super_doc_2" 
     } 
    ] 
    } 
} 


{ 
    "_id": "doc_5", 
    "play_count": 5, 
    "meta": { 
    "ancestors": [ 
     {"content_type": "Folder"}, 
     {"content_type": "SerieContainer", 
     "_id": "super_doc_2" 
     } 
    ] 
    } 
} 

Ist es möglich, zu einer Gruppe ist jene Dokumente, die von _id von Elementen der Vorfahren, deren „content_type“ Feld „SerieContainer“ gleich und dann bekommen Summe von "play_count" -Feld von ihnen?

Zum Beispiel:

super_doc_1: 111 (doc_1, doc_2, doc_3) 
super_doc_2: 505 (doc_4, doc_5) 

Antwort

0

Stellen Sie sicher, ancestors Art von nested ist so, dass es eine Verbindung ist. So könnten Sie Ihre mapping etwas wie folgt haben. Ich habe gerade für die array Art gegeben:

"ancestors": { 
      "type": "nested", 
      "include_in_parent": true, 
      "properties": { 
      "content_type": { 
       "index": "not_analyzed", 
       "type": "string" 
      }, 
      "_id": { 
       "index": "not_analyzed", 
       "type": "string" 
      } 
      } 
     } 

Und dann vielleicht könnten Sie Ihre aggs Abfrage so etwas wie dies, um groupby mit der ID und Content-Typ haben.

{ 
    "aggs": { 
    "ancestors": { 
     "nested": { 
     "path": "item.meta.ancestors" 
     }, 
     "aggs": { 
     "id": { 
      "terms": { 
      "field": "item.meta.ancestors.id" 
      }, 
      "aggs": { 
      "content_type": { 
       "terms": { 
       "field": "item.meta.tags.content_type" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

Daher könnten Sie ein pattern haben, um Ihren Inhaltstyp oder die ID-Feld passen zu filtern. Diese SO könnte sich als nützlich erweisen. Ich hoffe es hilft!

Verwandte Themen