From 948372771ac191c65f609d2608a490e5d3005f3d Mon Sep 17 00:00:00 2001 From: Jeremy Prevost Date: Tue, 13 Feb 2024 16:17:57 -0500 Subject: [PATCH] Extract Aggregations Class --- app/models/aggregations.rb | 84 ++++++++++++++++++++++++++++++++++++++ app/models/opensearch.rb | 57 +------------------------- 2 files changed, 85 insertions(+), 56 deletions(-) create mode 100644 app/models/aggregations.rb diff --git a/app/models/aggregations.rb b/app/models/aggregations.rb new file mode 100644 index 0000000..df2fbea --- /dev/null +++ b/app/models/aggregations.rb @@ -0,0 +1,84 @@ +class Aggregations + # https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html + def self.all + { + contributors:, + content_type:, + content_format:, + languages:, + literary_form:, + source:, + subjects: + } + end + + def self.contributors + { + nested: { + path: 'contributors' + }, + aggs: { + contributor_names: { + terms: { + field: 'contributors.value.keyword' + } + } + } + } + end + + def self.content_type + { + terms: { + field: 'content_type' + } + } + end + + def self.content_format + { + terms: { + field: 'format' + } + } + end + + def self.languages + { + terms: { + field: 'languages.keyword' + } + } + end + + def self.literary_form + { + terms: { + field: 'literary_form' + } + } + end + + def self.source + { + terms: { + field: 'source' + } + } + end + + def self.subjects + { + nested: { + path: 'subjects' + }, + aggs: { + subject_names: { + terms: { + field: 'subjects.value.keyword' + } + } + } + } + end +end diff --git a/app/models/opensearch.rb b/app/models/opensearch.rb index 47a3a4e..810c60f 100644 --- a/app/models/opensearch.rb +++ b/app/models/opensearch.rb @@ -20,7 +20,7 @@ def build_query(from) from:, size: SIZE, query:, - aggregations:, + aggregations: Aggregations.all, sort: } @@ -245,61 +245,6 @@ def source_array(param) sources end - # https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html - def aggregations - { - contributors: { - nested: { - path: 'contributors' - }, - aggs: { - contributor_names: { - terms: { - field: 'contributors.value.keyword' - } - } - } - }, - content_type: { - terms: { - field: 'content_type' - } - }, - content_format: { - terms: { - field: 'format' - } - }, - languages: { - terms: { - field: 'languages.keyword' - } - }, - literary_form: { - terms: { - field: 'literary_form' - } - }, - source: { - terms: { - field: 'source' - } - }, - subjects: { - nested: { - path: 'subjects' - }, - aggs: { - subject_names: { - terms: { - field: 'subjects.value.keyword' - } - } - } - } - } - end - private def match_single_field(field, match_array)