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



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/full_search/dsl.rb', line 83

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" : "source", f.reindex_on, f.async] },
    exact_matches.map { |e| [e.name] },
    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



29
30
31
32
33
34
# File 'lib/full_search/dsl.rb', line 29

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) ⇒ Object



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

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

#filter(name, required: false) ⇒ Object



43
44
45
46
47
48
# File 'lib/full_search/dsl.rb', line 43

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



66
67
68
# File 'lib/full_search/dsl.rb', line 66

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



36
37
38
39
40
41
# File 'lib/full_search/dsl.rb', line 36

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



50
51
52
53
54
55
56
# File 'lib/full_search/dsl.rb', line 50

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



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

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)


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

def typo_tolerance?
  !!@typo_tolerance
end

#typo_tolerance_min_term_lengthObject



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

def typo_tolerance_min_term_length
  @typo_tolerance_min_term_length || 3
end