Class: Sunspot::DSL::StandardQuery

Inherits:
FieldQuery show all
Includes:
Adjustable, Functional, Paginatable, Spellcheckable
Defined in:
lib/sunspot/dsl/standard_query.rb

Overview

This class presents a DSL for constructing queries using the Sunspot.search method. Methods of this class are available inside the search block. Much of the DSL’s functionality is implemented by this class’s superclasses, Sunspot::DSL::FieldQuery and Sunspot::DSL::Scope

See Sunspot.search for usage examples

Direct Known Subclasses

Search

Instance Method Summary collapse

Methods included from Paginatable

#paginate

Methods included from Adjustable

#adjust_solr_params, #request_handler

Methods included from Spellcheckable

#spellcheck

Methods included from Functional

#create_function_query, #function

Methods inherited from FieldQuery

#dynamic, #facet, #group, #initialize, #json_facet, #order_by, #order_by_function, #order_by_geodist, #order_by_random, #stats

Methods inherited from Scope

#all_of, #any_of, #dynamic, #field_list, #initialize, #text_fields, #without, #without_stored_fields

Constructor Details

This class inherits a constructor from Sunspot::DSL::FieldQuery

Instance Method Details

#all(&block) ⇒ Object



120
121
122
123
124
# File 'lib/sunspot/dsl/standard_query.rb', line 120

def all(&block)
  @query.conjunction do
    Util.instance_eval_or_call(self, &block)
  end
end

#any(&block) ⇒ Object



114
115
116
117
118
# File 'lib/sunspot/dsl/standard_query.rb', line 114

def any(&block)
  @query.disjunction do
    Util.instance_eval_or_call(self, &block)
  end
end

#boost(factor_or_function, &block) ⇒ Object

Defines a boost query

Examples

Sunspot.search(Post) do
  with(:blog_id, 1)

  boost(10) do
    with(:category_id, 2)
  end
end


139
140
141
142
143
144
145
146
147
148
# File 'lib/sunspot/dsl/standard_query.rb', line 139

def boost(factor_or_function, &block)
  if factor_or_function.is_a?(Sunspot::Query::FunctionQuery)
    @query.add_boost_function(factor_or_function)
  else
    Sunspot::Util.instance_eval_or_call(
      Scope.new(@query.add_boost_query(factor_or_function), @setup),
      &block
    )
  end
end

#boost_multiplicative(factor_or_function) ⇒ Object



150
151
152
# File 'lib/sunspot/dsl/standard_query.rb', line 150

def boost_multiplicative(factor_or_function)
  @query.add_multiplicative_boost_function(factor_or_function)
end

#fulltext(keywords, options = {}, &block) ⇒ Object Also known as: keywords

Specify a phrase that should be searched as fulltext. Only text fields are searched - see DSL::Fields.text

Keyword search is executed using Solr’s dismax handler, which strikes a good balance between powerful and foolproof. In particular, well-matched quotation marks can be used to group phrases, and the + and - modifiers work as expected. All other special Solr boolean syntax is escaped, and mismatched quotes are ignored entirely.

This method can optionally take a block, which is evaluated by the Fulltext DSL class, and exposes several powerful dismax features.

Parameters

keywords<String>

phrase to perform fulltext search on

Options

:fields<Array>

List of fields that should be searched for keywords. Defaults to all fields configured for the types under search.

:highlight<Boolean,Array>

If true, perform keyword highlighting on all searched fields. If an array of field names, perform highlighting on the specified fields. Note that for highlighting to work, the desired fields have to be set up with :stored => true. This can also be called from within the fulltext block.

:minimum_match<Integer>

The minimum number of search terms that a result must match. By default, all search terms must match; if the number of search terms is less than this number, the default behavior applies.

:tie<Float>

A tiebreaker coefficient for scores derived from subqueries that are lower-scoring than the maximum score subquery. Typically a near-zero value is useful. See wiki.apache.org/solr/DisMaxRequestHandler#tie_.28Tie_breaker.29 for more information.

:query_phrase_slop<Integer>

The number of words that can appear between the words in a user-entered phrase (i.e., keywords in quotes) and still match. For instance, in a search for “"great pizza"” with a phrase slop of 1, “great pizza” and “great big pizza” will match, but “great monster of a pizza” will not. Default behavior is a query phrase slop of zero.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/sunspot/dsl/standard_query.rb', line 58

def fulltext(keywords, options = {}, &block)
  return if not keywords or keywords.to_s =~ /^\s*$/

  field_names = Util.Array(options.delete(:fields)).compact

  add_fulltext(keywords, field_names) do |query, fields|
    query.minimum_match = options.delete(:minimum_match).to_i if options.key?(:minimum_match)
    query.tie = options.delete(:tie).to_f if options.key?(:tie)
    query.query_phrase_slop = options.delete(:query_phrase_slop).to_i if options.key?(:query_phrase_slop)

    if highlight_field_names = options.delete(:highlight)
      if highlight_field_names == true
        query.add_highlight
      else
        highlight_fields = []
        Util.Array(highlight_field_names).each do |field_name|
          highlight_fields.concat(@setup.text_fields(field_name))
        end
        query.add_highlight(highlight_fields)
      end
    end

    if block && query
      fulltext_dsl = Fulltext.new(query, @setup)
      Util.instance_eval_or_call(fulltext_dsl, &block)
    else
      fulltext_dsl = nil
    end

    if fields.empty? && (!fulltext_dsl || !fulltext_dsl.fields_added?)
      @setup.all_text_fields.each do |field|
        unless query.has_fulltext_field?(field)
          unless fulltext_dsl && fulltext_dsl.exclude_fields.include?(field.name)
            query.add_fulltext_field(field, field.default_boost)
          end
        end
      end
    end
  end
end

#with(*args) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/sunspot/dsl/standard_query.rb', line 101

def with(*args)
  case args.first
    when String, Symbol
      if args.length == 1 # NONE
        field = @setup.field(args[0].to_sym)
        return DSL::RestrictionWithNear.new(field, @scope, @query, false)
      end
  end

  # else
  super
end