Skip to content

Commit

Permalink
- Refactor Solr query class & add SolrMultiQuery class to handle gett…
Browse files Browse the repository at this point in the history
…ing multiple objects from a set of URIs (as in the case of related items). This avoids performance problems by submitting only a single query to Solr regardless of the number of related items an object has.

- Add some basic versions of a few objects we haven't come across yet which are cropping up as related item object types
- Update related items partial to match wireframes & include data from related items Solr query
  • Loading branch information
j-corry committed Nov 13, 2023
1 parent 4bdc60e commit b56bf0e
Show file tree
Hide file tree
Showing 15 changed files with 176 additions and 30 deletions.
2 changes: 1 addition & 1 deletion app/controllers/content_objects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def index

def show
# We get the object URI passed as a parameter and pass to a new instance of ApiCall
object_data = ApiCall.new(object_uri: params[:object]).object_data
object_data = SolrQuery.new(object_uri: params[:object]).object_data

# TODO: handle other error types
if object_data['statusCode'] == 500
Expand Down
8 changes: 7 additions & 1 deletion app/helpers/link_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ def ses_object_name(ses_id)
# used where the object type is dynamic but we don't actually want a link
# e.g. secondary information title

@ses_data[ses_id.to_i]&.singularize.downcase
@ses_data[ses_id.to_i]&.singularize&.downcase
end

def ses_name(ses_id)
# used where we want an as-is SES name but without a link
# TODO: refactor into main helper method using optional parameter?
display_name(@ses_data[ses_id.to_i])
end

def display_name(ses_name)
Expand Down
13 changes: 0 additions & 13 deletions app/models/api_call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,11 @@ class ApiCall
attr_reader :object_uri

BASE_API_URI = "https://api.parliament.uk/new-solr/"
# BASE_API_URI = "https://api.parliament.uk/search-mock/"
# BASE_API_URI = "http://localhost:3000/search-mock/"

def initialize(params)
@object_uri = params[:object_uri]
end

def object_data
return evaluated_response if evaluated_response['statusCode'] == 500

evaluated_response['response']['docs'].first
end

def ruby_uri
# build_uri("#{BASE_API_URI}objects.json?object=#{object_uri}")
build_uri("#{BASE_API_URI}select?q=uri:%22#{object_uri}%22")
end

private

def response_body
Expand Down
14 changes: 14 additions & 0 deletions app/models/command_paper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CommandPaper < ContentObject

def initialize(content_object_data)
super
end

def template
'search/objects/command_paper'
end

def object_name
'command paper'
end
end
15 changes: 15 additions & 0 deletions app/models/committee_proceeding.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CommitteeProceeding < ContentObject

def initialize(content_object_data)
super
end

def template
'search/objects/committee_proceeding'
end

def object_name
'committee proceeding'
end

end
32 changes: 26 additions & 6 deletions app/models/content_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,16 +198,36 @@ def notes
content_object_data['searcherNote_t'].first
end

def related_items
def old_related_items
# based on provided information, this will return one or more URIs of related item object pages

# if relation_t and relation_uri, gets a related item
# fields might be inconsistent
# link through to the item landing page
relation_uris = content_object_data['relation_t']

return if relation_uris.blank?

relation_objects = []

return if content_object_data['relation_t'].blank?
relation_uris.each do |relation_uri|
relation_data = SolrQuery.new(object_uri: relation_uri).object_data
relation_object = ContentObject.generate(relation_data)
relation_objects << relation_object
end

relation_objects
end

def related_items
relation_uris = content_object_data['relation_t']
return if relation_uris.blank?

related_objects = SolrMultiQuery.new(object_uris: relation_uris).object_data

ret = []
related_objects.each do |object|
ret << ContentObject.generate(object)
end

content_object_data['relation_t']
ret
end

def parliamentary_session
Expand Down
15 changes: 15 additions & 0 deletions app/models/european_deposited_document.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class EuropeanDepositedDocument < ContentObject

def initialize(content_object_data)
super
end

def template
'search/objects/european_deposited_document'
end

def object_name
'european deposited document'
end

end
15 changes: 15 additions & 0 deletions app/models/european_material.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class EuropeanMaterial < ContentObject

def initialize(content_object_data)
super
end

def template
'search/objects/european_material'
end

def object_name
'european material'
end

end
14 changes: 14 additions & 0 deletions app/models/house_of_commons_paper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class HouseOfCommonsPaper < ContentObject

def initialize(content_object_data)
super
end

def template
'search/objects/house_of_commons_paper'
end

def object_name
'house of commons paper'
end
end
2 changes: 1 addition & 1 deletion app/models/oral_answer_to_question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def question_url
def question_object
return unless has_question?

question_data = ApiCall.new(object_uri: question_url).object_data
question_data = SolrQuery.new(object_uri: question_url).object_data
ContentObject.generate(question_data)
end
end
2 changes: 1 addition & 1 deletion app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def correcting_object

return if content_object_data['correctingItem_uri'].blank?

correcting_item_data = ApiCall.new(object_uri: content_object_data['correctingItem_uri']).object_data
correcting_item_data = SolrQuery.new(object_uri: content_object_data['correctingItem_uri']).object_data
ContentObject.generate(correcting_item_data)
end

Expand Down
15 changes: 15 additions & 0 deletions app/models/research_material.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class ResearchMaterial < ContentObject

def initialize(content_object_data)
super
end

def template
'search/objects/research_material'
end

def object_name
'research material'
end

end
33 changes: 33 additions & 0 deletions app/models/solr_multi_query.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class SolrMultiQuery < ApiCall
# for fetching related item data etc.

require 'open-uri'
require 'net/http'

attr_reader :object_uris

BASE_API_URI = "https://api.parliament.uk/new-solr/"

def initialize(params)
@object_uris = params[:object_uris]
end

def object_data
return evaluated_response if evaluated_response['statusCode'] == 500

evaluated_response['response']['docs']
end

def ruby_uri
# this constructs q=uri: "www.google.com" OR uri: "www.apple.com" OR ...

uri = build_uri("#{BASE_API_URI}select?q=#{search_string}&rows=50")

# TODO: this currently returns the first 50 results; this should be set to a sensible number?
end

def search_string
query = object_uris.join("%22 OR uri:%22")
"(uri:%22#{query}%22)"
end
end
4 changes: 0 additions & 4 deletions app/models/solr_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ class SolrQuery < ApiCall

attr_reader :object_uri

# BASE_API_URI = "https://api.parliament.uk/search-mock/"
# BASE_API_URI = "http://localhost:3000/search-mock/"
BASE_API_URI = "https://api.parliament.uk/new-solr/"

def initialize(params)
Expand All @@ -19,8 +17,6 @@ def object_data
end

def ruby_uri
# build_uri("#{BASE_API_URI}objects.json?object=#{object_uri}")

build_uri("#{BASE_API_URI}select?q=uri:%22#{object_uri}%22")
end
end
22 changes: 19 additions & 3 deletions app/views/search/fragments/_related_items.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
<div class="about-item">
<h3 class="content-heading" id="related-items">Related items</h3>

<!-- relation_t or relation_uri-->
<!--we can only provide further information where there's a uri to follow-->
<!-- however it's further complicated by relation_t containing uris-->
<!-- if we have the uri, we can also add date_dt, type_ses and legislature_ses-->

<% related_items.each do |related_item| %>
<div>
<%= link_to related_item, '/' %><br/>
</div>
<div>
<%= link_to related_item.page_title, related_item.content_object_data['uri'] %><br/>
<% if related_item.date %>
<%= related_item.date&.strftime(ApplicationHelper::DATE_DISPLAY_FORMAT) %>
<% end %>
<% if related_item.type %>
<%= ses_name(related_item.type) %><br/>
<% end %>
<% if related_item.legislature %>
<%= ses_name(related_item.legislature) %><br/>
<% end %>
</div>
<br/>
<% end %>
</div>

0 comments on commit b56bf0e

Please sign in to comment.