Class: OpenSearch::DSL::Search::Highlight

Inherits:
Object
  • Object
show all
Includes:
BaseComponent
Defined in:
lib/opensearch/dsl/search/highlight.rb

Overview

Wraps the ‘highlight` part of a search definition

Instance Method Summary collapse

Methods included from BaseComponent

included

Constructor Details

#initialize(*args, &block) ⇒ Highlight

Returns a new instance of Highlight.



37
38
39
40
# File 'lib/opensearch/dsl/search/highlight.rb', line 37

def initialize(*args, &block)
  @value = args.pop || {}
  super
end

Instance Method Details

#encoder(value) ⇒ Object Also known as: encoder=

Specify the ‘encoder` option for highlighting



95
96
97
# File 'lib/opensearch/dsl/search/highlight.rb', line 95

def encoder(value)
  @value[:encoder] = value
end

#field(name, options = {}) ⇒ Object

Specify a single field to highlight

Examples:


search do
  highlight do
    field :title, fragment_size: 0
    field :body if options[:comments]
  end
end


77
78
79
# File 'lib/opensearch/dsl/search/highlight.rb', line 77

def field(name, options={})
  (@value[:fields] ||= {}).update name.to_sym => options
end

#fields(value_or_name) ⇒ Object

Specify the fields to highlight

Examples:


search do
  highlight do
    fields [:title, :body]
    field  :comments.body if options[:comments]
  end
end


53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/opensearch/dsl/search/highlight.rb', line 53

def fields(value_or_name)
  value = case value_or_name
    when Hash
      value_or_name
    when Array
      value_or_name.reduce({}) { |sum, item| sum.update item.to_sym => {}; sum }
    else
  end

  (@value[:fields] ||= {}).update value
  self
end

#post_tags(*value) ⇒ Object Also known as: post_tags=

Specify the closing tags for the highlighted snippets



89
90
91
# File 'lib/opensearch/dsl/search/highlight.rb', line 89

def (*value)
  @value[:post_tags] = value.flatten
end

#pre_tags(*value) ⇒ Object Also known as: pre_tags=

Specify the opening tags for the highlighted snippets



83
84
85
# File 'lib/opensearch/dsl/search/highlight.rb', line 83

def pre_tags(*value)
  @value[:pre_tags] = value.flatten
end

#tags_schema(value) ⇒ Object Also known as: tags_schema=

Specify the ‘tags_schema` option for highlighting



101
102
103
# File 'lib/opensearch/dsl/search/highlight.rb', line 101

def tags_schema(value)
  @value[:tags_schema] = value
end

#to_hashHash

Convert the definition to a Hash

Returns:

  • (Hash)


109
110
111
112
113
# File 'lib/opensearch/dsl/search/highlight.rb', line 109

def to_hash
  call
  @hash = @value
  @hash
end