Class: FullSearch::Search

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, query, filters:, include_soft_deleted:, limit:, offset:, highlight: false, highlight_fields: false) ⇒ Search

Returns a new instance of Search.



7
8
9
10
11
12
13
14
15
16
# File 'lib/full_search/search.rb', line 7

def initialize(model, query, filters:, include_soft_deleted:, limit:, offset:, highlight: false, highlight_fields: false)
  @model = model
  @query = query.to_s.strip
  @filters = filters
  @include_soft_deleted = include_soft_deleted
  @limit = limit
  @offset = offset
  @highlight = highlight
  @highlight_fields = highlight_fields
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



5
6
7
# File 'lib/full_search/search.rb', line 5

def filters
  @filters
end

#highlightObject (readonly)

Returns the value of attribute highlight.



5
6
7
# File 'lib/full_search/search.rb', line 5

def highlight
  @highlight
end

#highlight_fieldsObject (readonly)

Returns the value of attribute highlight_fields.



5
6
7
# File 'lib/full_search/search.rb', line 5

def highlight_fields
  @highlight_fields
end

#include_soft_deletedObject (readonly)

Returns the value of attribute include_soft_deleted.



5
6
7
# File 'lib/full_search/search.rb', line 5

def include_soft_deleted
  @include_soft_deleted
end

#limitObject (readonly)

Returns the value of attribute limit.



5
6
7
# File 'lib/full_search/search.rb', line 5

def limit
  @limit
end

#modelObject (readonly)

Returns the value of attribute model.



5
6
7
# File 'lib/full_search/search.rb', line 5

def model
  @model
end

#offsetObject (readonly)

Returns the value of attribute offset.



5
6
7
# File 'lib/full_search/search.rb', line 5

def offset
  @offset
end

#queryObject (readonly)

Returns the value of attribute query.



5
6
7
# File 'lib/full_search/search.rb', line 5

def query
  @query
end

Instance Method Details

#relationObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/full_search/search.rb', line 18

def relation
  validate_required_filters!

  parsed = QueryParser.parse(query)
  exact_ids = ExactMatch.ids_for(model, query, filters)
  primary_ids = fts_match_ids(parsed)
  fallback_ids = dsl.typo_tolerance? ? trigram_match_ids(parsed, primary_ids) : []

  all_ids = (exact_ids + primary_ids + fallback_ids).uniq
  return model.none if all_ids.empty?

  rel = model.where(id: all_ids)
  rel = rel.where(model.arel_table[dsl.soft_delete_column].eq(nil)) if dsl.soft_delete_column && !include_soft_deleted
  rel = rel.limit(limit) if limit
  rel = rel.offset(offset) if offset

  rel = apply_ranking(rel, all_ids, exact_ids)

  if highlight
    Highlighter.apply!(rel.to_a, model, query)
  elsif highlight_fields
    Highlighter.apply_fields!(rel.to_a, model, query)
  else
    rel
  end
end