Class: Mongoid::Contextual::TextSearch

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Command
Defined in:
lib/mongoid/contextual/text_search.rb

Overview

Wraps behaviour around a lazy text search command.

Since:

  • 4.0.0

Instance Attribute Summary

Attributes included from Command

#collection, #collection The collection to query against., #criteria, #criteria The criteria for the context.

Instance Method Summary collapse

Methods included from Command

#command, #session

Constructor Details

#initialize(collection, criteria, search_string) ⇒ TextSearch

Instantiate a new text search lazy proxy.

Examples:

Instantiate the text search.

TextSearch.new(collection, criteria, "test")

Parameters:

  • collection (Moped::Collection)

    The collection to execute on.

  • criteria (Criteria)

    The criteria to filter results.

  • search_string (String)

    The search string.

Since:

  • 4.0.0


47
48
49
50
51
52
# File 'lib/mongoid/contextual/text_search.rb', line 47

def initialize(collection, criteria, search_string)
  @collection, @criteria = collection, criteria
  command[:text] = collection.name.to_s
  command[:search] = search_string
  apply_criteria_options
end

Instance Method Details

#eachEnumerator

Iterate over the results of the text search command.

Examples:

Iterate over the results.

text_search.each do |doc|
  #...
end

Returns:

  • (Enumerator)

    The enumerator.

Since:

  • 4.0.0


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongoid/contextual/text_search.rb', line 25

def each
  if block_given?
    selecting(:project) do
      documents.each do |doc|
        yield doc
      end
    end
  else
    to_enum
  end
end

#executeHash

Execute the text search command, and return the raw results (in hash form).

Examples:

Execute the command.

text_search.execute

Returns:

  • (Hash)

    The raw results.

Since:

  • 4.0.0


83
84
85
# File 'lib/mongoid/contextual/text_search.rb', line 83

def execute
  results
end

#inspectString

Inspect the text search object.

Examples:

Inspect the text search.

text_search.inspect

Returns:

  • (String)

    The inspection.

Since:

  • 4.0.0


62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mongoid/contextual/text_search.rb', line 62

def inspect
%Q{#<Mongoid::Contextual::TextSearch
  selector:   #{criteria.selector.inspect}
  class:      #{criteria.klass}
  search:     #{command[:search]}
  filter:     #{command[:filter] || "N/A"}
  project:    #{command[:project] || "N/A"}
  limit:      #{command[:limit] || "N/A"}
  language:   #{command[:language] || "default"}>
}
end

#language(value) ⇒ TextSearch

Set the language of the text search.

Examples:

Set the text search language.

text_search.language("deutsch")

Parameters:

  • value (String)

    The name of the language.

Returns:

Since:

  • 4.0.0


97
98
99
100
# File 'lib/mongoid/contextual/text_search.rb', line 97

def language(value)
  command[:language] = value
  self
end

#project(value) ⇒ TextSearch

Limits the fields returned by the text search for each document. By default, _id is always included.

Examples:

Limit the returned fields.

text_search.project(name: 1, title: 1)

Parameters:

  • value (Hash)

    The fields to project.

Returns:

Since:

  • 4.0.0


113
114
115
116
# File 'lib/mongoid/contextual/text_search.rb', line 113

def project(value)
  command[:project] = value
  self
end

#statsHash

Get the raw statistics returned from the text search.

Examples:

Get the stats.

text_search.stats

Returns:

  • (Hash)

    The raw statistics.

Since:

  • 4.0.0


126
127
128
# File 'lib/mongoid/contextual/text_search.rb', line 126

def stats
  results["stats"]
end