Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion app/controllers/admin_request_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ class AdminRequestController < AdminController

def index
@query = params[:query]
@search_engine = params[:search_engine]

if @query
info_requests = InfoRequest.where(["lower(title) like lower('%'||?||'%')", @query])
if @search_engine == 'legacy'
info_requests = InfoRequest.where(
["lower(title) like lower('%'||?||'%')", @query]
)
else
info_requests = InfoRequest.newsearch(@query, limit: 10000)
end
else
info_requests = InfoRequest
end
Expand All @@ -27,6 +35,7 @@ def index

@info_requests =
info_requests.
includes(:embargo, :user, public_body: :translations).
order(sort_query).
paginate(page: params[:page], per_page: 100)
end
Expand Down
96 changes: 89 additions & 7 deletions app/models/concerns/searchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,96 @@
# This would normally not be run beyond the initial indexing of a
# pre-existing database.
def reindex_all(batch_size: 1000)
start = Time.zone.now
count = 0
indexable.find_each(batch_size: batch_size) do |record|
record.reindex
count += 1
opts = Searchable.class_variable_get(:@@searchable_models)[name]
return if opts.nil?

Check warning on line 258 in app/models/concerns/searchable.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Add empty line after guard clause. Raw Output: app/models/concerns/searchable.rb:258:7: C: Layout/EmptyLineAfterGuardClause: Add empty line after guard clause.
all_keys = (opts[:index] || {}).keys + (opts[:admin_index] || {}).keys
# if any of the index keys starts with a '.', we need to call ruby attributes,

Check warning on line 260 in app/models/concerns/searchable.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Line is too long. [84/80] (https://rubystyle.guide#max-line-length) Raw Output: app/models/concerns/searchable.rb:260:81: C: Layout/LineLength: Line is too long. [84/80] (https://rubystyle.guide#max-line-length)
# reindex instance one by one
if all_keys.any?(/^\./)
start = Time.zone.now
count = 0
indexable.find_each(batch_size: batch_size) do |record|
record.reindex
count += 1
end
elapsed = Time.zone.now - start
Rails.logger.info("Reindexed #{count} #{name} in #{elapsed} seconds")
else
reindex_all_inside_db
end
end

# alternative implementation of reindex_all that works for models
# whose searchable.index and searchable.admin_index only contain
# SQL column names, and no ruby attributes. In that case, it is
# possible to send a single request to the db to generate the entire
# set of search_documents.
def reindex_all_inside_db
def partition_table_name(model)

Check warning on line 282 in app/models/concerns/searchable.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Method definitions must not be nested. Use `lambda` instead. (https://rubystyle.guide#no-nested-methods) Raw Output: app/models/concerns/searchable.rb:282:7: W: Lint/NestedMethodDefinition: Method definitions must not be nested. Use `lambda` instead. (https://rubystyle.guide#no-nested-methods)
"search_documents_#{model.downcase.gsub('::', '_')}"
end
elapsed = Time.zone.now - start
Rails.logger.info("Reindexed #{count} #{name} in #{elapsed} seconds")

def raw_content_query(idx_name)

Check warning on line 286 in app/models/concerns/searchable.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Method definitions must not be nested. Use `lambda` instead. (https://rubystyle.guide#no-nested-methods) Raw Output: app/models/concerns/searchable.rb:286:7: W: Lint/NestedMethodDefinition: Method definitions must not be nested. Use `lambda` instead. (https://rubystyle.guide#no-nested-methods)
opts = Searchable.class_variable_get(:@@searchable_models)[model_name.name]

Check warning on line 287 in app/models/concerns/searchable.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Line is too long. [83/80] (https://rubystyle.guide#max-line-length) Raw Output: app/models/concerns/searchable.rb:287:81: C: Layout/LineLength: Line is too long. [83/80] (https://rubystyle.guide#max-line-length)
if opts[idx_name].nil?
"''"
else
"concat(#{opts[idx_name].keys.join(', \' \', ')})"
end
end

def content_tsv_query(idx_name, language)

Check warning on line 295 in app/models/concerns/searchable.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Method definitions must not be nested. Use `lambda` instead. (https://rubystyle.guide#no-nested-methods) Raw Output: app/models/concerns/searchable.rb:295:7: W: Lint/NestedMethodDefinition: Method definitions must not be nested. Use `lambda` instead. (https://rubystyle.guide#no-nested-methods)
opts = Searchable.class_variable_get(:@@searchable_models)[model_name.name]

Check warning on line 296 in app/models/concerns/searchable.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Line is too long. [83/80] (https://rubystyle.guide#max-line-length) Raw Output: app/models/concerns/searchable.rb:296:81: C: Layout/LineLength: Line is too long. [83/80] (https://rubystyle.guide#max-line-length)
if opts[idx_name].nil?
"''"
else
bits = opts[idx_name].keys.map { |c|
"setweight(to_tsvector('#{language}'::regconfig, unaccent(coalesce(#{c}, ''))), '#{opts[idx_name][c]}')"

Check warning on line 301 in app/models/concerns/searchable.rb

View workflow job for this annotation

GitHub Actions / build

[rubocop] reported by reviewdog 🐶 Line is too long. [116/80] (https://rubystyle.guide#max-line-length) Raw Output: app/models/concerns/searchable.rb:301:81: C: Layout/LineLength: Line is too long. [116/80] (https://rubystyle.guide#max-line-length)
}
bits.join('||')
end
end

language = Searchable.lang_from_locale(
AlaveteliLocalization.default_locale
)
start = Time.now
query = <<-SQL
INSERT INTO "#{partition_table_name(model_name.name)}" (
"searchable_type",
"searchable_id",
"language",
"section_ref",
"raw_content",
"raw_admin_content",
"content_tsv",
"admin_content_tsv",
"created_at",
"updated_at"
)
SELECT
'#{model_name.name}',
id,
'#{language}',
'1',
#{raw_content_query(:index)},
#{raw_content_query(:admin_index)},
#{content_tsv_query(:index, language)},
#{content_tsv_query(:admin_index, language)},
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP
FROM #{table_name}
SQL

Rails.logger.debug(query)
ActiveRecord::Base.connection.exec_query(
"TRUNCATE #{partition_table_name(model_name.name)}"
)
ActiveRecord::Base.connection.exec_query(query)
t = Time.now - start
Rails.logger.info(
"Reindexed #{indexable.count} #{name} in #{t} seconds (in database)"
)
end
end

Expand Down
11 changes: 11 additions & 0 deletions app/models/info_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class InfoRequest < ApplicationRecord
include LinkToHelper

include Categorisable
include Searchable
include Taggable
include Notable
include RateLimited
Expand Down Expand Up @@ -212,6 +213,16 @@ class << self
before_destroy :expire
after_destroy :notify_associations, :update_counter_cache

searchable index: {
"title": "A",
"url_title": "B",
Comment thread
garethrees marked this conversation as resolved.
"prominence_reason": "D"
},
admin_index: {
"external_user_name": "A",
"external_url": "A"
}

# Return info request corresponding to an incoming email address, or nil if
# none found. Checks the hash to ensure the email came from the public body -
# only they are sent the email address with the has in it. (We don't check
Expand Down
20 changes: 17 additions & 3 deletions app/views/admin_request/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,25 @@

<%= form_tag({}, method: 'get', class: 'form form-search') do %>
<div class="input-append">
<%= text_field_tag 'query', params[:query], size: 30, class: 'input-large search-query' %>
<%= submit_tag 'Search', class: 'btn' %>
<%= text_field_tag "query",
params[:query],
size: 30,
class: "input-large search-query" %>
<%= submit_tag "Search", class: "btn" %>
</div>

<span class="help-inline">(substring search, titles only)</span>
<div>
<%= radio_button_tag "search_engine", "legacy", checked: "legacy" == @search_engine %>
<%= label_tag "search_engine",
"legacy engine: substring search in titles only",
class: "help-inline" %>
<%= radio_button_tag "search_engine",
"new",
checked: (@search_engine ? "new" == @search_engine : true) %>
<%= label_tag "search_engine",
"new engine: please report poor search results!",
class: "help-inline" %>
</div>
<% end %>

<%= render partial: 'some_requests', locals: { info_requests: @info_requests } %>
4 changes: 2 additions & 2 deletions spec/models/concerns/searchable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
end

it 'does not index models that are not registered as searchable' do
body = FactoryBot.create(:initial_request)
expect(SearchDocument.where(searchable_type: "InfoRequest").count).to eq(0)
n = FactoryBot.create(:notification)
expect(SearchDocument.where(searchable_type: "Notification").count).to eq(0)
end

describe '.reindex_all' do
Expand Down
Loading