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"
)

A full-text search using the 'or' operator

query.match(
  my_field: 'match any of these',
  other_field: 'other words',
  operator: 'or'
)

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

    Options Hash (opts_or_string):

    • :operator (String) — default: 'and'

      Allows switching from the default 'and' operator to 'or', for all the specified fields

See Also:



61
62
63
64
# File 'lib/stretchy/clauses/match_clause.rb', line 61

def initialize(base, opts_or_str = {})
  super(base)
  add_params(opts_or_str)
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

#phrase(opts_or_str) ⇒ self #phrase(opts_or_str) ⇒ self

Specifies that values for this field should be matched with a proximity boost, rather than as a generic match query.

This means searching for "quick brown fox" will return matches in the following order:

  • "the quick brown fox jumped over"
  • "the brown quick fox jumped over"
  • "the fox, brown & quick jumped over"
  • "the quick fox jumped over"
  • "the quick green and purple sparkly fox jumped over"
  • "the quick dog jumped over"
  • "the adoreable puppy jumped over" not returned

Examples:

Matching multiple words together

query.match.phrase('hugs and love')

Not matching a phrase

query.match.not.phrase(comment: 'offensive words to hide')

Overloads:

  • #phrase(opts_or_str) ⇒ self

    Parameters:

    • opts_or_str

      = {} [String] A phrase that will be matched anywhere in the document

  • #phrase(opts_or_str) ⇒ self

    Parameters:

    • opts_or_str

      = {} [Hash] A hash of fields and phrases that should be matched in those fields

Returns:

  • (self)

    Allows continuing the query chain

See Also:



99
100
101
102
103
# File 'lib/stretchy/clauses/match_clause.rb', line 99

def fulltext(opts_or_str = {})
  add_params(opts_or_str, min: 1)
  add_params(opts_or_str, should: true, slop: 50)
  self
end

#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



124
125
126
127
128
# File 'lib/stretchy/clauses/match_clause.rb', line 124

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

Returns:

  • (MatchClause)

    query state with should filters added

See Also:



160
161
162
163
164
165
# File 'lib/stretchy/clauses/match_clause.rb', line 160

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



189
190
191
# File 'lib/stretchy/clauses/match_clause.rb', line 189

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:



174
175
176
177
178
179
180
181
182
183
# File 'lib/stretchy/clauses/match_clause.rb', line 174

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