Skip to content

Commit

Permalink
Merge pull request #62 from alphagov/extra-fields
Browse files Browse the repository at this point in the history
Extract `government_name` from documents
  • Loading branch information
csutter authored Oct 26, 2023
2 parents d848c9f + b909ee2 commit 234dbf5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
10 changes: 9 additions & 1 deletion lib/document_sync_worker/document/publish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ def metadata
part_of_taxonomy_tree: document_hash.dig("links", "taxons") || [],
# Vertex can only currently boost on numeric fields, not booleans
is_historic: historic? ? 1 : 0,
government_name:,
locale: document_hash["locale"],
parts:,
}
}.compact
end

# Extracts a single string of indexable unstructured content from the document.
Expand Down Expand Up @@ -102,6 +103,13 @@ def historic?
political && government&.dig("details", "current") == false
end

def government_name
document_hash
.dig("expanded_links", "government")
&.first
&.dig("title")
end

def parts
document_hash
.dig("details", "parts")
Expand Down
33 changes: 31 additions & 2 deletions spec/lib/document_sync_worker/document/publish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,6 @@
describe "is_historic" do
subject(:extracted_is_historic) { document.metadata[:is_historic] }

let(:document_hash) { { "locale" => "en" } }

context "when the document is non-political" do
let(:document_hash) { { "details" => {} } }

Expand Down Expand Up @@ -344,6 +342,37 @@
end
end

describe "government_name" do
subject(:extracted_government_name) { document.metadata[:government_name] }

context "when the document is non-political" do
let(:document_hash) { { "details" => {} } }

it { is_expected.to be_nil }
end

context "when the document is political" do
let(:document_hash) do
{
"details" => { "political" => true },
"expanded_links" => expanded_links,
}
end

context "without link to a government" do
let(:expanded_links) { {} }

it { is_expected.to be_nil }
end

context "with a link to a government" do
let(:expanded_links) { { "government" => [{ "title" => "2096 Something Party government" }] } }

it { is_expected.to eq("2096 Something Party government") }
end
end
end

describe "locale" do
subject(:extracted_locale) { document.metadata[:locale] }

Expand Down
5 changes: 2 additions & 3 deletions spec/lib/document_sync_worker_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
public_timestamp: 1_434_021_240,
document_type: "press_release",
is_historic: 0,
government_name: "2015 Conservative government",
content_purpose_supergroup: "news_and_communications",
part_of_taxonomy_tree: %w[
668cd623-c7a8-4159-9575-90caac36d4b4 c31256e8-f328-462b-993f-dce50b7892e9
],
locale: "en",
parts: nil,
)

expect(result[:content]).to start_with("<div class=\"govspeak\"><p>The government has")
Expand Down Expand Up @@ -118,14 +118,14 @@
public_timestamp: 1_284_336_000,
document_type: "news_story",
is_historic: 1,
government_name: "2010 to 2015 Conservative and Liberal Democrat coalition government",
content_purpose_supergroup: "news_and_communications",
part_of_taxonomy_tree: %w[
06ad07f7-1e79-462f-a192-6b2c9d92089c
ce9e9802-6138-4fe9-9f33-045ef213be29
3dbeb4a3-33c0-4bda-bd21-b721b0f8736f
],
locale: "en",
parts: nil,
)

expect(result[:content]).to start_with("<div class=\"govspeak\"><p>In the UEFA Champions")
Expand Down Expand Up @@ -155,7 +155,6 @@
content_purpose_supergroup: "other",
part_of_taxonomy_tree: [],
locale: "en",
parts: nil,
)

expect(result[:content]).to be_blank
Expand Down

0 comments on commit 234dbf5

Please sign in to comment.