### Install elasticsearch-dsl from source
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/README.md
Install elasticsearch-dsl from a source code checkout.
```shell
git clone https://github.com/elasticsearch/elasticsearch-dsl-ruby.git
cd elasticsearch-dsl-ruby
bundle install
rake install
```
--------------------------------
### Clone and install dependencies
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/CONTRIBUTING.md
Clones the repository and installs dependencies using Bundler.
```bash
git clone https://github.com/elasticsearch/elasticsearch-dsl-ruby.git
cd elasticsearch-dsl/
bundle install
```
--------------------------------
### Complete highlight example
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/highlight.md
Complete example showing how to build a search definition with highlight options and the resulting hash.
```ruby
include Elasticsearch::DSL
# Field-level options are the way to set per-field highlight settings:
definition = search do
query { match title: 'test' }
highlight do
fields [:title, :body]
field :title, fragment_size: 100
pre_tags ''
post_tags ''
end
end
definition.to_hash
# => { query: { match: { title: 'test' } },
# highlight: { fields: { title: { fragment_size: 100 }, body: {} },
# pre_tags: [''], post_tags: [''] } }
```
--------------------------------
### Bool query example
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/queries-catalog.md
Example of building a `bool` query with `must`, `should`, `must_not`, `filter`, and `minimum_should_match`.
```ruby
search do
query do
bool do
must { term category: 'men' }
must { term size: 'xxl' }
should { term color: 'red' }
must_not { term manufacturer: 'evil' }
filter { term status: 'published' }
minimum_should_match 1
end
end
end
```
--------------------------------
### Install elasticsearch-dsl gem
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/README.md
Install the elasticsearch-dsl gem from Rubygems.
```shell
gem install elasticsearch-dsl
```
--------------------------------
### FunctionScore query example
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/queries-catalog.md
Example of a `function_score` query with a `match_all` query, a `gauss` function, and `boost_mode` set to 'multiply'.
```ruby
search do
query do
function_score do
query { match_all }
functions [ { gauss: { date: { origin: 'now', scale: '30d' } } } ]
boost_mode 'multiply'
end
end
end
```
--------------------------------
### Full search definition example with Elasticsearch::DSL
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/search.md
Full example of building a search definition with the Elasticsearch::DSL DSL, including query, filter, post_filter, aggregation, sort, size, from, and _source (delegated to Options). Then converts the definition to a Hash and passes it as the body to client.search.
```ruby
include Elasticsearch::DSL
definition = search do
query do
bool do
must { match title: 'test' }
must_not { term status: 'archived' }
end
end
filter { term category: 'news' }
post_filter { range published_at: { gte: '2020-01-01' } }
aggregation :by_category do
terms field: 'category', size: 10
end
sort do
by :published_at, order: 'desc'
end
size 25
from 0
_source ['title', 'category'] # delegated to Options
end
definition.to_hash
client.search body: definition
```
--------------------------------
### BucketSort aggregation example
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/aggregations-catalog.md
Example of using the BucketSort pipeline aggregation with a sort by total_sales in descending order and a size of 3.
```ruby
search do
aggregation :sales_bucket_sort do
bucket_sort do
sort do
by :total_sales, order: 'desc'
end
size 3
end
end
end
```
--------------------------------
### Pipeline aggregation example with moving_avg
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/aggregations-catalog.md
Pipeline example showing a date_histogram with a sum sub-aggregation and a moving_avg pipeline aggregation that references the total bucket path.
```ruby
search do
aggregation :sales do
date_histogram field: 'sale_date', fixed_interval: 'month' do
aggregation :total do
sum field: 'total'
end
aggregation :sales_moving_avg do
moving_avg buckets_path: 'total', window: 5, model: 'simple'
end
end
end
end
```
--------------------------------
### initialize method for BaseComponent
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/base-component.md
Sets up instance state: @hash starts as { => {} }, @args is the first constructor argument (typically the field name), @options is a second trailing argument, and @block is the DSL block.
```ruby
def initialize(*args, &block)
@hash = { name => {} }
@args = args.first || {}
@options = args.size > 1 ? args.last : {}
@block = block
end
```
--------------------------------
### ConstantScore query example
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/queries-catalog.md
Example of a `constant_score` query with a `filter` and a `boost` value.
```ruby
search do
query do
constant_score do
filter { term category: 'news' }
boost 1.2
end
end
end
```
--------------------------------
### suggest usage example
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/search.md
Example usage of the `suggest` method inside a `search` block, defining a suggestion with key `:title` and options for text and completion field.
```ruby
search do
suggest :title, text: 't', completion: { field: 'suggest.title' }
end
```
--------------------------------
### Using field with options
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/highlight.md
Example usage of the field method with per-field options.
```ruby
search do
highlight do
field :title, fragment_size: 0
field :body
end
end
```
--------------------------------
### HasChild query example
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/queries-catalog.md
Example of a `has_child` query with a `match` query and `score_mode` set to 'max'.
```ruby
search do
query do
has_child type: 'author' do
query { match name: 'John' }
score_mode 'max'
end
end
end
```
--------------------------------
### Using search with a block
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/search.md
Example of using the `search` method with a block to build a match query.
```ruby
include Elasticsearch::DSL
definition = search do
query { match title: 'test' }
end
```
--------------------------------
### to_hash usage examples
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/base-component.md
Shows to_hash output for a Term with a Hash argument and a Match with a block and option methods.
```ruby
Search::Queries::Term.new(category: 'Opinion').to_hash
# => { term: { category: 'Opinion' } }
Search::Queries::Match.new(:content) do
query 'how to fix my printer'
operator 'and'
end.to_hash
# => { match: { content: { query: 'how to fix my printer', operator: 'and' } } }
```
--------------------------------
### Example: using _and filter with term sub-filters
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/base-compound-filter-component.md
Shows how the DSL builds a list of filter components inside an _and block. The term methods are camelized and looked up in the Filters namespace, resulting in a serialized filter with an array of term hashes.
```ruby
search do
query do
filtered do
filter do
_and do
term category: 'men'
term size: 'xxl'
end
end
end
end
end
# serializes filter as
# { and: [ { term: { category: 'men' } }, { term: { size: 'xxl' } } ] }
```
--------------------------------
### Example of using name class method
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/base-component.md
Shows how to set a custom name for a component class and retrieve it. The setter name=(value) stores value.to_sym.
```ruby
class MyQuery
include BaseComponent
name :my_special_query
end
MyQuery.name
# => :my_special_query
```
--------------------------------
### Collapse usage with max_concurrent_group_searches
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/collapse.md
Example of using the collapse component with max_concurrent_group_searches set to 4.
```ruby
collapse :user do
max_concurrent_group_searches 4
end
```
--------------------------------
### method_missing usage example
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/search.md
Example usage of `method_missing` inside a `search` block: `from` is handled by Search, while `min_score` is delegated to Options.
```ruby
search do
query { match title: 'test' }
from 10 # handled by Search
min_score 0.5 # delegated to Options
end
```
--------------------------------
### Declarative search definition with Elasticsearch client
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/README.md
Build a search definition using the declarative DSL and execute it with the official Elasticsearch client. The example shows the resulting hash and the curl command sent by the client.
```ruby
require 'elasticsearch/dsl'
include Elasticsearch::DSL
definition = search do
query do
match title: 'test'
end
end
definition.to_hash
# => { query: { match: { title: "test"} } }
require 'elasticsearch'
client = Elasticsearch::Client.new trace: true
client.search body: definition
# curl -X GET 'http://localhost:9200/test/_search?pretty' -d '{
# "query":{
# "match":{
# "title":"test"
# }
# }
# }'
# ...
# => {"took"=>10, "hits"=> {"total"=>42, "hits"=> [...] } }
```
--------------------------------
### Collapse constructor: initialize(field, &block)
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/collapse.md
Initializes a Collapse component with the field to collapse on. Does not call super, so @args, @options, and @value are never initialized; @hash starts as { field: field }.
```ruby
def initialize(field, &block)
@hash = { field: field }
@block = block
end
```
--------------------------------
### Using the search DSL to build a query
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/search.md
Demonstrates including the DSL and using the `search` method with a block to build a match query, then converting to a hash.
```ruby
include Elasticsearch::DSL
definition = search do
query do
match title: 'test'
end
end
definition.to_hash
# => { query: { match: { title: "test"} } }
```
--------------------------------
### InnerHits usage example
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/queries-catalog.md
Example of using `inner_hits` with a name, `size`, `from`, and a `sort` block.
```ruby
inner_hits 'last_tweet' do
size 10
from 5
sort do
by :date, order: 'desc'
by :likes, order: 'asc'
end
end
```
--------------------------------
### Using Options methods
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/options.md
Shows how to use the dynamically defined option methods on an Options instance and the resulting hash from to_hash.
```ruby
o = Search::Options.new
o._source ['title', 'category'] # also: o.source [...]
o.script_fields score: { script: '_score * 2' }
o.explain true
o.min_score 0.5
o.track_total_hits true
o.to_hash
# => { _source: ['title', 'category'],
# script_fields: { score: { script: '_score * 2' } },
# explain: true,
# min_score: 0.5,
# track_total_hits: true }
```
--------------------------------
### Initializing a Search instance
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/search.md
Shows the constructor that stores options and evaluates the block either with instance_eval or by calling with the instance.
```ruby
def initialize(*args, &block)
@options = Options.new *args
@block = block
@block.arity < 1 ? self.instance_eval(&@block) : @block.call(self) if @block
end
```
--------------------------------
### Highlight.new(*args, &block)
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/highlight.md
Constructor for the Highlight component.
```APIDOC
## Highlight.new(*args, &block)
### Description
Initializes a new Highlight component. The last argument (if any) becomes the initial highlight options hash.
### Method
new
### Parameters
- **args** (*args) - Optional - The last argument becomes the initial highlight options (a Hash).
- **block** (&block) - Optional - DSL block.
### Returns
Highlight instance
### Example
```ruby
highlight = Highlight.new(fields: { title: {} })
```
```
--------------------------------
### Options#initialize
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/options.md
Initializes an Options instance with an empty hash. The arguments and block are accepted but ignored.
```ruby
def initialize(*args, &block)
@hash = {}
end
```
--------------------------------
### aggregation usage example
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/search.md
Example usage of the `aggregation` method inside a `search` block, defining a terms aggregation on the 'tags' field.
```ruby
search do
aggregation :tags do
terms field: 'tags'
end
end
```
--------------------------------
### Query#initialize
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/query.md
Constructor for the Query wrapper. Stores the DSL block; *args are ignored and passed through to the resolved component via method_missing.
```ruby
def initialize(*args, &block)
@block = block
end
```
--------------------------------
### Declaring suggestions with Search#suggest
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/suggest.md
Shows how to declare multiple suggestions via Search#suggest and the resulting to_hash output. Each suggestion is keyed by name; repeated names overwrite earlier entries.
```ruby
include Elasticsearch::DSL
definition = search do
suggest :title, text: 't', completion: { field: 'suggest.title' }
suggest :category, text: 'new', term: { field: 'category' }
end
definition.to_hash
# => { suggest: { title: { text: 't', completion: { field: 'suggest.title' } },
# category: { text: 'new', term: { field: 'category' } } } }
```
--------------------------------
### Highlight#initialize
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/highlight.md
Constructor for the Highlight component. The last argument becomes the initial highlight options hash; a single non-Hash argument (e.g. :title) will later break @value[:fields].
```ruby
def initialize(*args, &block)
@value = args.pop || {}
super
end
```
--------------------------------
### Post_filter usage with range
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/filter.md
Usage example for the top-level post_filter, using a range filter.
```ruby
search do
post_filter do
range published_at: { gte: '2020-01-01' }
end
end
```
--------------------------------
### Suggest.new(key, options={}, &block)
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/suggest.md
Constructor for the Suggest component. Takes a key, options hash, and optional block. The block is stored but not evaluated.
```APIDOC
## Suggest.new(key, options={}, &block)
### Description
Creates a new Suggest component that wraps one entry of the `suggest` part of a search definition. The block is accepted and stored but never evaluated.
### Method
Constructor
### Parameters
- **key** (Symbol/String) - Required - The suggestion name (e.g. `:title`)
- **options** (Hash) - Optional, default `{}` - Suggestion body, e.g. `{ text: 't', completion: { field: 'suggest.title' } }`
- **&block** (Proc) - Optional - Accepted and stored, but never evaluated by `Suggest`
### Request Example
```ruby
Suggest.new(:title, text: 't', completion: { field: 'suggest.title' })
```
### Response
#### Returns
- **Suggest** - The Suggest component instance
```
--------------------------------
### Collapse usage with inner_hits
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/collapse.md
Example of using the collapse component with inner_hits, specifying size, from, and sort options.
```ruby
collapse :user do
inner_hits 'last_tweet' do
size 10
from 5
sort do
by :date, order: 'desc'
by :likes, order: 'asc'
end
end
end
```
--------------------------------
### Suggest#initialize constructor
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/suggest.md
Constructor for the Suggest component. Stores the suggestion key, options hash, and block. The block is accepted but never evaluated by this class.
```ruby
def initialize(key, options={}, &block)
@key = key
@options = options
@block = block
end
```
--------------------------------
### Compound filter with _and
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/filter.md
Usage example for compound filters using _and to combine term and missing filters.
```ruby
search do
filter do
_and do
term category: 'news'
missing field: 'author'
end
end
end
```
--------------------------------
### Filter usage with term
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/filter.md
Usage example showing a filter block with a term filter, producing the expected hash output.
```ruby
include Elasticsearch::DSL
search do
filter do
term category: 'news'
end
end
# => { filter: { term: { category: 'news' } } }
```
--------------------------------
### Using fields with an array
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/highlight.md
Example usage of the fields method inside a search definition, passing an array of field names.
```ruby
search do
highlight do
fields [:title, :body]
end
end
```
--------------------------------
### Get aggregations collection
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/aggregation.md
Returns the underlying component's AggregationsCollection after evaluating the block. Useful for accessing nested aggregations.
```ruby
def aggregations
call
@value.__send__ :aggregations
end
```
--------------------------------
### Example of nested aggregation using aggregation method
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/base-aggregation-component.md
Shows how to define a nested aggregation with a histogram and a sub-aggregation summing a field. This is used in the context of the aggregation method.
```ruby
search do
aggregation :price do
histogram field: 'price', interval: 50 do
aggregation :revenue do
sum field: 'total'
end
end
end
end
```
--------------------------------
### Including Elasticsearch::DSL into a class
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/search.md
Shows how the module is included into a host class, adding the Elasticsearch::DSL::Search module.
```ruby
def self.included(base)
base.__send__ :include, Elasticsearch::DSL::Search
end
```
--------------------------------
### Creating Search instances with different block arities
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/search.md
Shows two ways to create a Search instance: with a block that uses instance_eval (arity 0) and with a block that receives the definition object.
```ruby
# block with arity 0 (default style)
s = Search.new do
size 10
end
# block receiving the definition object
s = Search.new do |q|
q.size 10
q.from 5
end
```
--------------------------------
### Collapse usage inside a search definition
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/collapse.md
Complete example of using collapse within a search definition, including query, collapse with max_concurrent_group_searches and inner_hits, and the resulting hash.
```ruby
include Elasticsearch::DSL
definition = search do
query { match_all }
collapse :user do
max_concurrent_group_searches 4
inner_hits 'last_tweet' do
size 5
sort { by :date, order: 'desc' }
end
end
end
definition.to_hash
# => { query: { match_all: {} },
# collapse: { field: :user,
# max_concurrent_group_searches: 4,
# inner_hits: { name: 'last_tweet', size: 5, sort: [ { date: { order: 'desc' } } ] } } }
```
--------------------------------
### Composite aggregation with sources
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/aggregations-catalog.md
Shows how to define a composite aggregation with a size and a sources array.
```ruby
search do
aggregation :my_composite do
composite size: 10 do
sources [ { product: { terms: { field: 'product' } } } ]
end
end
end
```
--------------------------------
### Range aggregation with key method usage
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/aggregations-catalog.md
Shows how to use the key method to define keyed ranges for a price field. Duplicate keys are ignored.
```ruby
search do
aggregation :price_ranges do
range do
field 'price'
key 'cheap', to: 50
key 'mid', from: 50, to: 100
key 'expensive', from: 100
end
end
end
```
--------------------------------
### Options#empty?
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/options.md
Returns true when no options have been set. Used by Search#to_hash to skip empty options.
```ruby
def empty?
@hash.empty?
end
```
--------------------------------
### Usage through the search DSL
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/options.md
Demonstrates using option methods directly inside a search block via Search#method_missing delegation to the internal Options instance.
```ruby
include Elasticsearch::DSL
definition = search do
query { match title: 'test' }
_source ['title', 'body']
fields ['title']
explain true
version true
track_scores true
min_score 0.5
indices_boost 'category_news' => 2.0 # accepts any value; stored verbatim
track_total_hits true
end
definition.to_hash
# => { query: { match: { title: 'test' } },
# _source: ['title', 'body'],
# fields: ['title'],
# explain: true,
# version: true,
# track_scores: true,
# min_score: 0.5,
# indices_boost: { 'category_news' => 2.0 },
# track_total_hits: true }
```
--------------------------------
### Implementation of the query method
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/search.md
Shows how the query method handles a block, an argument, or returns the current query.
```ruby
def query(*args, &block)
case
when block
@query = Query.new(*args, &block)
self
when !args.empty?
@query = args.first
self
else
@query
end
end
```
--------------------------------
### Query.new(*args, &block)
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/query.md
Initializes a new Query wrapper. The block is stored for later evaluation, and arguments are passed through to the resolved component via method_missing.
```APIDOC
## Query.new(*args, &block)
### Description
Initializes a new Query wrapper. The block is stored for later evaluation, and arguments are passed through to the resolved component via method_missing.
### Method
new
### Parameters
- `*args` (Array) - Optional - Ignored by the wrapper; passed through to the resolved component via method_missing.
- `&block` (Proc) - Optional - DSL block to be evaluated later.
### Source
`lib/elasticsearch/dsl/search/query.rb` (lines 31–33)
### Example
```ruby
q = Elasticsearch::DSL::Search::Query.new
```
```
--------------------------------
### Show __camelize output for match_phrase_prefix
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/utils.md
Demonstrates the output of `__camelize` when called with `:match_phrase_prefix`, and checks that the constant `'MatchPhrasePrefix'` is defined in the `Queries` module. This is equivalent to what happens when using the `match_phrase_prefix` DSL method.
```ruby
# Equivalent of what happens when you write: match_phrase_prefix 'abc'
include Elasticsearch::DSL
Utils.__camelize(:match_phrase_prefix)
# => "MatchPhrasePrefix"
Queries.const_defined?('MatchPhrasePrefix')
# => true
```
--------------------------------
### Define #to_hash instance method
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/base-component.md
Returns a Hash keyed by the component name, with four branches: block given, option methods built a value, @args is a non-empty Hash, or internal hash as-is. Source: lib/elasticsearch/dsl/search/base_component.rb lines 162-183.
```ruby
def to_hash(options={})
case
# 1. Create hash from the block
when @block
@hash = (@args && ! @args.empty?) ? { name => { @args => {} } } : { name => {} }
call
@hash[self.name.to_sym].update @options unless @options.empty?
@hash
# 2. Hash created with option methods
when @hash[self.name.to_sym] && ! @args.is_a?(Hash) && @hash[self.name.to_sym][@args]
@hash[self.name.to_sym].update @options unless @options.empty?
@hash
# 3. Hash passsed as @args
when @hash[self.name.to_sym] && @args.respond_to?(:to_hash) && ! @args.empty?
{ name => @args.to_hash }
# 4. Hash already built
else
@hash
end
end
```
--------------------------------
### Highlight#pre_tags
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/highlight.md
Sets the pre_tags option to the flattened argument list. Also available as pre_tags=.
```ruby
def pre_tags(*value)
@value[:pre_tags] = value.flatten
end; alias_method :pre_tags=, :pre_tags
```
--------------------------------
### Elasticsearch::DSL::Search::Search.new
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/search.md
Initializes a new Search object with optional arguments and a DSL block. The block is evaluated with instance_eval if arity is less than 1, otherwise called with the Search instance as argument.
```APIDOC
## Elasticsearch::DSL::Search::Search.new
### Description
Creates a new Search instance that wraps the whole search definition. The block is evaluated to build the search definition.
### Method
new
### Parameters
- `*args` (Hash/option pairs) - Optional - Extra search options; stored in an `Options` instance and merged into `to_hash`.
- `&block` (Proc) - Optional - DSL block; evaluated with `instance_eval` when arity < 1, otherwise called with the `Search` instance as argument.
### Examples
```ruby
# block with arity 0 (default style)
s = Search.new do
size 10
end
# block receiving the definition object
s = Search.new do |q|
q.size 10
q.from 5
end
```
```
--------------------------------
### Constructor-argument form of Sort
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/sort.md
Shows how to create a Sort object directly with constructor arguments.
```ruby
Search::Sort.new(:category).to_hash
# or
Search::Sort.new(:clicks => { order: 'desc' })
```
--------------------------------
### DSL_METHODS constant
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/options.md
Defines the list of accepted option keys for the Options class. Only these keys are recognized by the dynamically defined methods.
```ruby
DSL_METHODS = [
:_source,
:fields,
:script_fields,
:fielddata_fields,
:rescore,
:explain,
:version,
:indices_boost,
:track_scores,
:min_score,
:track_total_hits
]
```
--------------------------------
### Set search options in a search block
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/configuration.md
Shows how to set search definition options (e.g., _source, min_score, track_total_hits) inside a search block. Values are stored verbatim and unset options are omitted from the resulting hash.
```ruby
include Elasticsearch::DSL
definition = search do
query { match_all }
_source ['title']
min_score 0.5
track_total_hits true
end
definition.to_hash
# => { query: { match_all: {} }, _source: ['title'], min_score: 0.5, track_total_hits: true }
```
--------------------------------
### Sort usage in search block
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/sort.md
Shows how to use the `by` method inside a `sort` block within a search definition, with expected output.
```ruby
search do
sort do
by :category
by :clicks, order: 'desc'
end
end
# => { sort: [ :category, { clicks: { order: 'desc' } } ] }
```
--------------------------------
### InnerHits#initialize
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/queries-catalog.md
Initializes an InnerHits object with an optional name.
```APIDOC
## InnerHits#initialize
### Description
Initializes an `InnerHits` object with an optional name.
### Method
`initialize(name = nil, &block)`
### Parameters
- `name` (String) - Optional - The name of the inner hits.
- `block` (Proc) - Optional - A block for further configuration.
### Returns
The inner hits object.
### Example
```ruby
inner_hits 'last_tweet' do
size 10
end
```
```
--------------------------------
### suggest method definition
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/search.md
Defines the `suggest` method. When called with arguments or a block, it initializes `@suggest` as a hash, creates a `Suggest` object with the given key and options, and returns `self`; otherwise returns the current `@suggest` hash.
```ruby
def suggest(*args, &block)
if !args.empty? || block
@suggest ||= {}
key, options = args
@suggest.update key => Suggest.new(key, options, &block)
self
else
@suggest
end
end
```
--------------------------------
### Highlight#tags_schema
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/highlight.md
Sets the tags_schema option. Also available as tags_schema=.
```ruby
def tags_schema(value)
@value[:tags_schema] = value
end; alias_method :tags_schema=, :tags_schema
```
--------------------------------
### Define Sum and Stats metric aggregations
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/aggregations-catalog.md
Shows how to define metric aggregations (Sum and Stats) inside a search block. The Sum aggregation uses the field option, while Stats uses both field and script options.
```ruby
search do
aggregation :total do
sum field: 'price'
end
aggregation :stats do
stats do
field 'price'
script '_value * 2'
end
end
end
```
--------------------------------
### to_hash
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/suggest.md
Converts the Suggest component to a Hash representation. Returns the key and options without evaluating the block.
```APIDOC
## to_hash
### Description
Serializes the Suggest component to a Hash. Returns `{ key => options }`. The stored block is not invoked, so block-based suggestion definitions are not serialized.
### Method
Instance method
### Parameters
None
### Request Example
```ruby
suggest = Suggest.new(:title, text: 't', completion: { field: 'suggest.title' })
suggest.to_hash
```
### Response
#### Returns
- **Hash** - `{ key => options }`
#### Response Example
```ruby
{ title: { text: 't', completion: { field: 'suggest.title' } } }
```
```
--------------------------------
### Call __camelize with 'query_string'
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/utils.md
Shows how to call `__camelize` directly after requiring the library and including the DSL module. The method returns `'QueryString'`.
```ruby
require 'elasticsearch/dsl'
include Elasticsearch::DSL
Utils.__camelize('query_string')
# => 'QueryString'
```
--------------------------------
### Setting a query with a block or imperative assignment
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/_autodocs/api-reference/search.md
Shows two ways to set a query: using a block in the search DSL, or assigning a query object directly.
```ruby
search do
query do
match title: 'test'
end
end
# imperative:
definition.query = Search::Queries::Match.new title: 'test'
```
--------------------------------
### Run test suites with Rake
Source: https://github.com/elastic/elasticsearch-dsl-ruby/blob/main/CONTRIBUTING.md
Runs the unit and integration test suites via Rake tasks.
```bash
bundle exec rake test:unit
bundle exec rake test:integration
```