Class: Chewy::Search::QueryProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/chewy/search/query_proxy.rb

Overview

This specialized proxy class is used to provide an ability of query, filter, post_filter parameters additional modification.

Instance Method Summary collapse

Constructor Details

#initialize(parameter_name, request) ⇒ QueryProxy

Returns a new instance of QueryProxy.

Parameters:

  • parameter_name (Symbol)

    modified parameter name

  • request (Chewy::Search::Request)

    request instance for modification



14
15
16
17
# File 'lib/chewy/search/query_proxy.rb', line 14

def initialize(parameter_name, request)
  @parameter_name = parameter_name
  @request = request
end

Instance Method Details

#and(query_hash) ⇒ Chewy::Search::Request #and(scope) ⇒ Chewy::Search::Request #and { ... } ⇒ Chewy::Search::Request

Executes Parameters::QueryStorage#and in the scope of newly created request object.

Overloads:

  • #and(query_hash) ⇒ Chewy::Search::Request

    If pure hash is passed, the current root bool query and the passed one are joined into a single must array of the new root query.

    Examples:

    scope = PlacesIndex.query.must_not(match: {name: 'Moscow'})
    scope.query.and(match: {name: 'London'})
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :must=>[{:bool=>{:must_not=>{:match=>{:name=>"Moscow"}}}}, {:match=>{:name=>"London"}}]}}}}>
    scope = PlacesIndex.query(match: {name: 'Moscow'})
    scope.query.and(match: {name: 'London'})
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :must=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>

    Parameters:

    • query_hash (Hash)

      pure query hash

    See Also:

  • #and(scope) ⇒ Chewy::Search::Request

    If a scope is passed, the appropriate parameter storage value will be extracted from it and used as a second query.

    Examples:

    scope1 = PlacesIndex.query.must_not(match: {name: 'Moscow'})
    scope2 = PlacesIndex.query(match: {name: 'London'})
    scope1.query.and(scope2)
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :must=>[{:bool=>{:must_not=>{:match=>{:name=>"Moscow"}}}}, {:match=>{:name=>"London"}}]}}}}>

    Parameters:

  • #and { ... } ⇒ Chewy::Search::Request

    If block is passed instead of a pure hash, `elasticsearch-dsl" gem will be used to process it.

    Examples:

    scope = PlacesIndex.query.must_not(match: {name: 'Moscow'})
    scope.query.and { match name: 'London' }
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :must=>[{:bool=>{:must_not=>{:match=>{:name=>"Moscow"}}}}, {:match=>{:name=>"London"}}]}}}}>

    Yields:

    • the block is processed by elasticsearch-dsl gem

    See Also:

Returns:

See Also:



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/chewy/search/query_proxy.rb', line 239

%i[and or not].each do |method|
  define_method method do |query_hash_or_scope = nil, &block|
    unless query_hash_or_scope || block
      raise ArgumentError,
            "Please provide a parameter or a block to `#{method}`"
    end

    if !block && query_hash_or_scope.is_a?(Chewy::Search::Request)
      query_hash_or_scope = query_hash_or_scope.parameters[@parameter_name].value
    end
    @request.send(:modify, @parameter_name) { send(method, block || query_hash_or_scope) }
  end
end

#minimum_should_match(value) ⇒ Chewy::Search::Request

Executes Parameters::QueryStorage#minimum_should_match in the scope of newly created request object.

Parameters:

  • value (String, Integer, nil)

Returns:

See Also:



259
260
261
# File 'lib/chewy/search/query_proxy.rb', line 259

def minimum_should_match(value)
  @request.send(:modify, @parameter_name) { minimum_should_match(value) }
end

#must(query_hash) ⇒ Chewy::Search::Request #must { ... } ⇒ Chewy::Search::Request

Executes Parameters::QueryStorage#must in the scope of newly created request object.

Overloads:

Returns:

See Also:



102
103
104
105
106
107
108
# File 'lib/chewy/search/query_proxy.rb', line 102

%i[must should must_not].each do |method|
  define_method method do |query_hash = nil, &block|
    raise ArgumentError, "Please provide a parameter or a block to `#{method}`" unless query_hash || block

    @request.send(:modify, @parameter_name) { send(method, block || query_hash) }
  end
end

#must_not(query_hash) ⇒ Chewy::Search::Request #must_not { ... } ⇒ Chewy::Search::Request

Executes Parameters::QueryStorage#must_not in the scope of newly created request object.

Overloads:

  • #must_not(query_hash) ⇒ Chewy::Search::Request

    If pure hash is passed it is added to must_not array of the bool query.

    Examples:

    PlacesIndex.query.must_not(match: {name: 'Moscow'}).query.must_not(match: {name: 'London'})
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :must_not=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>

    Parameters:

    • query_hash (Hash)

      pure query hash

    See Also:

  • #must_not { ... } ⇒ Chewy::Search::Request

    If block is passed instead of a pure hash, `elasticsearch-dsl" gem will be used to process it.

    Examples:

    PlacesIndex.query.must_not { match name: 'Moscow' }.query.must_not { match name: 'London' }
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :must_not=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>

    Yields:

    • the block is processed by elasticsearch-dsl gem

    See Also:

Returns:

See Also:



102
103
104
105
106
107
108
# File 'lib/chewy/search/query_proxy.rb', line 102

%i[must should must_not].each do |method|
  define_method method do |query_hash = nil, &block|
    raise ArgumentError, "Please provide a parameter or a block to `#{method}`" unless query_hash || block

    @request.send(:modify, @parameter_name) { send(method, block || query_hash) }
  end
end

#not(query_hash) ⇒ Chewy::Search::Request #not(scope) ⇒ Chewy::Search::Request #not { ... } ⇒ Chewy::Search::Request

Executes Parameters::QueryStorage#not in the scope of newly created request object. The only difference from #must_not is that is accepts another scope additionally.

Overloads:

  • #not(query_hash) ⇒ Chewy::Search::Request

    If pure hash is passed it is added to must_not array of the bool query.

    Examples:

    scope = PlacesIndex.query.must_not(match: {name: 'Moscow'})
    scope.query.not(match: {name: 'London'})
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :must_not=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>

    Parameters:

    • query_hash (Hash)

      pure query hash

    See Also:

  • #not(scope) ⇒ Chewy::Search::Request

    If a scope is passed, the appropriate parameter storage value will be extracted from it and used as a second query.

    Examples:

    scope1 = PlacesIndex.query.must_not(match: {name: 'Moscow'})
    scope2 = PlacesIndex.query(match: {name: 'London'})
    scope1.query.not(scope2)
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :must_not=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>

    Parameters:

  • #not { ... } ⇒ Chewy::Search::Request

    If block is passed instead of a pure hash, `elasticsearch-dsl" gem will be used to process it.

    Examples:

    scope = PlacesIndex.query.must_not(match: {name: 'Moscow'})
    scope.query.not { match name: 'London' }
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :must_not=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>

    Yields:

    • the block is processed by elasticsearch-dsl gem

    See Also:

Returns:

See Also:



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/chewy/search/query_proxy.rb', line 239

%i[and or not].each do |method|
  define_method method do |query_hash_or_scope = nil, &block|
    unless query_hash_or_scope || block
      raise ArgumentError,
            "Please provide a parameter or a block to `#{method}`"
    end

    if !block && query_hash_or_scope.is_a?(Chewy::Search::Request)
      query_hash_or_scope = query_hash_or_scope.parameters[@parameter_name].value
    end
    @request.send(:modify, @parameter_name) { send(method, block || query_hash_or_scope) }
  end
end

#or(query_hash) ⇒ Chewy::Search::Request #or(scope) ⇒ Chewy::Search::Request #or { ... } ⇒ Chewy::Search::Request

Executes Parameters::QueryStorage#or in the scope of newly created request object.

Overloads:

  • #or(query_hash) ⇒ Chewy::Search::Request

    If pure hash is passed, the current root bool query and the passed one are joined into a single should array of the new root query.

    Examples:

    scope = PlacesIndex.query.must_not(match: {name: 'Moscow'})
    scope.query.or(match: {name: 'London'})
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :should=>[{:bool=>{:must_not=>{:match=>{:name=>"Moscow"}}}}, {:match=>{:name=>"London"}}]}}}}>
    scope = PlacesIndex.query(match: {name: 'Moscow'})
    scope.query.or(match: {name: 'London'})
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :should=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>

    Parameters:

    • query_hash (Hash)

      pure query hash

    See Also:

  • #or(scope) ⇒ Chewy::Search::Request

    If a scope is passed, the appropriate parameter storage value will be extracted from it and used as a second query.

    Examples:

    scope1 = PlacesIndex.query.must_not(match: {name: 'Moscow'})
    scope2 = PlacesIndex.query(match: {name: 'London'})
    scope1.query.or(scope2)
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :should=>[{:bool=>{:must_not=>{:match=>{:name=>"Moscow"}}}}, {:match=>{:name=>"London"}}]}}}}>

    Parameters:

  • #or { ... } ⇒ Chewy::Search::Request

    If block is passed instead of a pure hash, `elasticsearch-dsl" gem will be used to process it.

    Examples:

    scope = PlacesIndex.query.must_not(match: {name: 'Moscow'})
    scope.query.or { match name: 'London' }
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :should=>[{:bool=>{:must_not=>{:match=>{:name=>"Moscow"}}}}, {:match=>{:name=>"London"}}]}}}}>

    Yields:

    • the block is processed by elasticsearch-dsl gem

    See Also:

Returns:

See Also:



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/chewy/search/query_proxy.rb', line 239

%i[and or not].each do |method|
  define_method method do |query_hash_or_scope = nil, &block|
    unless query_hash_or_scope || block
      raise ArgumentError,
            "Please provide a parameter or a block to `#{method}`"
    end

    if !block && query_hash_or_scope.is_a?(Chewy::Search::Request)
      query_hash_or_scope = query_hash_or_scope.parameters[@parameter_name].value
    end
    @request.send(:modify, @parameter_name) { send(method, block || query_hash_or_scope) }
  end
end

#should(query_hash) ⇒ Chewy::Search::Request #should { ... } ⇒ Chewy::Search::Request

Executes Parameters::QueryStorage#should in the scope of newly created request object.

Overloads:

  • #should(query_hash) ⇒ Chewy::Search::Request

    If pure hash is passed it is added to should array of the bool query.

    Examples:

    PlacesIndex.query.should(match: {name: 'Moscow'}).query.should(match: {name: 'London'})
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :should=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>

    Parameters:

    • query_hash (Hash)

      pure query hash

    See Also:

  • #should { ... } ⇒ Chewy::Search::Request

    If block is passed instead of a pure hash, `elasticsearch-dsl" gem will be used to process it.

    Examples:

    PlacesIndex.query.should { match name: 'Moscow' }.query.should { match name: 'London' }
    # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
    #      :should=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>

    Yields:

    • the block is processed by elasticsearch-dsl gem

    See Also:

Returns:

See Also:



102
103
104
105
106
107
108
# File 'lib/chewy/search/query_proxy.rb', line 102

%i[must should must_not].each do |method|
  define_method method do |query_hash = nil, &block|
    raise ArgumentError, "Please provide a parameter or a block to `#{method}`" unless query_hash || block

    @request.send(:modify, @parameter_name) { send(method, block || query_hash) }
  end
end