Class: Lcms::Engine::Search::Repository

Inherits:
Object
  • Object
show all
Includes:
Elasticsearch::Persistence::Repository, Elasticsearch::Persistence::Repository::DSL
Defined in:
app/models/lcms/engine/search/repository.rb

Constant Summary collapse

SYNONYMS =
{
  'text sets' => 'text set',
  'expert pack' => 'expert packs'
}.freeze

Instance Method Summary collapse

Instance Method Details

#accepted_filtersObject



185
186
187
# File 'app/models/lcms/engine/search/repository.rb', line 185

def accepted_filters
  %i(model_type subject grade doc_type)
end

#all_query(options) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/lcms/engine/search/repository.rb', line 83

def all_query(options)
  limit = options.fetch(:per_page, 20)
  page = options.fetch(:page, 1)

  query = {
    query: {
      bool: {
        must: { match_all: {} },
        filter: []
      }
    },
    sort: [
      { subject: 'asc' },
      { position: 'asc' },
      { 'title.key' => 'asc' }
    ],
    size: limit,
    from: (page - 1) * limit
  }

  apply_filters(query, options)
end

#apply_filters(query, options) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'app/models/lcms/engine/search/repository.rb', line 189

def apply_filters(query, options)
  accepted_filters.each do |filter|
    next unless options[filter]

    filter_term = if options[filter].is_a? Array
                    { terms: { filter => options[filter] } }
                  else
                    { match: { filter => { query: options[filter] } } }
                  end
    query[:query][:bool][:filter] << filter_term
  end
  query
end

#empty_responseObject



203
204
205
206
207
208
# File 'app/models/lcms/engine/search/repository.rb', line 203

def empty_response
  Elasticsearch::Persistence::Repository::Response::Results.new(
    self,
    hits: { total: 0, max_score: nil, hits: [] }
  )
end

#fts_query(term, options) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/lcms/engine/search/repository.rb', line 106

def fts_query(term, options)
  return term if term.respond_to?(:to_hash)

  limit = options.fetch(:per_page, 20)
  page = options.fetch(:page, 1)
  term = replace_synonyms term.downcase

  query = {
    min_score: 6,
    query: {
      bool: {
        should: [
          { match: { 'title.full' => { query: term, boost: 3, type: 'phrase' } } },
          { match: { 'title.partial' => { query: term, boost: 0.5 } } },

          { match: { 'teaser.full' => { query: term, boost: 4, type: 'phrase' } } },

          { match: { document_metadata: { query: term, boost: 1 } } }
        ],
        filter: []
      }
    },
    size: limit,
    from: (page - 1) * limit
  }

  apply_filters(query, options)
end

#multisearch(queries) ⇒ Object



210
211
212
213
214
215
# File 'app/models/lcms/engine/search/repository.rb', line 210

def multisearch(queries)
  body = queries.map { |query| { search: query } }
  client.msearch(index: index, type: type, body: body)['responses'].map do |r|
    Elasticsearch::Persistence::Repository::Response::Results.new(self, r)
  end
end

#replace_synonyms(term) ⇒ Object



217
218
219
# File 'app/models/lcms/engine/search/repository.rb', line 217

def replace_synonyms(term)
  SYNONYMS[term] || term
end

#standards_query(term, options) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'app/models/lcms/engine/search/repository.rb', line 135

def standards_query(term, options)
  return term if term.respond_to?(:to_hash)

  limit = options.fetch(:per_page, 20)
  page = options.fetch(:page, 1)

  query = {
    query: {
      bool: {
        filter: [],
        should: [
          { term: { tag_standards: term } },
          { match_phrase_prefix: { tag_standards: { query: term } } }
        ],
        minimum_should_match: 1
      }
    },
    size: limit,
    from: (page - 1) * limit
  }

  apply_filters query, options
end

#tags_query(term, tags, options) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'app/models/lcms/engine/search/repository.rb', line 159

def tags_query(term, tags, options)
  return term if term.respond_to?(:to_hash)

  limit = options.fetch(:per_page, 20)
  page = options.fetch(:page, 1)

  query = {
    query: {
      bool: {
        filter: [],
        should: tags.map { |t| { match: { t => term } } }.concat(
          [
            { match_phrase: { title: term } },
            { match_phrase: { teaser: term } }
          ]
        ),
        minimum_should_match: 1
      }
    },
    size: limit,
    from: (page - 1) * limit
  }

  apply_filters query, options
end