Module: SearchEnjoy::Searching::ClassMethods

Defined in:
lib/search_enjoy/searching.rb

Instance Method Summary collapse

Instance Method Details

#search(*args) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/search_enjoy/searching.rb', line 72

def search(*args)
  conditions = if args.first.instance_of? Query
                 args.first.query_hash
               else
                 Query.new({**args.first}).query_hash
               end

  previous_result = args.last if args.last.instance_of? QueryResult

  comparator = Comparator.new(conditions)

  source = previous_result.nil? ? search_index.values : previous_result

  result = source.select { |object| comparator.compare(object) }

  QueryResult.new(self, result)
end

#search_must(*args) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/search_enjoy/searching.rb', line 102

def search_must(*args)
  raise SearchException, 'Forbidden use Query in search_must' if args.first.instance_of? Query

  query = Query.new(args.first, must: true)

  result = search(query, args[1..])

  QueryResult.new(self, result)
rescue StandardError => e
  e.message
end

#search_must_not(*args) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/search_enjoy/searching.rb', line 114

def search_must_not(*args)
  raise SearchException, 'Forbidden use Query in search_must_not' if args.first.instance_of? Query

  query = Query.new(args.first, inverse: true)

  result = search(query, args[1..])

  QueryResult.new(self, result)
rescue StandardError => e
  e.message
end

#search_not(*args) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/search_enjoy/searching.rb', line 90

def search_not(*args)
  query = if args.first.instance_of? Query
            args.first.inverse!
          else
            Query.new(args.first, must: true, inverse: true)
          end

  result = search(query, args[1..])

  QueryResult.new(self, result)
end