Skip to content

Commit

Permalink
disable aggregation unique citations
Browse files Browse the repository at this point in the history
  • Loading branch information
kjgarza committed Jul 30, 2019
1 parent 6e35753 commit 2ab6f96
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 37 deletions.
22 changes: 18 additions & 4 deletions app/controllers/concerns/facetable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 11 additions & 8 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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] = {
Expand All @@ -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
Expand Down
63 changes: 38 additions & 25 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 } },
Expand All @@ -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
Expand Down

0 comments on commit 2ab6f96

Please sign in to comment.