Class: Tire::Search::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/tire/search/queries/custom_filters_score.rb,
lib/tire/search/query.rb,
lib/tire/search/queries/match.rb

Overview

Custom Filters Score

Author: Jerry Luk [email protected]

Adds support for "custom_filters_score" queries in Tire DSL.

It hooks into the Query class and inserts the custom_filters_score query types.

Usage:

Require the component:

require 'tire/queries/custom_filters_score'

Example:

Tire.search 'articles' do
  query do
    custom_filters_score do
      query { term :title, 'Harry Potter' }
      filter do
        filter :match_all
        boost 1.1
      end
      filter do
        filter :term, :author => 'Rowling',
        script '2.0'
      end
      score_mode 'total'
    end
  end
end

For available options for these queries see:

Defined Under Namespace

Classes: CustomFiltersScoreQuery

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Query

Returns a new instance of Query.



7
8
9
10
# File 'lib/tire/search/query.rb', line 7

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

Instance Attribute Details

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#all(options = {}) ⇒ Object



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

def all(options = {})
  @value = { :match_all => options }
  @value
end

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



61
62
63
64
65
66
# File 'lib/tire/search/query.rb', line 61

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

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



100
101
102
103
104
105
# File 'lib/tire/search/query.rb', line 100

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

#constant_score(&block) ⇒ Object



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

def constant_score(&block)
  @value.update( { :constant_score => ConstantScoreQuery.new(&block).to_hash } ) if block_given?
end

#custom_filters_score(&block) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/tire/search/queries/custom_filters_score.rb', line 49

def custom_filters_score(&block)
  @custom_filters_score = CustomFiltersScoreQuery.new
  block.arity < 1 ? @custom_filters_score.instance_eval(&block) : block.call(@custom_filters_score) if
    block_given?
  @value[:custom_filters_score] = @custom_filters_score.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



75
76
77
78
79
80
# File 'lib/tire/search/query.rb', line 75

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



68
69
70
71
72
73
# File 'lib/tire/search/query.rb', line 68

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



56
57
58
59
# File 'lib/tire/search/query.rb', line 56

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

#ids(values, type = nil) ⇒ Object



94
95
96
97
98
# File 'lib/tire/search/query.rb', line 94

def ids(values, type=nil)
  @value = { :ids => { :values => Array(values) }  }
  @value[:ids].update(:type => type) if type
  @value
end

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



5
6
7
8
9
10
11
12
# File 'lib/tire/search/queries/match.rb', line 5

def match(field, value, options={})
  if @value.empty?
    @value = MatchQuery.new(field, value, options).to_hash
  else
    MatchQuery.add(self, field, value, options)
  end
  @value
end

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



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

def nested(options={}, &block)
  @nested = NestedQuery.new(options)
  block.arity < 1 ? @nested.instance_eval(&block) : block.call(@nested) if block_given?
  @value[:nested] = @nested.to_hash
  @value
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



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

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



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

def term(field, value, options={})
  query = if value.is_a?(Hash)
    { field => value.to_hash }
  else
    { field => { :term => value }.update(options) }
  end
  @value = { :term => query }
end

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



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

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

#to_hashObject



107
108
109
# File 'lib/tire/search/query.rb', line 107

def to_hash
  @value
end

#to_json(options = {}) ⇒ Object



111
112
113
# File 'lib/tire/search/query.rb', line 111

def to_json(options={})
  to_hash.to_json
end