Class: Elasticsearch::DSL::Search::Queries::InnerHits

Inherits:
Object
  • Object
show all
Includes:
BaseComponent
Defined in:
lib/elasticsearch/dsl/search/queries/inner_hits.rb

Overview

Wraps the ‘inner_hits` part of a search definition

Instance Method Summary collapse

Methods included from BaseComponent

included

Constructor Details

#initialize(name = nil, &block) ⇒ InnerHits

Initialize the inner_hits definition.

Parameters:

  • name (String, Symbol) (defaults to: nil)

    The name to be used for the particular inner hit definition in the response. Useful when multiple inner hits have been defined in a single search request. The default depends in which query the inner hit is defined. For has_child query and filter this is the child type, has_parent query and filter this is the parent type and the nested query and filter this is the nested path.

Since:

  • 0.1.9



38
39
40
41
# File 'lib/elasticsearch/dsl/search/queries/inner_hits.rb', line 38

def initialize(name=nil, &block)
  @value = name ? { name: name } : {}
  super
end

Instance Method Details

#from(from) ⇒ Object

Specify the from setting on the inner_hits definition, the offset from where the first hit to fetch for

each inner_hits in the returned regular search hits.

Examples:

inner_hits 'last_tweet' do
  size 10
  from 5
end

Parameters:

  • from (Integer)

    The from setting.

Returns:

  • self.

Since:

  • 0.1.9



76
77
78
79
# File 'lib/elasticsearch/dsl/search/queries/inner_hits.rb', line 76

def from(from)
  @value[:from] = from
  self
end

#size(size) ⇒ Object

Specify the size setting on the inner_hits definition, the maximum number of hits to return per inner_hits.

By default the top three matching hits are returned.

Examples:

inner_hits 'last_tweet' do
  size 10
  from 5
end

Parameters:

  • size (Integer)

    The size setting.

Returns:

  • self.

Since:

  • 0.1.9



57
58
59
60
# File 'lib/elasticsearch/dsl/search/queries/inner_hits.rb', line 57

def size(size)
  @value[:size] = size
  self
end

#sort(*args, &block) ⇒ Object

Specify the sorting on the inner_hits definition. By default the hits are sorted by the score.

Examples:

inner_hits 'last_tweet' do
  size 10
  from 5
  sort do
    by :date, order: 'desc'
    by :likes, order: 'asc'
  end
end

Parameters:

  • from (Integer)

    The from setting.

Returns:

  • self.

Since:

  • 0.1.9



98
99
100
101
102
103
104
105
# File 'lib/elasticsearch/dsl/search/queries/inner_hits.rb', line 98

def sort(*args, &block)
  if !args.empty? || block
    @sort = Sort.new(*args, &block)
    self
  else
    @sort
  end
end

#to_hashHash

Convert the definition to a hash, to be used in a search request.

Examples:

definition = begin do
  inner_hits 'last_tweet' do
    size 10
    from 5
    sort do
      by :date, order: 'desc'
      by :likes, order: 'asc'
    end
  end

Returns:

  • (Hash)

    The inner_hits clause as a hash.

Since:

  • 0.1.9



123
124
125
126
127
128
# File 'lib/elasticsearch/dsl/search/queries/inner_hits.rb', line 123

def to_hash
  call
  @hash = @value
  @hash[:sort] = @sort.to_hash if @sort
  @hash
end