Skip to content

Commit ab6530f

Browse files
committed
Rubocop fixes
1 parent cbbc5fa commit ab6530f

File tree

4 files changed

+48
-32
lines changed

4 files changed

+48
-32
lines changed

app/controllers/better_together/search_controller.rb

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,58 @@
33
module BetterTogether
44
# Handles dispatching search queries to elasticsearch and displaying the results
55
class SearchController < ApplicationController
6-
def search
6+
def search # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
77
searchable_models = BetterTogether::Searchable.included_in_models
88
@query = params[:q]
99
search_results = []
1010
suggestions = []
1111

1212
if @query.present?
13-
response = Elasticsearch::Model.search({
14-
query: {
15-
bool: {
16-
must: [
17-
{
18-
multi_match: {
19-
query: @query,
20-
fields: ['title^3', 'content', 'blocks.rich_text_content.body^2', 'formatted_address^2', 'name^3', 'description^2'],
21-
type: 'best_fields'
22-
}
23-
}
24-
]
25-
}
26-
},
27-
suggest: {
28-
text: @query,
29-
suggestions: {
30-
term: {
31-
field: 'name',
32-
suggest_mode: 'always'
33-
}
34-
}
35-
}
36-
}, searchable_models)
13+
response = Elasticsearch::Model.search(build_search_query(@query), searchable_models)
3714

3815
search_results = response.records.to_a
39-
suggestions = response.response['suggest']['suggestions'].map { |s| s['options'].map { |o| o['text'] } }.flatten
16+
suggestions = response.response['suggest']['suggestions'].map do |s|
17+
s['options'].map do |o|
18+
o['text']
19+
end
20+
end.flatten
4021
end
4122

4223
# Use Kaminari for pagination
4324
@results = Kaminari.paginate_array(search_results).page(params[:page]).per(10)
4425
@suggestions = suggestions
4526
end
4627

28+
private
29+
30+
def build_search_query(query) # rubocop:todo Metrics/MethodLength
31+
{
32+
query: {
33+
bool: {
34+
must: [
35+
{
36+
multi_match: {
37+
query: query,
38+
fields: [
39+
'title^3', 'content', 'blocks.rich_text_content.body^2',
40+
'formatted_address^2', 'name^3', 'description^2'
41+
],
42+
type: 'best_fields'
43+
}
44+
}
45+
]
46+
}
47+
},
48+
suggest: {
49+
text: query,
50+
suggestions: {
51+
term: {
52+
field: 'name',
53+
suggest_mode: 'always'
54+
}
55+
}
56+
}
57+
}
58+
end
4759
end
4860
end

app/jobs/better_together/elasticsearch_index_job.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
# frozen_string_literal: true
12

23
module BetterTogether
4+
# Queues up Elasticsearch indexing jobs in the background
5+
# This job is responsible for indexing and deleting documents in Elasticsearch
6+
# when records are created, updated, or destroyed.
37
class ElasticsearchIndexJob < ApplicationJob
48
queue_as :default
59

app/models/better_together/content/block.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ def to_s
8181
end}"
8282
end
8383

84-
# Method to return the content used for Elasticsearch indexing
85-
def cached_content
84+
# Method to return the content used for Elasticsearch indexing
85+
def cached_content
8686
{
8787
id: id,
8888
type: type,

app/models/concerns/better_together/searchable.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ def self.refresh_elastic_index!
2828

2929
# Need to create another way to access elasticsearch import.
3030
# class.import is using by activerecord-import, I think
31-
def self.elastic_import args={}
31+
def self.elastic_import(args = {})
3232
__elasticsearch__.import(args) unless Rails.env.test?
3333
end
3434
end
3535

3636
class_methods do
37-
def default_elasticsearch_index
37+
def default_elasticsearch_index # rubocop:todo Metrics/MethodLength
3838
{
3939
number_of_shards: 1,
4040
analysis: {
@@ -43,13 +43,13 @@ def default_elasticsearch_index
4343
type: 'edge_ngram',
4444
min_gram: 2,
4545
max_gram: 20,
46-
token_chars: ['letter', 'digit']
46+
token_chars: %w[letter digit]
4747
}
4848
},
4949
analyzer: {
5050
custom_analyzer: {
5151
tokenizer: 'edge_ngram_tokenizer',
52-
filter: ['lowercase', 'asciifolding']
52+
filter: %w[lowercase asciifolding]
5353
}
5454
}
5555
}

0 commit comments

Comments
 (0)