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.

Constant Summary collapse

FULLTEXT_SLOP =
50
FULLTEXT_MIN =
1

Instance Attribute Summary

Attributes inherited from Base

#base

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

This class inherits a constructor from Stretchy::Clauses::Base

Instance Method Details

#phrase(params) ⇒ self #phrase(params) ⇒ 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(params) ⇒ self

    Parameters:

    • params

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

  • #phrase(params) ⇒ self

    Parameters:

    • params

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

Returns:

  • (self)

    Allows continuing the query chain

See Also:



56
57
58
59
60
# File 'lib/stretchy/clauses/match_clause.rb', line 56

def fulltext(params = {})
  add_params(params)
  add_params(params, should: true, slop: FULLTEXT_SLOP)
  self
end

#match(params = {}, options = {}) ⇒ Object



17
18
19
20
21
# File 'lib/stretchy/clauses/match_clause.rb', line 17

def match(params = {}, options = {})
  @inverse = false unless should?
  add_params(hashify_params(params), options)
  self
end

#more_like(params = {}, options = {}) ⇒ MatchClause

Adds a MoreLikeThis query to the chain. Pass in document ids, an array of index/name/ids, or a string to get documents that have similar terms.

This method accepts all the options in the Elasticsearch more_like_this query, which are pretty extensive. See the documentation (link below) to get an idea of what is available.

Only one of :docs, :ids, or :like_text is required, and one of those three must be present.

Examples:

Getting more like a document by id

query.more_like(ids: other_result_id)

Getting more like a document from another query

query.more_like(docs: other_query.results)

Getting more like a document from a string

query.more_like(like_text: 'puppies and kittens are great')

Parameters:

  • params (defaults to: {})

    = {} [Hash] Params used to build the more_like_this query @option params [Array] :like_text A string to compare documents do @option params [Array] :ids A list of document ids to compare documents to @option params [Array] :docs An array of document hashes. You can pass the results of another query here, or a hash with fields '_index', '_type' and '_id' @option params [Array] :fields A list of fields to use in the comparison. Defaults to '_all' @option params [Array] :include Whether the source documents should be included in the result set. Defaults to false

Returns:

See Also:



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

def more_like(params = {}, options = {})
  query = Queries::MoreLikeThisQuery.new(params)
  options[:inverse] = true if inverse?
  options[:should]  = true if should?

  base.add_query(query, options)
  self
end

#not(params) ⇒ MatchClause #not(params) ⇒ MatchClause

Switches to inverted context. Matches applied here work the same way as Base#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(params) ⇒ MatchClause

    Parameters:

    • A (String)

      string that must not be matched anywhere in the document

  • #not(params) ⇒ 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



126
127
128
129
130
# File 'lib/stretchy/clauses/match_clause.rb', line 126

def not(params = {}, options = {})
  @inverse = true
  add_params(params, options)
  self
end

#should(params) ⇒ MatchClause #should(params) ⇒ 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(params) ⇒ MatchClause

    Parameters:

    • A (String)

      string that should be matched anywhere in the document

  • #should(params) ⇒ 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:



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

def should(params = {}, options = {})
  @should  = true
  @inverse = false
  add_params(params, options)
  self
end

#should?true, false

Accessor for @should

Returns:

  • (true, false)

    @should



191
192
193
# File 'lib/stretchy/clauses/match_clause.rb', line 191

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:



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

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