diff --git a/app/controllers/concerns/facetable.rb b/app/controllers/concerns/facetable.rb index 32aba70e0..62c42372d 100644 --- a/app/controllers/concerns/facetable.rb +++ b/app/controllers/concerns/facetable.rb @@ -136,8 +136,8 @@ def facet_by_source(arr) def facet_citations_by_year(hash) arr = hash.dig('years', 'buckets').map do |h| - year = h['key_as_string'][0..3].to_i - title = h['key_as_string'][0..3] + year = h['key'] + title = h['key'].to_i.to_s { 'id' => year, diff --git a/app/models/event.rb b/app/models/event.rb index 2d32fb2c3..793ae5a32 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -135,7 +135,7 @@ class Event < ActiveRecord::Base indexes :indexed_at, type: :date indexes :occurred_at, type: :date indexes :citation_id, type: :keyword - indexes :citation_year, type: :keyword + indexes :citation_year, type: :integer indexes :cache_key, type: :keyword end @@ -232,11 +232,15 @@ def self.metrics_aggregations } } + views_filter = {script: {script: "#{VIEWS_RELATION_TYPES}.contains(doc['relation_type_id'].value) && doc['source_id'].value == 'datacite-usage' && doc['occurred_at'].value.getMillis() >= doc['obj.datePublished'].value.getMillis() && doc['occurred_at'].value.getMillis() < new Date().getTime()"}} + + downloads_filter = {script: {script: "#{DOWNLOADS_RELATION_TYPES}.contains(doc['relation_type_id'].value) && doc['source_id'].value == 'datacite-usage' && doc['occurred_at'].value.getMillis() >= doc['obj.datePublished'].value.getMillis() && doc['occurred_at'].value.getMillis() < new Date().getTime()"} } + { citations_histogram: { filter: {script: {script: "#{INCLUDED_RELATION_TYPES}.contains(doc['relation_type_id'].value)"} }, - aggs: { years: { date_histogram: { field: 'citation_year', interval: 'year', min_doc_count: 1 }, aggs: { "total_by_year" => { sum: { field: 'total' }}}},"sum_distribution"=>sum_year_distribution} + aggs: { years: { histogram: { field: 'citation_year', interval: 1 , min_doc_count: 1 }, aggs: { "total_by_year" => { sum: { field: 'total' }}}},"sum_distribution"=>sum_year_distribution} }, citations: { filter: {script: {script: "#{INCLUDED_RELATION_TYPES}.contains(doc['relation_type_id'].value)"} @@ -246,15 +250,25 @@ def self.metrics_aggregations }} }, views: { - filter: {script: {script: "#{VIEWS_RELATION_TYPES}.contains(doc['relation_type_id'].value) && doc['source_id'].value == 'datacite-usage' && doc['occurred_at'].value.getMillis() >= doc['obj.datePublished'].value.getMillis() && doc['occurred_at'].value.getMillis() < new Date().getTime()"} - }, + filter: views_filter, + aggs: { dois: { + terms: { field: 'obj_id', size: 50, min_doc_count: 1 } + }} + }, + views_histogram: { + filter: views_filter, aggs: { year_months: { date_histogram: { field: 'occurred_at', interval: 'month', min_doc_count: 1 }, aggs: { "total_by_year_month" => { sum: { field: 'total' } } } }, "sum_distribution" => sum_distribution } }, downloads: { - filter: {script: {script: "#{DOWNLOADS_RELATION_TYPES}.contains(doc['relation_type_id'].value) && doc['source_id'].value == 'datacite-usage' && doc['occurred_at'].value.getMillis() >= doc['obj.datePublished'].value.getMillis() && doc['occurred_at'].value.getMillis() < new Date().getTime()"} + filter: downloads_filter, + aggs: { dois: { + terms: { field: 'obj_id', size: 50, min_doc_count: 1 } + }} }, + downloads_histogram: { + filter: downloads_filter, aggs: { year_months: { date_histogram: { field: 'occurred_at', interval: 'month', min_doc_count: 1 }, aggs: { "total_by_year_month" => { sum: { field: 'total' } } } }, "sum_distribution" => sum_distribution }