2016-12-19 2 views
0

Вот пример документы:группа по области элемента массива

{ 
    "_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" 
     } 
    ] 
    } 
} 

Можно сгруппировать эти документы по _id предметов предков которых поле «content_type» равно «SerieContainer», а затем получить сумма поля «play_count»?

Например:

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

ответ

0

Убедитесь ancestors является тип nested, так что существует связь. Таким образом, вы могли бы сделать свой mapping следующим образом. Я только что для array типа:

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

И тогда, возможно, вы могли бы иметь свой-то aggs запрос как это для того, чтобы groupby используя идентификатор и тип содержимого.

{ 
    "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" 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
} 

Таким образом, вы могли бы иметь pattern, чтобы соответствовать вашему типу контента или поле идентификатора для фильтрации. Это может пригодиться SO. Надеюсь, поможет!

Смежные вопросы