Class: Noiseless::Model

Inherits:
Object
  • Object
show all
Extended by:
DSL::ClassMethods
Defined in:
lib/noiseless/model.rb

Direct Known Subclasses

ApplicationSearch

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL::ClassMethods

adapter, bulk_importer, connection, import, import_scoped, index_name, mapping, multi_search, page, per, reindex, search_index, searchable, searchable_fields

Constructor Details

#initializeModel

Returns a new instance of Model.



7
8
9
# File 'lib/noiseless/model.rb', line 7

def initialize
  @builder = QueryBuilder.new(self.class)
end

Class Method Details

.search(indexes: nil, connection: nil, response_type: nil) {|builder| ... } ⇒ Object

Yields:

  • (builder)


11
12
13
14
15
16
17
18
# File 'lib/noiseless/model.rb', line 11

def self.search(indexes: nil, connection: nil, response_type: nil)
  client = Noiseless.connections.client(connection || self.connection)
  builder = QueryBuilder.new(self)
  builder.indexes(indexes) if indexes
  yield(builder)
  ast = builder.to_ast
  client.search(ast, model_class: self, response_type: response_type)
end

.search_sync(indexes: nil, connection: nil, response_type: nil) ⇒ Object



20
21
22
23
24
# File 'lib/noiseless/model.rb', line 20

def self.search_sync(indexes: nil, connection: nil, response_type: nil, &)
  Sync do
    search(indexes: indexes, connection: connection, response_type: response_type, &).wait
  end
end

Instance Method Details

#aggregation(name, type) ⇒ Object



67
68
69
70
# File 'lib/noiseless/model.rb', line 67

def aggregation(name, type, **)
  @builder.aggregation(name, type, **)
  self
end

#execute(connection: nil, response_type: nil) ⇒ Object



97
98
99
100
101
# File 'lib/noiseless/model.rb', line 97

def execute(connection: nil, response_type: nil)
  client = Noiseless.connections.client(connection || self.class.connection)
  ast = @builder.to_ast
  client.search(ast, model_class: self.class, response_type: response_type)
end

#execute_sync(connection: nil, response_type: nil) ⇒ Object



103
104
105
106
107
# File 'lib/noiseless/model.rb', line 103

def execute_sync(connection: nil, response_type: nil)
  Sync do
    execute(connection: connection, response_type: response_type).wait
  end
end

#facet_sample_slope(value) ⇒ Object



87
88
89
90
# File 'lib/noiseless/model.rb', line 87

def facet_sample_slope(value)
  @builder.facet_sample_slope(value)
  self
end

#filter(field, value) ⇒ Object



37
38
39
40
# File 'lib/noiseless/model.rb', line 37

def filter(field, value, **)
  @builder.filter(field, value, **)
  self
end

#geo_distance(field, lat:, lon:, distance:) ⇒ Object



72
73
74
75
# File 'lib/noiseless/model.rb', line 72

def geo_distance(field, lat:, lon:, distance:, **)
  @builder.geo_distance(field, lat: lat, lon: lon, distance: distance, **)
  self
end

#indexes(names) ⇒ Object



77
78
79
80
# File 'lib/noiseless/model.rb', line 77

def indexes(names)
  @builder.indexes(names)
  self
end

#limit(size) ⇒ Object



52
53
54
55
# File 'lib/noiseless/model.rb', line 52

def limit(size)
  @builder.limit(size)
  self
end

#match(field, value) ⇒ Object

Instance methods that delegate to the query builder



27
28
29
30
# File 'lib/noiseless/model.rb', line 27

def match(field, value, **)
  @builder.match(field, value, **)
  self
end

#multi_match(query, fields) ⇒ Object



32
33
34
35
# File 'lib/noiseless/model.rb', line 32

def multi_match(query, fields, **)
  @builder.multi_match(query, fields, **)
  self
end

#offset(from) ⇒ Object



57
58
59
60
# File 'lib/noiseless/model.rb', line 57

def offset(from)
  @builder.offset(from)
  self
end

#paginate(page: nil, per_page: nil) ⇒ Object



62
63
64
65
# File 'lib/noiseless/model.rb', line 62

def paginate(page: nil, per_page: nil)
  @builder.paginate(page: page, per_page: per_page)
  self
end

#pinned_hits(value) ⇒ Object



92
93
94
95
# File 'lib/noiseless/model.rb', line 92

def pinned_hits(value)
  @builder.pinned_hits(value)
  self
end

#range(field, gte: nil, lte: nil, gt: nil, lt: nil) ⇒ Object



42
43
44
45
# File 'lib/noiseless/model.rb', line 42

def range(field, gte: nil, lte: nil, gt: nil, lt: nil)
  @builder.range(field, gte: gte, lte: lte, gt: gt, lt: lt)
  self
end

#remove_duplicates(value: true) ⇒ Object



82
83
84
85
# File 'lib/noiseless/model.rb', line 82

def remove_duplicates(value: true)
  @builder.remove_duplicates(value: value)
  self
end

#sort(field, direction = :asc) ⇒ Object



47
48
49
50
# File 'lib/noiseless/model.rb', line 47

def sort(field, direction = :asc, **)
  @builder.sort(field, direction, **)
  self
end