Class: Stretchy::Clauses::MatchClause

Inherits:
Base
  • Object
show all
Defined in:
lib/stretchy/clauses/match_clause.rb

Overview

A Match clause inherits the same state as any clause. There aren't any more specific methods to chain, as this clause only handles basic full-text searches.

Author:

  • [atevans]

Instance Attribute Summary

Attributes inherited from Base

#base

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#aggregations, #boost, #explain, #fields, #get_aggregations, #get_explain, #get_fields, #get_limit, #get_offset, #get_page, #inverse?, #limit, #match, #offset, #page, #query_results, #where

Constructor Details

#initialize(base, opts_or_string) ⇒ MatchClause #initialize(base, opts_or_string) ⇒ MatchClause

Creates a new state with a match query applied.

Examples:

A basic full-text match

query.match("anywhere in document")

A full-text search on specific fields

query.match(
  my_field: "match in my_field",
  other_field: "match in other_field"
)

Overloads:

  • #initialize(base, opts_or_string) ⇒ MatchClause

    Parameters:

    • Base (Base)

      clause to copy data from

    • Performs (String)

      a full-text query for this string on all fields in the document.

  • #initialize(base, opts_or_string) ⇒ MatchClause

    Parameters:

    • Base (Base)

      clause to copy data from

    • A (Hash)

      hash of fields and values to perform full-text matches with

See Also:



52
53
54
55
56
57
58
59
# File 'lib/stretchy/clauses/match_clause.rb', line 52

def initialize(base, opts_or_str = {}, options = {})
  super(base)
  if opts_or_str.is_a?(Hash)
    add_params(options.merge(opts_or_str))
  else
    add_params(options.merge('_all' => opts_or_str))
  end
end

Class Method Details

.tmp(options = {}) ⇒ MatchClause

Creates a temporary MatchClause outside the main query scope by using a new Base. Primarily used in BoostClause for boosting on full-text matches.

Parameters:

  • options (defaults to: {})

    = {} [Hash] Options to pass to the full-text match

Returns:

  • (MatchClause)

    Temporary clause outside current state



23
24
25
26
27
28
29
# File 'lib/stretchy/clauses/match_clause.rb', line 23

def self.tmp(options = {})
  if options.delete(:inverse)
    self.new(Builders::ShellBuilder.new).not(options)
  else
    self.new(Builders::ShellBuilder.new, options)
  end
end

Instance Method Details

#not(opts_or_str) ⇒ MatchClause #not(opts_or_str) ⇒ MatchClause

Switches to inverted context. Matches applied here work the same way as #initialize, but returned documents must not match these filters.

Examples:

Inverted full-text

query.match.not("hello")

Inverted full-text matching for specific fields

query.match.not(
  my_field: "not_match_1",
  other_field: "not_match_2"
)

Overloads:

  • #not(opts_or_str) ⇒ MatchClause

    Parameters:

    • A (String)

      string that must not be matched anywhere in the document

  • #not(opts_or_str) ⇒ MatchClause

    Parameters:

    • A (Hash)

      hash of fields and strings that must not be matched in those fields

Returns:

  • (MatchClause)

    inverted query state with match filters applied



80
81
82
83
84
# File 'lib/stretchy/clauses/match_clause.rb', line 80

def not(opts_or_str = {})
  @inverse = true
  add_params(opts_or_str)
  self
end

#should(opts_or_str) ⇒ MatchClause #should(opts_or_str) ⇒ MatchClause

Switches to should context. Applies full-text matches that are not required, but boost the relevance score for matching documents.

Can be chained with #not

Examples:

Should match with full-text

query.match.should("anywhere")

Should match specific fields

query.match.should(
  field_one: "one",
  field_two: "two"
)

Should not match

query.match.should.not(
  field_one: "one",
  field_two: "two"
)

Overloads:

  • #should(opts_or_str) ⇒ MatchClause

    Parameters:

    • A (String)

      string that should be matched anywhere in the document

  • #should(opts_or_str) ⇒ MatchClause

    Parameters:

    • A (Hash)

      hash of fields and strings that should be matched in those fields

Parameters:

  • opts_or_str (defaults to: {})

    = {} [type] [description]

  • options

    = {} [type] [description]

Returns:

  • (MatchClause)

    query state with should filters added

See Also:



119
120
121
122
123
124
# File 'lib/stretchy/clauses/match_clause.rb', line 119

def should(opts_or_str = {})
  @should  = true
  @inverse = false
  add_params(opts_or_str)
  self
end

#should?true, false

Accessor for @should

Returns:

  • (true, false)

    @should



148
149
150
# File 'lib/stretchy/clauses/match_clause.rb', line 148

def should?
  !!@should
end

#to_boost(weight = nil) ⇒ Stretchy::Boosts::FilterBoost

Converts this match context to a set of boosts to use in a Queries::FunctionScoreQuery

Parameters:

  • weight (defaults to: nil)

    = nil [Numeric] Weight of generated boost

Returns:



133
134
135
136
137
138
139
140
141
142
# File 'lib/stretchy/clauses/match_clause.rb', line 133

def to_boost(weight = nil)
  weight ||= Stretchy::Boosts::FilterBoost::DEFAULT_WEIGHT
  
  Stretchy::Boosts::FilterBoost.new(
    filter: Stretchy::Filters::QueryFilter.new(
      base.match_builder.to_query
    ),
    weight: weight
  )
end