Class: Tire::Search::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/tire/search/query.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Query

Returns a new instance of Query.



5
6
7
8
# File 'lib/tire/search/query.rb', line 5

def initialize(&block)
  @value = {}
  block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block_given?
end

Instance Method Details

#allObject



78
79
80
81
# File 'lib/tire/search/query.rb', line 78

def all
  @value = { :match_all => {} }
  @value
end

#boolean(options = {}, &block) ⇒ Object



57
58
59
60
61
62
# File 'lib/tire/search/query.rb', line 57

def boolean(options={}, &block)
  @boolean ||= BooleanQuery.new(options)
  block.arity < 1 ? @boolean.instance_eval(&block) : block.call(@boolean) if block_given?
  @value[:bool] = @boolean.to_hash
  @value
end

#custom_score(options = {}, &block) ⇒ Object



45
46
47
48
49
50
# File 'lib/tire/search/query.rb', line 45

def custom_score(options={}, &block)
  @custom_score ||= Query.new(&block)
  @value[:custom_score] = options
  @value[:custom_score].update({:query => @custom_score.to_hash})
  @value
end

#dis_max(options = {}, &block) ⇒ Object



71
72
73
74
75
76
# File 'lib/tire/search/query.rb', line 71

def dis_max(options={}, &block)
  @dis_max ||= DisMaxQuery.new(options)
  block.arity < 1 ? @dis_max.instance_eval(&block) : block.call(@dis_max) if block_given?
  @value[:dis_max] = @dis_max.to_hash
  @value
end

#filtered(&block) ⇒ Object



64
65
66
67
68
69
# File 'lib/tire/search/query.rb', line 64

def filtered(&block)
  @filtered = FilteredQuery.new
  block.arity < 1 ? @filtered.instance_eval(&block) : block.call(@filtered) if block_given?
  @value[:filtered] = @filtered.to_hash
  @value
end

#fuzzy(field, value, options = {}) ⇒ Object



52
53
54
55
# File 'lib/tire/search/query.rb', line 52

def fuzzy(field, value, options={})
  query = { field => { :term => value }.update(options) }
  @value = { :fuzzy => query }
end

#ids(values, type) ⇒ Object



83
84
85
# File 'lib/tire/search/query.rb', line 83

def ids(values, type)
  @value = { :ids => { :values => values, :type => type }  }
end

#prefix(field, value, options = {}) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/tire/search/query.rb', line 37

def prefix(field, value, options={})
  if options[:boost]
    @value = { :prefix => { field => { :prefix => value, :boost => options[:boost] } } }
  else
    @value = { :prefix => { field => value } }
  end
end

#range(field, value) ⇒ Object



21
22
23
# File 'lib/tire/search/query.rb', line 21

def range(field, value)
  @value = { :range => { field => value } }
end

#string(value, options = {}) ⇒ Object



31
32
33
34
35
# File 'lib/tire/search/query.rb', line 31

def string(value, options={})
  @value = { :query_string => { :query => value } }
  @value[:query_string].update(options)
  @value
end

#term(field, value, options = {}) ⇒ Object



10
11
12
13
# File 'lib/tire/search/query.rb', line 10

def term(field, value, options={})
  query = { field => { :term => value }.update(options) }
  @value = { :term => query }
end

#terms(field, value, options = {}) ⇒ Object



15
16
17
18
19
# File 'lib/tire/search/query.rb', line 15

def terms(field, value, options={})
  @value = { :terms => { field => value } }
  @value[:terms].update( { :minimum_match => options[:minimum_match] } ) if options[:minimum_match]
  @value
end

#text(field, value, options = {}) ⇒ Object



25
26
27
28
29
# File 'lib/tire/search/query.rb', line 25

def text(field, value, options={})
  query_options = { :query => value }.update(options)
  @value = { :text => { field => query_options } }
  @value
end

#to_hashObject



87
88
89
# File 'lib/tire/search/query.rb', line 87

def to_hash
  @value
end

#to_jsonObject



91
92
93
# File 'lib/tire/search/query.rb', line 91

def to_json
  to_hash.to_json
end