Skip to content

Commit

Permalink
code tunning aggregations
Browse files Browse the repository at this point in the history
only call aggregation for adavtaced
  • Loading branch information
kjgarza committed Jan 2, 2020
1 parent 8bc5e15 commit 97c6c90
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,10 @@ def index

sources = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_source(response.response.aggregations.sources.buckets) : nil
prefixes = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_source(response.response.aggregations.prefixes.buckets) : nil
citation_types = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_citation_type(response.response.aggregations.citation_types.buckets) : nil
relation_types = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_relation_type(response.response.aggregations.relation_types.buckets) : nil
registrants = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_registrants(response.response.aggregations.registrants.buckets) : nil
pairings = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_pairings(response.response.aggregations.pairings.buckets) : nil
citation_types = total.positive? && aggregations.include?("advanced_aggregations") ? facet_by_citation_type(response.response.aggregations.citation_types.buckets) : nil
relation_types = total.positive? && aggregations.include?("advanced_aggregations") ? facet_by_relation_type(response.response.aggregations.relation_types.buckets) : nil
registrants = total.positive? && aggregations.include?("advanced_aggregations") ? facet_by_registrants(response.response.aggregations.registrants.buckets) : nil
pairings = total.positive? && aggregations.include?("advanced_aggregations") ? facet_by_pairings(response.response.aggregations.pairings.buckets) : nil
dois = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_by_dois(response.response.aggregations.dois.buckets) : nil
dois_usage = total.positive? ? EventsQuery.new.usage(params[:doi]) : nil
# dois_citations = total.positive? && aggregations.blank? || aggregations.include?("query_aggregations") ? facet_citations_by_year_v1(response.response.aggregations.dois_citations) : nil
Expand Down
20 changes: 10 additions & 10 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,9 @@ def self.query_fields
end

def self.query_aggregations
sum_distribution = {
sum_bucket: {
buckets_path: "year_months>total_by_year_month"
}
}

{
sources: { terms: { field: "source_id", size: 50, min_doc_count: 1 } },
prefixes: { terms: { field: "prefix", size: 50, min_doc_count: 1 } },
registrants: { terms: { field: "registrant_id", size: 50, min_doc_count: 1 }, aggs: { year: { date_histogram: { field: "occurred_at", interval: "year", min_doc_count: 1 }, aggs: { "total_by_year" => { sum: { field: "total" } } } } } },
pairings: { terms: { field: "registrant_id", size: 50, min_doc_count: 1 }, aggs: { recipient: { terms: { field: "registrant_id", size: 50, min_doc_count: 1 }, aggs: { "total" => { sum: { field: "total" } } } } } },
citation_types: { terms: { field: "citation_type", size: 50, min_doc_count: 1 }, aggs: { year_months: { date_histogram: { field: "occurred_at", interval: "month", min_doc_count: 1 }, aggs: { "total_by_year_month" => { sum: { field: "total" } } } } } },
relation_types: { terms: { field: "relation_type_id", size: 50, min_doc_count: 1 }, 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 } },
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" } } } } } }
}
end
Expand Down Expand Up @@ -259,7 +249,17 @@ def self.monthly_histogram_aggregation


def self.advanced_aggregations
sum_distribution = {
sum_bucket: {
buckets_path: "year_months>total_by_year_month"
}
}

{
registrants: { terms: { field: "registrant_id", size: 50, min_doc_count: 1 }, aggs: { year: { date_histogram: { field: "occurred_at", interval: "year", min_doc_count: 1 }, aggs: { "total_by_year" => { sum: { field: "total" } } } } } },
pairings: { terms: { field: "registrant_id", size: 50, min_doc_count: 1 }, aggs: { recipient: { terms: { field: "registrant_id", size: 50, min_doc_count: 1 }, aggs: { "total" => { sum: { field: "total" } } } } } },
citation_types: { terms: { field: "citation_type", size: 50, min_doc_count: 1 }, aggs: { year_months: { date_histogram: { field: "occurred_at", interval: "month", min_doc_count: 1 }, aggs: { "total_by_year_month" => { sum: { field: "total" } } } } } },
relation_types: { terms: { field: "relation_type_id", size: 50, min_doc_count: 1 }, 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 } },
unique_obj_count: { cardinality: { field: "obj_id" } },
unique_subj_count: { cardinality: { field: "subj_id" } }
}
Expand Down
16 changes: 8 additions & 8 deletions spec/concerns/indexable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@
aggregations = Event.get_aggregations_hash({aggregations:""})
expect(aggregations[:sources]).not_to be_nil
expect(aggregations[:prefixes]).not_to be_nil
expect(aggregations[:citation_types]).not_to be_nil
expect(aggregations[:relation_types]).not_to be_nil
expect(aggregations[:registrants]).not_to be_nil
expect(aggregations[:pairings]).not_to be_nil
expect(aggregations[:citation_types]).to be_nil
expect(aggregations[:relation_types]).to be_nil
expect(aggregations[:registrants]).to be_nil
expect(aggregations[:pairings]).to be_nil
# expect(aggregations[:dois_usage]).not_to be_nil
expect(aggregations[:citations_histogram]).to be_nil
expect(aggregations[:citations]).to be_nil
Expand All @@ -182,10 +182,10 @@
aggregations = Event.get_aggregations_hash({aggregations:"query_aggregations,citation_count_aggregation"})
expect(aggregations[:sources]).not_to be_nil
expect(aggregations[:prefixes]).not_to be_nil
expect(aggregations[:citation_types]).not_to be_nil
expect(aggregations[:relation_types]).not_to be_nil
expect(aggregations[:registrants]).not_to be_nil
expect(aggregations[:pairings]).not_to be_nil
expect(aggregations[:citation_types]).to be_nil
expect(aggregations[:relation_types]).to be_nil
expect(aggregations[:registrants]).to be_nil
expect(aggregations[:pairings]).to be_nil
expect(aggregations[:dois]).not_to be_nil
# expect(aggregations[:dois_usage]).not_to be_nil
expect(aggregations[:citations]).not_to be_nil
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
end

context "with registrant information" do
let(:uri) { "/events" }
let(:uri) { "/events?aggregations=advanced_aggregations" }
let(:params) do
{ "data" => { "type" => "events",
"attributes" => {
Expand Down

0 comments on commit 97c6c90

Please sign in to comment.