diff --git a/Gemfile b/Gemfile index db5e5504..a071ed89 100644 --- a/Gemfile +++ b/Gemfile @@ -29,6 +29,7 @@ gem "plek", "~> 4.1" # TODO: unconstrain once govuk_pub_components up-to-date. gem "puma" gem "redcarpet" gem "rest-client" +gem "rsolr" gem "sass-rails" gem "secure_headers" gem "sentry-raven" diff --git a/Gemfile.lock b/Gemfile.lock index af56d1c9..da61e4b8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -528,6 +528,9 @@ GEM rexml (3.2.8) strscan (>= 3.0.9) rouge (4.2.0) + rsolr (2.6.0) + builder (>= 2.1.2) + faraday (>= 0.9, < 3, != 2.0.0) rspec (3.13.0) rspec-core (~> 3.13.0) rspec-expectations (~> 3.13.0) @@ -697,6 +700,7 @@ DEPENDENCIES rails (= 7.0.5.1) redcarpet rest-client + rsolr rspec rspec-rails rubocop-govuk diff --git a/app/controllers/solr_search_controller.rb b/app/controllers/solr_search_controller.rb new file mode 100644 index 00000000..ac354d89 --- /dev/null +++ b/app/controllers/solr_search_controller.rb @@ -0,0 +1,14 @@ +class SolrSearchController < ApplicationController + before_action :search_for_dataset, only: [:search] + + def search; end + +private + + def search_for_dataset + query = Search::Solr.search(params) + + @datasets = query["response"]["docs"] + @num_results = query["response"]["numFound"] + end +end diff --git a/app/services/search/solr.rb b/app/services/search/solr.rb new file mode 100644 index 00000000..503714a8 --- /dev/null +++ b/app/services/search/solr.rb @@ -0,0 +1,37 @@ +module Search + class Solr + def self.search(params) + query_param = params.fetch("q", "").squish + page = params["page"] + page && page.to_i.positive? ? page.to_i : 1 + + solr_client = client + + query = "*:*" if query_param.empty? + + solr_client.get "select", params: { + q: query, + start: page, + rows: 20, + fl: field_list, + } + end + + def self.field_list + %w[ + id + name + title + organization + notes + metadata_modified + extras_theme-primary + validated_data_dict + ].freeze + end + + def self.client + @client ||= RSolr.connect(url: ENV["SOLR_URL"]) + end + end +end diff --git a/app/views/solr_search/search.html.erb b/app/views/solr_search/search.html.erb new file mode 100644 index 00000000..9f6f33eb --- /dev/null +++ b/app/views/solr_search/search.html.erb @@ -0,0 +1,55 @@ +<% content_for :page_title do %><%= search_term.present? ? + t('.results_page_title', query: search_term) : + t('.search_results') %> +<% end %> + +<% content_for :head do %> + +<% end %> +
diff --git a/config/locales/views/search/en.yml b/config/locales/views/search/en.yml index 60ff5dab..d975d2f5 100644 --- a/config/locales/views/search/en.yml +++ b/config/locales/views/search/en.yml @@ -42,3 +42,16 @@ en: sort_by: "Sort by" accessibility: submit_button: "Apply sorting" + solr_search: + search: + results_page_title: 'Results for "%{query}"' + search_results: 'Search Results' + search_results_heading: "Search results" + search_data_gov_uk: "Search data.gov.uk" + result: "result" + found: "found" + meta_data_box: + availability: "Availability" + published_by: "Published by" + last_updated: "Last updated" + not_applicable: "Not applicable" diff --git a/config/routes.rb b/config/routes.rb index 46a6ae27..2525d69d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -50,6 +50,9 @@ get "dataset/:uuid/:name", to: "datasets#show", as: "dataset" get "dataset/:dataset_uuid/:name/datafile/:datafile_uuid/preview", to: "previews#show", as: "datafile_preview" + # /solr feature flag + get "search/solr", to: "solr_search#search" + get "acknowledge", to: "messages#acknowledge" # Route everything else to CKAN diff --git a/spec/features/solr_search_page_spec.rb b/spec/features/solr_search_page_spec.rb new file mode 100644 index 00000000..3ced8d1f --- /dev/null +++ b/spec/features/solr_search_page_spec.rb @@ -0,0 +1,45 @@ +require "rails_helper" + +RSpec.describe "Solr Search page", type: :feature do + let(:results) { File.read(Rails.root.join("spec/fixtures/solr_response.json").to_s) } + + before do + allow(Search::Solr).to receive(:search).and_return(JSON.parse(results)) + end + + scenario "Displays search heading" do + visit "/search/solr" + expect(page).to have_css("h1", text: "Search results") + end + + scenario "Displays search box" do + visit "/search/solr" + expect(page).to have_content("Search data.gov.uk") + end + + scenario "Displays search results" do + visit "/search/solr" + expect(page).to have_content("2 results found") + end + + scenario "Displays the title for each search result" do + visit "/search/solr" + expect(page).to have_css("h2", text: "A very interesting dataset") + expect(page).to have_css("h2", text: "A dataset with additional inspire metadata") + end + + scenario "Displays the publisher for each search result" do + visit "/search/solr" + results = all(".published_by") + expect(results.length).to be(2) + expect(results[0]).to have_content "Ministry of Housing, Communities and Local Government" + expect(results[1]).to have_content "Mole Valley District Council" + end + + scenario "Displays the last updated for each search result" do + visit "/search/solr" + expect(page).to have_content("Last updated", count: 2) + expect(page).to have_content("30 June 2017") + expect(page).to have_content("17 August 2018") + end +end diff --git a/spec/fixtures/solr_response.json b/spec/fixtures/solr_response.json new file mode 100644 index 00000000..18cc2c46 --- /dev/null +++ b/spec/fixtures/solr_response.json @@ -0,0 +1,29 @@ +{ + "response":{ + "numFound":2, + "start":0, + "numFoundExact":true, + "docs":[ + { + "id":"420932c7-e6f8-43ea-adc5-3141f757b5cb", + "name":"a-very-interesting-dataset", + "title":"A very interesting dataset", + "notes":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec euismod mauris in augue laoreet congue. Phasellus bibendum leo vel magna lacinia eleifend. Nam vitae lectus quis nulla varius faucibus id quis nibh. Nullam auctor ipsum non nunc efficitur bibendum Sed vitae ex nisi. Suspendisse posuere purus ac dui posuere, in interdum risus ornare. \n\nThe following files can be found on the website here:\n\nhttps://www.gov.uk/", + "metadata_modified":"2017-06-30T09:08:37.040Z", + "organization":"department-for-communities-and-local-government", + "extras_theme-primary":"government", + "validated_data_dict": "{\"author\": null, \"author_email\": null, \"contact-email\": \"\", \"contact-name\": \"\", \"contact-phone\": \"\", \"core-dataset\": false, \"creator_user_id\": \"6e382fff-b824-43aa-bbd4-ebbea5a62d7d\", \"date_released\": \"2011-10-27\", \"date_updated\": \"2016-03-24\", \"foi-email\": \"\", \"foi-name\": \"\", \"foi-phone\": \"\", \"foi-web\": \"\", \"geographic_coverage\": \"100000: England\", \"geographic_granularity\": \"national\", \"id\": \"420932c7-e6f8-43ea-adc5-3141f757b5cb\", \"isopen\": true, \"license_id\": \"uk-ogl\", \"license_title\": \"UK Open Government Licence (OGL)\", \"license_url\": \"http://reference.data.gov.uk/id/open-government-licence\", \"maintainer\": null, \"maintainer_email\": null, \"metadata_created\": \"2011-10-27T13:29:52.056625\", \"metadata_modified\": \"2017-06-30T09:08:37.040414\", \"name\": \"performance-related-pay-department-for-communities-and-local-government\", \"notes\": \"Publication of information on Non-consolidated Performance Related Pay (NCPRP) data for the Department for Communities and Local Government and its executive agencies in respect of the performance years 2010-11, 2011-12, 2013-14 and 2014-15.\\r\\n\\r\\nThe following files can be found on the DCLG website here:\\r\\n\\r\\nhttps://www.gov.uk/government/collections/dclg-performance-related-pay\", \"num_resources\": 12, \"num_tags\": 0, \"odi-certificate\": {\"status\": \"final\", \"source\": \"Automatically awarded by ODI\", \"certification_type\": \"automatically awarded\", \"level\": \"bronze\", \"title\": \"Non-consolidated Performance Related Pay - Department for Communities and Local Government\", \"created_at\": \"2014-10-28T12:53:13Z\", \"jurisdiction\": \"GB\", \"certificate_url\": \"https://certificates.theodi.org/en/datasets/5922/certificate\", \"badge_url\": \"https://certificates.theodi.org/en/datasets/5922/certificate/badge.png\", \"cert_title\": \"Bronze level certificate\"}, \"organization\": {\"id\": \"48f370c9-7ab9-4550-9722-8c9477020fc7\", \"name\": \"department-for-communities-and-local-government\", \"title\": \"Ministry of Housing, Communities and Local Government\", \"type\": \"organization\", \"description\": \"The Ministry of Housing, Communities and Local Government's (formerly the Department for Communities and Local Government) job is to create great places to live and work, and to give more power to local people to shape what happens in their area.\\r\\n\\r\\nhttps://www.gov.uk/government/organisations/ministry-of-housing-communities-and-local-government\\r\\n\", \"image_url\": \"\", \"created\": \"2012-06-27T14:48:38.548677\", \"is_organization\": true, \"approval_status\": \"pending\", \"state\": \"active\"}, \"owner_org\": \"48f370c9-7ab9-4550-9722-8c9477020fc7\", \"private\": false, \"state\": \"active\", \"temporal_coverage-from\": \"2010-04-01\", \"temporal_coverage-to\": \"2015-03-31\", \"temporal_granularity\": \"year\", \"theme-primary\": \"government\", \"theme-secondary\": [\"Environment\"], \"title\": \"Non-consolidated Performance Related Pay - Department for Communities and Local Government\", \"type\": \"dataset\", \"unpublished\": false, \"update_frequency\": \"annual\", \"url\": \"http://https://www.gov.uk/government/collections/dclg-performance-related-pay\", \"version\": null, \"extras\": [{\"key\": \"its-dataset\", \"value\": \"false\"}, {\"key\": \"register\", \"value\": \"false\"}, {\"key\": \"sla\", \"value\": \"\"}], \"resources\": [{\"cache_last_updated\": null, \"cache_url\": null, \"description\": \"Non-consolidated performance related payments 2010-11\", \"format\": \"XLS\", \"id\": \"ed403118-c791-4494-92f3-acd633e48178\", \"last_modified\": null, \"metadata_modified\": null, \"mimetype\": null, \"mimetype_inner\": null, \"name\": null, \"package_id\": \"420932c7-e6f8-43ea-adc5-3141f757b5cb\", \"position\": 0, \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/5722/2016987.xls\", \"url_type\": null}, {\"cache_last_updated\": null, \"cache_url\": null, \"description\": \"Non-consolidated performance related payments 2010-11 (CSV format)\", \"format\": \"CSV\", \"id\": \"0e1a19ab-fef2-408f-84ce-ae4e991ec56b\", \"last_modified\": null, \"metadata_modified\": null, \"mimetype\": null, \"mimetype_inner\": null, \"name\": null, \"package_id\": \"420932c7-e6f8-43ea-adc5-3141f757b5cb\", \"position\": 1, \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/5723/2016992.csv\", \"url_type\": null}, {\"cache_last_updated\": null, \"cache_url\": null, \"created\": \"2012-12-20T10:09:01.499730\", \"description\": \"Non-consolidated performance related payments 2011-12\", \"format\": \"XLS\", \"id\": \"404b765a-f66f-4cb0-9a23-788c941d5a47\", \"last_modified\": null, \"metadata_modified\": \"2012-12-20T10:09:01.499730\", \"mimetype\": null, \"mimetype_inner\": null, \"name\": null, \"package_id\": \"420932c7-e6f8-43ea-adc5-3141f757b5cb\", \"position\": 2, \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/39452/NCPRP_exercise_template_2011-12_Performance_Year_Publish.xls\", \"url_type\": null}, {\"cache_last_updated\": null, \"cache_url\": null, \"created\": \"2012-12-20T10:09:01.499752\", \"description\": \"Non-consolidated performance related payments 2011-12 (CSV format)\", \"format\": \"CSV\", \"id\": \"2e529e11-5d1e-48f0-89c8-1310836a329a\", \"last_modified\": null, \"metadata_modified\": \"2012-12-20T10:09:01.499752\", \"mimetype\": null, \"mimetype_inner\": null, \"name\": null, \"package_id\": \"420932c7-e6f8-43ea-adc5-3141f757b5cb\", \"position\": 3, \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/39453/NCPRP_exercise_template_2011-12_Performance_Year_Publish.csv\", \"url_type\": null}, {\"cache_last_updated\": null, \"cache_url\": null, \"created\": \"2014-02-27T12:29:43.822722\", \"description\": \"Non-consolidated performance related payments 2012-13 (XLS format)\", \"format\": \"XLS\", \"id\": \"46abbced-dd0f-45c4-b15e-72d94fbce62e\", \"last_modified\": null, \"metadata_modified\": \"2014-02-27T12:29:43.822722\", \"mimetype\": null, \"mimetype_inner\": null, \"name\": null, \"package_id\": \"420932c7-e6f8-43ea-adc5-3141f757b5cb\", \"position\": 4, \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/285255/NCPRP_template_for_publication_2013.xls\", \"url_type\": null}, {\"cache_last_updated\": null, \"cache_url\": null, \"created\": \"2014-02-27T12:29:43.822741\", \"description\": \"Non-consolidated performance related payments 2012-13 (CSV format)\", \"format\": \"CSV\", \"id\": \"64f94d94-cbba-4dd6-a5db-593604f3451c\", \"last_modified\": null, \"metadata_modified\": \"2014-02-27T12:29:43.822741\", \"mimetype\": null, \"mimetype_inner\": null, \"name\": null, \"package_id\": \"420932c7-e6f8-43ea-adc5-3141f757b5cb\", \"position\": 5, \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/285256/NCPRP_data_collection_CSV_2013.csv\", \"url_type\": null}, {\"cache_last_updated\": null, \"cache_url\": null, \"created\": \"2016-03-24T15:53:39.464970\", \"description\": \"Non-consolidated performance related payments 2013-14 (XLS format)\", \"format\": \"XLS\", \"hash\": \"\", \"id\": \"8f923422-a9b5-475c-a57d-df85ef796b24\", \"last_modified\": null, \"metadata_modified\": \"2016-03-24T15:53:39.464970\", \"mimetype\": null, \"mimetype_inner\": null, \"name\": null, \"package_id\": \"420932c7-e6f8-43ea-adc5-3141f757b5cb\", \"position\": 6, \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/414725/NCPRP_template_for_publication_2014.xls\", \"url_type\": null}, {\"cache_last_updated\": null, \"cache_url\": null, \"created\": \"2016-03-24T15:53:39.464998\", \"description\": \"Non-consolidated performance related payments 2013-14 (CSV format)\", \"format\": \"CSV\", \"hash\": \"\", \"id\": \"9ce8b540-cd45-4c77-ad18-e26752987200\", \"last_modified\": null, \"metadata_modified\": \"2016-03-24T15:53:39.464998\", \"mimetype\": null, \"mimetype_inner\": null, \"name\": null, \"package_id\": \"420932c7-e6f8-43ea-adc5-3141f757b5cb\", \"position\": 7, \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/414726/NCPRP_template_for_publication_2014.csv\", \"url_type\": null}, {\"cache_last_updated\": null, \"cache_url\": null, \"created\": \"2016-03-24T15:53:39.465009\", \"description\": \"Non-consolidated performance related payments 2014-15 (XLS format)\", \"format\": \"XLS\", \"hash\": \"\", \"id\": \"05ad2563-fea3-4c74-bf62-829b52ef834a\", \"last_modified\": null, \"metadata_modified\": \"2016-03-24T15:53:39.465009\", \"mimetype\": null, \"mimetype_inner\": null, \"name\": null, \"package_id\": \"420932c7-e6f8-43ea-adc5-3141f757b5cb\", \"position\": 8, \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/510778/DCLG_non-consolidated_performance_related_pay_2014-15.xlsx\", \"url_type\": null}, {\"cache_last_updated\": null, \"cache_url\": null, \"created\": \"2016-03-24T15:53:39.465019\", \"description\": \"Non-consolidated performance related payments 2014-15 (CSV format)\", \"format\": \"CSV\", \"hash\": \"\", \"id\": \"b2ed285e-23f2-4ec2-9f6c-d03908778a40\", \"last_modified\": null, \"metadata_modified\": \"2016-03-24T15:53:39.465019\", \"mimetype\": null, \"mimetype_inner\": null, \"name\": null, \"package_id\": \"420932c7-e6f8-43ea-adc5-3141f757b5cb\", \"position\": 9, \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/510779/DCLG_non-consolidated_performance_related_pay_2014-15.csv\", \"url_type\": null}, {\"cache_last_updated\": null, \"cache_url\": null, \"created\": \"2017-06-30T10:08:37.107376\", \"description\": \"Non-consolidated performance related payments 2015-16 (CSV format)\", \"format\": \"CSV\", \"hash\": \"\", \"id\": \"fa0d6d30-8e17-441c-9544-ba41a9b96bc9\", \"last_modified\": null, \"metadata_modified\": \"2017-06-30T10:08:37.107376\", \"mimetype\": null, \"mimetype_inner\": null, \"name\": null, \"package_id\": \"420932c7-e6f8-43ea-adc5-3141f757b5cb\", \"position\": 10, \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/623632/DCLG_NCPRP_exercise_2015-2016.csv\", \"url_type\": null}, {\"cache_last_updated\": null, \"cache_url\": null, \"created\": \"2017-06-30T10:08:37.107405\", \"description\": \"Non-consolidated performance related payments 2015-16 (XLS format)\", \"format\": \"XLS\", \"hash\": \"\", \"id\": \"fd3aeffd-01ed-4611-9c4c-e0ea0aec82fd\", \"last_modified\": null, \"metadata_modified\": \"2017-06-30T10:08:37.107405\", \"mimetype\": null, \"mimetype_inner\": null, \"name\": null, \"package_id\": \"420932c7-e6f8-43ea-adc5-3141f757b5cb\", \"position\": 11, \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/623723/DCLG_NCPRP_exercise_2015-2016.xlsx\", \"url_type\": null}], \"tags\": [], \"groups\": [], \"relationships_as_subject\": [], \"relationships_as_object\": []}" + }, + { + "id":"9f569f8e-6059-4e45-80e4-ae07dbbb0b70", + "name":"a-dataset-with-additional-inspire-metadata", + "title":"A dataset with additional inspire metadata", + "notes":"Integer ligula est, porttitor quis enim eget, tincidunt eleifend lectus. Suspendisse vitae urna consequat, posuere lacus vitae, hendrerit nisi. Maecenas leo mauris, placerat nec hendrerit nec, scelerisque eu libero.", + "metadata_modified":"2018-08-17T11:15:47.380Z", + "organization":"mole-valley-district-council", + "extras_theme-primary":"government", + "validated_data_dict": "{\"author\": null, \"author_email\": null, \"contact-email\": \"info@council.gov.uk\", \"creator_user_id\": \"431b5956-7b87-41ba-8fc5-a4a8b681b797\", \"id\": \"9f569f8e-6059-4e45-80e4-ae07dbbb0b70\", \"isopen\": false, \"license_id\": \"\", \"license_title\": \"\", \"maintainer\": null, \"maintainer_email\": null, \"metadata_created\": \"2018-08-17T11:11:47.990684\", \"metadata_modified\": \"2018-08-17T11:15:47.380083\", \"name\": \"a-dataset-with-additional-inspire-metadata\", \"notes\": \"Integer ligula est, porttitor quis enim eget, tincidunt eleifend lectus. Suspendisse vitae urna consequat, posuere lacus vitae, hendrerit nisi. Maecenas leo mauris, placerat nec hendrerit nec, scelerisque eu libero\", \"num_resources\": 2, \"num_tags\": 0, \"organization\": {\"id\": \"fe00f406-5b1b-4f7c-b35b-f535536fb87e\", \"name\": \"mole-valley-district-council\", \"title\": \"Mole Valley District Council\", \"type\": \"organization\", \"description\": \"Second tier Local Government authority in Surrey, England.\", \"image_url\": \"\", \"created\": \"2013-10-14T10:21:57.920455\", \"is_organization\": true, \"approval_status\": \"pending\", \"state\": \"active\"}, \"owner_org\": \"fe00f406-5b1b-4f7c-b35b-f535536fb87e\", \"private\": false, \"state\": \"active\", \"temporal_coverage-from\": [], \"temporal_coverage-to\": [], \"theme-primary\": \"environment\", \"theme-secondary\": [], \"title\": \"A dataset with additional inspire metadata\", \"type\": \"dataset\", \"url\": null, \"version\": null, \"extras\": [{\"key\": \"UKLP\", \"value\": \"True\"}, {\"key\": \"access_constraints\", \"value\": \"[\\\"Public Sector End User Licence for INSPIRE applies (http://www.ordnancesurvey.co.uk/business-and-government/public-sector/mapping-agreements/inspire-licence.html)\\\"]\"}, {\"key\": \"bbox-east-long\", \"value\": \"0.22941173\"}, {\"key\": \"bbox-north-lat\", \"value\": \"51.45277088\"}, {\"key\": \"bbox-south-lat\", \"value\": \"50.97386111\"}, {\"key\": \"bbox-west-long\", \"value\": \"-0.86191815\"}, {\"key\": \"coupled-resource\", \"value\": \"[]\"}, {\"key\": \"dataset-reference-date\", \"value\": \"[{\\\"type\\\": \\\"publication\\\", \\\"value\\\": \\\"2015-04-10\\\"}]\"}, {\"key\": \"frequency-of-update\", \"value\": \"notPlanned\"}, {\"key\": \"guid\", \"value\": \"18054077-ba8e-42e0-825e-fd219f36a8d1\"}, {\"key\": \"harvest_object_id\", \"value\": \"8d2be61e-cd6d-43c8-b338-2b4a2ab34b00\"}, {\"key\": \"harvest_source_reference\", \"value\": \"http://inspire.misoportal.com/metadata/files/Mole_Valley_District_Council/228-MVDC_ISP_SPA.xml\"}, {\"key\": \"import_source\", \"value\": \"harvest\"}, {\"key\": \"licence\", \"value\": \"Public Sector End User Licence for INSPIRE applies (http://www.ordnancesurvey.co.uk/business-and-government/public-sector/mapping-agreements/inspire-licence.html) (http://www.ordnancesurvey.co.uk/business-and-government/public-sector/mapping-agreements/inspire-licence.html)\"}, {\"key\": \"metadata-date\", \"value\": \"2018-08-17\"}, {\"key\": \"metadata-language\", \"value\": \"eng\"}, {\"key\": \"provider\", \"value\": \"Mole Valley District Council\"}, {\"key\": \"resource-type\", \"value\": \"dataset\"}, {\"key\": \"responsible-party\", \"value\": \"Mole Valley District Council (publisher)\"}, {\"key\": \"spatial\", \"value\": \"{\\\"type\\\":\\\"Polygon\\\",\\\"coordinates\\\":[[[0.22941173, 50.97386111],[0.22941173, 51.45277088], [-0.86191815, 51.45277088], [-0.86191815, 50.97386111], [0.22941173, 50.97386111]]]}\"}, {\"key\": \"spatial-data-service-type\", \"value\": \"\"}, {\"key\": \"spatial-reference-system\", \"value\": \"http://www.opengis.net/def/crs/EPSG/0/27700\"}, {\"key\": \"harvest_source_id\", \"value\": \"48826151-c3d9-4e0e-8920-013c5e128938\"}, {\"key\": \"harvest_source_title\", \"value\": \"Mole Valley District Council\"}], \"resources\": [{\"cache_last_updated\": null, \"cache_url\": null, \"created\": \"2018-08-17T12:11:48.046491\", \"description\": \"information\", \"format\": \"WMS\", \"hash\": \"\", \"id\": \"ca238695-0be6-48b1-87f0-9a1265ee54c4\", \"last_modified\": null, \"metadata_modified\": \"2018-08-17T12:11:48.046491\", \"mimetype\": null, \"mimetype_inner\": null, \"name\": \"INSPIRE View Service\", \"package_id\": \"9f569f8e-6059-4e45-80e4-ae07dbbb0b70\", \"position\": 0, \"resource_locator_function\": \"information\", \"resource_locator_protocol\": \"\", \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"http://inspire.misoportal.com/geoserver/mole_valley_district_council_special_protection_areas_polygon/wms?request=getCapabilities\", \"url_type\": null, \"wms_base_urls\": \"http://inspire.misoportal.com:80/geoserver/mole_valley_district_council_special_protection_areas_polygon/ows\"}, {\"cache_last_updated\": null, \"cache_url\": null, \"created\": \"2018-08-17T12:11:48.046517\", \"description\": \"download\", \"format\": \"\", \"hash\": \"\", \"id\": \"3dc8939c-947e-477e-9257-a5dd6d6f73c7\", \"last_modified\": null, \"metadata_modified\": \"2018-08-17T12:11:48.046517\", \"mimetype\": null, \"mimetype_inner\": null, \"name\": \"INSPIRE Download Service\", \"package_id\": \"9f569f8e-6059-4e45-80e4-ae07dbbb0b70\", \"position\": 1, \"resource_locator_function\": \"download\", \"resource_locator_protocol\": \"\", \"resource_type\": \"file\", \"size\": null, \"state\": \"active\", \"url\": \"http://inspire.misoportal.com/geoserver/mole_valley_district_council_special_protection_areas_polygon/wfs?service=wfs&version=2.0.0&request=GetCapabilities\", \"url_type\": null}], \"tags\": [], \"groups\": [], \"relationships_as_subject\": [], \"relationships_as_object\": []}" + } + ] + } +} diff --git a/spec/services/search/solr_spec.rb b/spec/services/search/solr_spec.rb new file mode 100644 index 00000000..707bb88b --- /dev/null +++ b/spec/services/search/solr_spec.rb @@ -0,0 +1,79 @@ +require "rails_helper" + +RSpec.describe Search::Solr do + before do + ENV["SOLR_URL"] = "http://localhost:8983/solr" + end + + describe "#client" do + let(:client) { described_class.client } + + it "is an instance of the RSolr client" do + expect(client).to be_an_instance_of(RSolr::Client) + end + + it "connects to Solr with a valid URL" do + expect(client.options[:url]).to eq(ENV["SOLR_URL"]) + end + + it "only sets up the connection once" do + client1 = described_class.client + expect(client1).to eq(client) + end + end + + describe "#search" do + let(:response) { File.read(Rails.root.join("spec/fixtures/solr_response.json").to_s) } + let(:results) { described_class.search("q" => "") } + + before do + allow_any_instance_of(RSolr::Client).to receive(:get).and_return(JSON.parse(response)) + end + + it "returns a JSON response" do + expect(results).to be_a(Hash) + end + + it "includes a count of the results" do + expect(results["response"]["numFound"]).to eq(2) + end + + it "includes the datasets" do + datasets = results["response"]["docs"] + expect(datasets.length).to eq(2) + end + + context "datasets" do + let(:dataset) { results["response"]["docs"].first } + + it "includes the id" do + expect(dataset["id"]).to eq("420932c7-e6f8-43ea-adc5-3141f757b5cb") + end + + it "includes the name" do + expect(dataset["name"]).to eq("a-very-interesting-dataset") + end + + it "includes the title" do + expect(dataset["title"]).to eq("A very interesting dataset") + end + + it "includes the notes" do + expect(dataset["notes"]).to eq("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec euismod mauris in augue laoreet congue. Phasellus bibendum leo vel magna lacinia eleifend. Nam vitae lectus quis nulla varius faucibus id quis nibh. Nullam auctor ipsum non nunc efficitur bibendum Sed vitae ex nisi. Suspendisse posuere purus ac dui posuere, in interdum risus ornare. \n\nThe following files can be found on the website here:\n\nhttps://www.gov.uk/") + end + + it "includes the last updated date" do + expect(dataset["metadata_modified"]).to eq("2017-06-30T09:08:37.040Z") + end + + it "includes the organization slug" do + expect(dataset["organization"]).to eq("department-for-communities-and-local-government") + end + + it "includes the organization name" do + data_dict = JSON.parse(dataset["validated_data_dict"]) + expect(data_dict["organization"]["title"]).to eq("Ministry of Housing, Communities and Local Government") + end + end + end +end