Module: Lycra::Search
- Defined in:
- lib/lycra/search.rb,
lib/lycra/search/sort.rb,
lib/lycra/search/query.rb,
lib/lycra/search/filters.rb,
lib/lycra/search/scoping.rb,
lib/lycra/search/enumerable.rb,
lib/lycra/search/pagination.rb,
lib/lycra/search/aggregations.rb
Defined Under Namespace
Modules: ClassMethods, Enumerable, Pagination, Scoping
Classes: Aggregations, Filters, Query, Sort
Class Method Summary
collapse
Instance Method Summary
collapse
-
#document_types ⇒ Object
-
#entry_name ⇒ Object
-
#fields(flds = false) ⇒ Object
-
#initialize(term = nil, query: nil, filter: nil, post_filter: nil, sort: nil, models: nil, fields: nil, aggregations: nil, &block) ⇒ Object
-
#match_all_query ⇒ Object
-
#match_phrase_prefix_query ⇒ Object
-
#models(mdls = false) ⇒ Object
-
#multi_match_query ⇒ Object
-
#query(qry = nil) ⇒ Object
-
#query_method(meth = false) ⇒ Object
-
#response ⇒ Object
-
#response! ⇒ Object
-
#search(qry = nil, &block) ⇒ Object
-
#to_query_hash ⇒ Object
(also: #to_query, #to_q)
Instance Method Details
#document_types ⇒ Object
38
39
40
|
# File 'lib/lycra/search.rb', line 38
def document_types
document_types ||= response.search.definition[:type]
end
|
#entry_name ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'lib/lycra/search.rb', line 42
def entry_name
if document_types.count == 1
document_types.first
elsif document_types.count > 1
return document_types.map(&:pluralize).to_sentence
else
'result'
end
end
|
#fields(flds = false) ⇒ Object
127
128
129
130
131
132
133
|
# File 'lib/lycra/search.rb', line 127
def fields(flds=false)
if flds != false
@fields = flds.is_a?(Array) ? flds : [flds]
else
@fields ||= self.class.fields
end
end
|
#initialize(term = nil, query: nil, filter: nil, post_filter: nil, sort: nil, models: nil, fields: nil, aggregations: nil, &block) ⇒ Object
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/lycra/search.rb', line 19
def initialize(term=nil, query: nil, filter: nil, post_filter: nil, sort: nil, models: nil, fields: nil, aggregations: nil, &block)
@term = term
@models = models
@fields = fields
self.query(block_given? ? instance_eval(&block) : query)
self.filter(filter)
self.post_filter(post_filter)
self.sort(sort)
self.aggregate(aggregations)
end
|
#match_all_query ⇒ Object
81
82
83
|
# File 'lib/lycra/search.rb', line 81
def match_all_query
{match_all: {}}
end
|
#match_phrase_prefix_query ⇒ Object
99
100
101
102
103
104
105
106
107
|
# File 'lib/lycra/search.rb', line 99
def match_phrase_prefix_query
return if term.nil?
field_queries = fields.map do |field|
{match_phrase_prefix: {field.to_s.gsub(/\^\d\Z/, '').to_sym => term}}
end
{or: field_queries}
end
|
#models(mdls = false) ⇒ Object
119
120
121
122
123
124
125
|
# File 'lib/lycra/search.rb', line 119
def models(mdls=false)
if mdls != false
@models = mdls.is_a?(Array) ? mdls : [mdls]
else
@models ||= self.class.models
end
end
|
#multi_match_query ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/lycra/search.rb', line 85
def multi_match_query
return if term.nil?
{
multi_match: {
query: term,
type: :best_fields,
fields: fields,
tie_breaker: 0.5,
operator: 'and'
}
}
end
|
#query(qry = nil) ⇒ Object
60
61
62
63
64
65
66
67
|
# File 'lib/lycra/search.rb', line 60
def query(qry=nil)
if !qry.nil?
@query = Lycra::Search::Query[qry]
self
else
@query ||= Lycra::Search::Query[send("#{query_method}_query")]
end
end
|
#query_method(meth = false) ⇒ Object
109
110
111
112
113
114
115
116
117
|
# File 'lib/lycra/search.rb', line 109
def query_method(meth=false)
if meth == false
@query_method ||= :match_all
else
@query = nil
@query_method = meth
self
end
end
|
#response ⇒ Object
30
31
32
|
# File 'lib/lycra/search.rb', line 30
def response
@response ||= search
end
|
#response! ⇒ Object
34
35
36
|
# File 'lib/lycra/search.rb', line 34
def response!
@response = search
end
|
#search(qry = nil, &block) ⇒ Object
52
53
54
55
56
57
58
|
# File 'lib/lycra/search.rb', line 52
def search(qry=nil, &block)
if block_given?
Lycra.search(instance_eval(&block), models)
else
Lycra.search((qry || to_query), models)
end
end
|
#to_query_hash ⇒ Object
Also known as:
to_query, to_q
69
70
71
72
73
74
75
76
77
|
# File 'lib/lycra/search.rb', line 69
def to_query_hash
{
query: query.to_query,
filter: filters.to_query,
sort: sorter.to_query,
post_filter: post_filters.to_query,
aggregations: aggregations.to_query
}
end
|