diff --git a/app/controllers/concerns/facetable.rb b/app/controllers/concerns/facetable.rb index 4787c3075..99a7d8942 100644 --- a/app/controllers/concerns/facetable.rb +++ b/app/controllers/concerns/facetable.rb @@ -134,12 +134,26 @@ def facet_by_source(arr) end end + # def facet_citations_by_year(hash) + # hash.map do |hsh| + # { "id" => hsh["key"].to_i, + # "title" => hsh["key"], + # "count" => hsh["doc_count"] } + # end + # end + def facet_citations_by_year(hash) - hash.map do |hsh| - { "id" => hsh["key"].to_i, - "title" => hsh["key"], - "count" => hsh["doc_count"] } + arr = hash.dig('years', 'buckets').map do |h| + year = h['key_as_string'][0..3].to_i + title = h['key_as_string'][0..3] + + { + 'id' => year, + 'title' => title, + 'sum' => h.dig('total_by_year', 'value') } end + { "count" => hash.dig("sum_distribution", "value"), + "years" => arr } end def facet_by_relation_type(arr) diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 3dd3a5a66..70de73952 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -121,8 +121,9 @@ def index pairings = total.positive? && params[:extra] ? facet_by_pairings(response.response.aggregations.pairings.buckets) : nil dois = total.positive? && params[:extra] ? facet_by_dois(response.response.aggregations.dois.buckets) : nil dois_usage = total.positive? && params[:extra] ? facet_by_dois(response.response.aggregations.dois_usage.dois.buckets) : nil - citations_histogram = total.positive? && params[:extra] ? facet_citations_by_year(response.response.aggregations.citations_histogram.years.buckets) : nil - citations = total.positive? && params[:extra] ? facet_citations_by_dois(response.response.aggregations.citations.dois.buckets) : nil + citations_histogram = total.positive? && params[:extra] ? facet_citations_by_year(response.response.aggregations.dois_citations) : nil + # citations_histogram = total.positive? && params[:extra] ? facet_citations_by_year(response.response.aggregations.citations_histogram.years.buckets) : nil + # citations = total.positive? && params[:extra] ? facet_citations_by_dois(response.response.aggregations.citations.dois.buckets) : nil results = response.results @@ -139,8 +140,8 @@ def index registrants: registrants, "doisRelationTypes": dois, "doisUsageTypes": dois_usage, - "doisCitations": citations_histogram, - "uniqueCitations": citations + "doisCitations": citations_histogram + # "uniqueCitations": citations }.compact options[:links] = { @@ -165,14 +166,16 @@ def index "page[size]" => page[:size] }.compact.to_query }.compact - options[:include] = [] options[:is_collection] = true bmr = Benchmark.ms { - doi_names = (results.map { |event| event.doi}).uniq().join(",") events_serialized = EventSerializer.new(results, options).serializable_hash - events_serialized[:included] = DoiSerializer.new((Doi.find_by_id(doi_names).results), {is_collection: true}).serializable_hash.dig(:data) - render json: events_serialized, status: :ok + if @include.include?(:dois) + options[:include] =[] + doi_names = (results.map { |event| event.doi}).uniq().join(",") + events_serialized[:included] = DoiSerializer.new((Doi.find_by_id(doi_names).results), {is_collection: true}).serializable_hash.dig(:data) + end + render json: events_serialized, status: :ok } if bmr > 3000 diff --git a/app/models/event.rb b/app/models/event.rb index bc499018c..ef36017e4 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -180,6 +180,12 @@ def self.query_aggregations } } + sum_year_distribution = { + sum_bucket: { + buckets_path: "years>total_by_year" + } + } + { sources: { terms: { field: 'source_id', size: 50, min_doc_count: 1 } }, prefixes: { terms: { field: 'prefix', size: 50, min_doc_count: 1 } }, @@ -193,37 +199,44 @@ def self.query_aggregations aggs: { dois: { terms: { field: 'obj_id', size: 50, min_doc_count: 1 }, aggs: { relation_types: { terms: { field: 'relation_type_id',size: 50, min_doc_count: 1 }, aggs: { "total_by_type" => { sum: { field: 'total' }}}}} } } }, - citations_histogram: { + dois_citations: { filter: { script: { script: "#{INCLUDED_RELATION_TYPES}.contains(doc['relation_type_id'].value)" } }, - aggs: { years: { terms: { script: { source: " - String subjDatePublished = params['_source']['subj']['date_published']?.substring(0, 4); - String objDatePublished = params['_source']['obj']['date_published']?.substring(0, 4); + aggs: { years: { date_histogram: { field: 'occurred_at', interval: 'year', min_doc_count: 1 }, aggs: { "total_by_year" => { sum: { field: 'total' }}}},"sum_distribution"=>sum_year_distribution} + # citations_histogram: { + # filter: { + # script: { + # script: "#{INCLUDED_RELATION_TYPES}.contains(doc['relation_type_id'].value)" + # } + # }, + # aggs: { years: { terms: { script: { source: " + # String subjDatePublished = params['_source']['subj']['date_published']?.substring(0, 4); + # String objDatePublished = params['_source']['obj']['date_published']?.substring(0, 4); - if( params['_source']['subj']['date_published']?.substring(0, 4) !== null && params['_source']['obj']['date_published']?.substring(0, 4) !== null){ - - if(Integer.parseInt(objDatePublished) > Integer.parseInt(subjDatePublished) ) - { - objDatePublished - } - else{ - subjDatePublished - } - } - " }}}} - }, - citations: { - filter: { - script: { - script: "#{INCLUDED_RELATION_TYPES}.contains(doc['relation_type_id'].value)" - } - }, - aggs: { dois: { - terms: { field: 'obj_id', size: 50, min_doc_count: 1 }, aggs: { unique_citations: { cardinality: { field: 'citation_id' }}} - }} + # if( params['_source']['subj']['date_published']?.substring(0, 4) !== null && params['_source']['obj']['date_published']?.substring(0, 4) !== null){ + + # if(Integer.parseInt(objDatePublished) > Integer.parseInt(subjDatePublished) ) + # { + # objDatePublished + # } + # else{ + # subjDatePublished + # } + # } + # " }}}} + # }, + # citations: { + # filter: { + # script: { + # script: "#{INCLUDED_RELATION_TYPES}.contains(doc['relation_type_id'].value)" + # } + # }, + # aggs: { dois: { + # terms: { field: 'obj_id', size: 50, min_doc_count: 1 }, aggs: { unique_citations: { cardinality: { field: 'citation_id' }}} + # }} } } end