Class: GdsApi::Rummager
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#add_document(type, id, document) ⇒ GdsApi::Response
Add a document to the search index.
-
#advanced_search(args) ⇒ Object
deprecated
Deprecated.
Only in use by Whitehall. Use the ‘#search` method.
-
#delete_content(base_path) ⇒ Object
Delete a content-document from the index by base path.
- #delete_content! ⇒ Object
-
#delete_document(type, id) ⇒ Object
Delete a non-content document from the search index.
-
#get_content(base_path) ⇒ Object
Retrieve a content-document from the index.
- #get_content! ⇒ Object
-
#search(args, additional_headers = {}) ⇒ Object
Perform a search.
-
#search_enum(args, page_size: 100, additional_headers: {}) ⇒ Object
Perform a search, returning the results as an enumerator.
Methods inherited from Base
#client, #create_client, #get_list, #initialize, #url_for_slug
Constructor Details
This class inherits a constructor from GdsApi::Base
Instance Method Details
#add_document(type, id, document) ⇒ GdsApi::Response
Add a document to the search index.
58 59 60 61 62 63 64 65 66 |
# File 'lib/gds_api/rummager.rb', line 58 def add_document(type, id, document) post_json( documents_url, document.merge( _type: type, _id: id, ) ) end |
#advanced_search(args) ⇒ Object
Only in use by Whitehall. Use the ‘#search` method.
Advanced search.
44 45 46 47 48 |
# File 'lib/gds_api/rummager.rb', line 44 def advanced_search(args) raise ArgumentError.new("Args cannot be blank") if args.nil? || args.empty? request_path = "#{base_url}/advanced_search?#{Rack::Utils.build_nested_query(args)}" get_json(request_path) end |
#delete_content(base_path) ⇒ Object
Delete a content-document from the index by base path.
Content documents are pages on GOV.UK that have a base path and are returned in searches. This excludes best bets, recommended-links, and contacts, which may be deleted with ‘delete_document`.
76 77 78 79 |
# File 'lib/gds_api/rummager.rb', line 76 def delete_content(base_path) request_url = "#{base_url}/content?link=#{base_path}" delete_json(request_url) end |
#delete_content! ⇒ Object
82 83 84 |
# File 'lib/gds_api/rummager.rb', line 82 def delete_content!(*) raise "`Rummager#delete_content!` is deprecated. Use `Rummager#delete_content`" end |
#delete_document(type, id) ⇒ Object
Delete a non-content document from the search index.
For example, best bets, recommended links, or contacts.
110 111 112 113 114 115 |
# File 'lib/gds_api/rummager.rb', line 110 def delete_document(type, id) delete_json( "#{documents_url}/#{id}", _type: type, ) end |
#get_content(base_path) ⇒ Object
Retrieve a content-document from the index.
Content documents are pages on GOV.UK that have a base path and are returned in searches. This excludes best bets, recommended-links, and contacts.
94 95 96 97 |
# File 'lib/gds_api/rummager.rb', line 94 def get_content(base_path) request_url = "#{base_url}/content?link=#{base_path}" get_json(request_url) end |
#get_content! ⇒ Object
100 101 102 |
# File 'lib/gds_api/rummager.rb', line 100 def get_content!(*) raise "`Rummager#get_content!` is deprecated. Use `Rummager#get_content`" end |
#search(args, additional_headers = {}) ⇒ Object
Perform a search.
12 13 14 15 |
# File 'lib/gds_api/rummager.rb', line 12 def search(args, additional_headers = {}) request_url = "#{base_url}/search.json?#{Rack::Utils.build_nested_query(args)}" get_json(request_url, additional_headers) end |
#search_enum(args, page_size: 100, additional_headers: {}) ⇒ Object
Perform a search, returning the results as an enumerator.
The enumerator abstracts away rummager’s pagination and fetches new pages when necessary.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/gds_api/rummager.rb', line 26 def search_enum(args, page_size: 100, additional_headers: {}) Enumerator.new do |yielder| (0..Float::INFINITY).step(page_size).each do |index| search_params = args.merge(start: index.to_i, count: page_size) results = search(search_params, additional_headers).to_h.fetch('results', []) results.each do |result| yielder << result end if results.count < page_size break end end end end |