Class: GdsApi::Search

Inherits:
Base
  • Object
show all
Defined in:
lib/gds_api/search.rb

Defined Under Namespace

Classes: UnknownAPIVersion, V1, V2

Constant Summary collapse

DEFAULT_API_VERSION =
"V1".freeze
API_VERSIONS =
{
  "V1" => GdsApi::Search::V1,
  "V2" => GdsApi::Search::V2,
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#client, #create_client, #get_list, #url_for_slug

Constructor Details

#initialize(endpoint_url, options = {}) ⇒ Search

Returns a new instance of Search.



57
58
59
60
61
62
63
64
# File 'lib/gds_api/search.rb', line 57

def initialize(endpoint_url, options = {})
  super
  # The API version provides a simple wrapper around this base class so that we
  # can still access the shared methods present in this class.
  version = options.fetch(:api_version, DEFAULT_API_VERSION)
  api_class = API_VERSIONS[version] || raise(UnknownAPIVersion)
  @api = api_class.new(self)
end

Instance Method Details

#add_document(*args) ⇒ GdsApi::Response

Add a document to the search index.

Parameters:

  • type (String)

    The search-api document type.

  • id (String)

    The search-api/elasticsearch id. Typically the same as the ‘link` field, but this is not strictly enforced.

  • document (Hash)

    The document to add. Must match the search-api schema matching the ‘type` parameter and contain a `link` field.

  • index_name (V2 only)

    Name of the index to be deleted from on GOV.UK - we only allow deletion from metasearch

Returns:

  • (GdsApi::Response)

    A status code of 202 indicates the document has been successfully queued.

See Also:



124
125
126
# File 'lib/gds_api/search.rb', line 124

def add_document(*args)
  @api.add_document(*args)
end

#base_urlObject



166
167
168
# File 'lib/gds_api/search.rb', line 166

def base_url
  endpoint
end

#batch_search(searches, additional_headers = {}) ⇒ Object

Parameters:

  • searches (Array)

    An array valid search queries. Maximum of 6. See search-api documentation for options.



81
82
83
84
85
86
87
88
# File 'lib/gds_api/search.rb', line 81

def batch_search(searches, additional_headers = {})
  url_friendly_searches = searches.each_with_index.map do |search, index|
    { index => search }
  end
  searches_query = { search: url_friendly_searches }
  request_url = "#{base_url}/batch_search.json?#{Rack::Utils.build_nested_query(searches_query)}"
  get_json(request_url, additional_headers)
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`.

Parameters:

  • base_path

    Base path of the page on GOV.UK.

See Also:



136
137
138
139
# File 'lib/gds_api/search.rb', line 136

def delete_content(base_path)
  request_url = "#{base_url}/content?link=#{base_path}"
  delete_json(request_url)
end

#delete_document(*args) ⇒ Object

Delete a non-content document from the search index.

For example, best bets, recommended links, or contacts.

Parameters:

  • type (String)

    The search-api document type.

  • id (String)

    The search-api/elasticsearch id. Typically the same as the ‘link` field.

  • index_name (V2 only)

    Name of the index to be deleted from on GOV.UK - we only allow deletion from metasearch



162
163
164
# File 'lib/gds_api/search.rb', line 162

def delete_document(*args)
  @api.delete_document(*args)
end

#documents_urlObject



170
171
172
# File 'lib/gds_api/search.rb', line 170

def documents_url
  "#{base_url}/documents"
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.

Parameters:

  • base_path (String)

    Base path of the page on GOV.UK.

See Also:



149
150
151
152
# File 'lib/gds_api/search.rb', line 149

def get_content(base_path)
  request_url = "#{base_url}/content?link=#{base_path}"
  get_json(request_url)
end

#search(args, additional_headers = {}) ⇒ Object

Perform a search.

Parameters:

  • args (Hash)

    A valid search query. See search-api documentation for options.

See Also:



71
72
73
74
# File 'lib/gds_api/search.rb', line 71

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 search-api’s pagination and fetches new pages when necessary.

Parameters:

  • args (Hash)

    A valid search query. See search-api documentation for options.

  • page_size (Integer) (defaults to: 100)

    Number of results in each page.

See Also:



99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/gds_api/search.rb', line 99

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