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



25
26
27
28
# File 'lib/elasticsearch/dsl/search/queries/inner_hits.rb', line 25

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



63
64
65
66
# File 'lib/elasticsearch/dsl/search/queries/inner_hits.rb', line 63

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



44
45
46
47
# File 'lib/elasticsearch/dsl/search/queries/inner_hits.rb', line 44

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



85
86
87
88
89
90
91
92
# File 'lib/elasticsearch/dsl/search/queries/inner_hits.rb', line 85

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



110
111
112
113
114
115
# File 'lib/elasticsearch/dsl/search/queries/inner_hits.rb', line 110

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