Class: FullSearch::Dsl

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

Defined Under Namespace

Classes: ExactMatch, Field, Filter, RankBy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ Dsl

Returns a new instance of Dsl.



12
13
14
15
16
17
18
19
20
# File 'lib/full_search/dsl.rb', line 12

def initialize(model_class)
  @model_class = model_class
  @fields = []
  @exact_matches = []
  @filters = []
  @rank_bys = []
  @tokenize = FullSearch.config.default_tokenizer
  @soft_delete_column = nil
end

Instance Attribute Details

#exact_matchesObject (readonly)

Returns the value of attribute exact_matches.



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

def exact_matches
  @exact_matches
end

#fieldsObject (readonly)

Returns the value of attribute fields.



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

def fields
  @fields
end

#filtersObject (readonly)

Returns the value of attribute filters.



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

def filters
  @filters
end

#highlight_configObject (readonly)

Returns the value of attribute highlight_config.



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

def highlight_config
  @highlight_config
end

#model_classObject (readonly)

Returns the value of attribute model_class.



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

def model_class
  @model_class
end

#rank_bysObject (readonly)

Returns the value of attribute rank_bys.



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

def rank_bys
  @rank_bys
end

#tokenize(value = :_no_arg_) ⇒ Object (readonly)

Returns the value of attribute tokenize.



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

def tokenize
  @tokenize
end

Instance Method Details

#config_hashObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/full_search/dsl.rb', line 86

def config_hash
  require "digest"
  Digest::SHA256.hexdigest([
    model_class.table_name,
    tokenize,
    soft_delete_column,
    typo_tolerance?,
    typo_tolerance_min_term_length,
    fields.map { |f| [f.name, f.weight, f.source.nil? ? "column" : f.source.source_location&.join(":"), f.reindex_on, f.async, f.as] },
    exact_matches.map { |e| [e.name, e.source&.source_location&.join(":")] },
    filters.map { |f| [f.name, f.required] },
    rank_bys.map { |r| [r.column, r.direction] }
  ].inspect)
end

#exact_match(name, source: -> { public_send(name) }) ⇒ Object



32
33
34
35
36
37
# File 'lib/full_search/dsl.rb', line 32

def exact_match(name, source: -> { public_send(name) })
  unless valid_name?(name)
    raise InvalidFieldError, "Invalid exact_match name: #{name.inspect}"
  end
  @exact_matches << ExactMatch.new(name: name.to_s, source: source)
end

#field(name, weight: 1, source: nil, reindex_on: nil, async: FullSearch.config.default_async_reindex, as: nil) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/full_search/dsl.rb', line 22

def field(name, weight: 1, source: nil, reindex_on: nil, async: FullSearch.config.default_async_reindex, as: nil)
  unless valid_name?(name)
    raise InvalidFieldError, "Invalid field name: #{name.inspect}"
  end
  if as && !valid_name?(as)
    raise InvalidFieldError, "Invalid field alias (as): #{as.inspect}"
  end
  @fields << Field.new(name: name.to_s, weight: weight.to_i, source: source, reindex_on: reindex_on&.to_s, async: async, as: as&.to_s)
end

#filter(name, required: false) ⇒ Object



46
47
48
49
50
51
# File 'lib/full_search/dsl.rb', line 46

def filter(name, required: false)
  unless valid_name?(name)
    raise InvalidFieldError, "Invalid filter name: #{name.inspect}"
  end
  @filters << Filter.new(name: name.to_s, required: required)
end

#highlight(open_tag: "<mark>", close_tag: "</mark>") ⇒ Object



69
70
71
# File 'lib/full_search/dsl.rb', line 69

def highlight(open_tag: "<mark>", close_tag: "</mark>")
  @highlight_config = { open_tag: open_tag, close_tag: close_tag }
end

#rank_by(column, direction = :desc) ⇒ Object



39
40
41
42
43
44
# File 'lib/full_search/dsl.rb', line 39

def rank_by(column, direction = :desc)
  unless valid_name?(column)
    raise InvalidFieldError, "Invalid rank_by column: #{column.inspect}"
  end
  @rank_bys << RankBy.new(column: column.to_s, direction: direction.to_sym)
end

#soft_delete_column(name = :_no_arg_) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/full_search/dsl.rb', line 53

def soft_delete_column(name = :_no_arg_)
  if name == :_no_arg_
    @soft_delete_column
  else
    @soft_delete_column = name.to_s
  end
end

#typo_tolerance(enabled = true, min_term_length: nil) ⇒ Object



73
74
75
76
# File 'lib/full_search/dsl.rb', line 73

def typo_tolerance(enabled = true, min_term_length: nil)
  @typo_tolerance = enabled
  @typo_tolerance_min_term_length = min_term_length || 3
end

#typo_tolerance?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/full_search/dsl.rb', line 78

def typo_tolerance?
  !!@typo_tolerance
end

#typo_tolerance_min_term_lengthObject



82
83
84
# File 'lib/full_search/dsl.rb', line 82

def typo_tolerance_min_term_length
  @typo_tolerance_min_term_length || 3
end