类:Mongo::SearchIndex::View

继承:
对象
  • 对象
显示全部
包括:
Enumerable、Collection::Helpers、Retryable
定义于:
lib/ Mongo/search_index/view.rb

Overview

表示搜索索引视图的类。

实例属性摘要折叠

实例方法摘要折叠

Collection::Helpers 中包含的方法

#do_drop

Retryable 中包含的方法

#read_worker#select_server#write_worker

构造函数详情

#initialize (集合, options = {}) ⇒查看

创建新的搜索索引视图。

参数:

  • 集合 ( Collection )

    集合。

  • 选项 哈希 (默认为: {}

    配置视图行为的选项。

选项哈希 ( options ):

  • :id string

    要查询的特定索引ID(可选)

  • :name string

    要查询的特定索引的名称(可选)

  • :aggregate 哈希

    查询可用索引时发送到聚合命令的选项哈希。

引发:

  • ( ArgumentError )
[查看源代码]

33
34
35
36
37
38
39
40
41
42
# File 'lib/ Mongo/search_index/view.rb', line 33

def 初始化(集合, 选项 = {})
  @collection = 集合
  @requested_index_id = 选项[:id]
  @requested_index_name = 选项[:name]
  @aggregate_options = 选项[:aggregate] || {}

  return if @aggregate_options.is_a?(哈希)

  提高 ArgumentError, " :aggregate 选项必须是哈希(有 #{ @aggregate_options . class } "
end

实例属性详细信息

#aggregate_options 哈希(只读)

在查询可用索引时,返回用于聚合命令的选项哈希。

返回:

  • (哈希)

    查询可用索引时用于聚合命令的选项哈希。


22
23
24
# File 'lib/ Mongo/search_index/view.rb', line 22

def aggregate_options
  @aggregate_options
end

# 集合Mongo::Collection (只读)

返回此视图所属的集合。

返回:


12
13
14
# File 'lib/ Mongo/search_index/view.rb', line 12

def 集合
  @collection
end

# requests_index_id nil |字符串(只读)

返回要查询的索引ID 。

返回:

  • ( nil | string )

    要查询的索引ID


15
16
17
# File 'lib/ Mongo/search_index/view.rb', line 15

def requests_index_id
  @requested_index_id
end

# requests_index_name nil |字符串(只读)

返回要查询的索引名称。

返回:

  • ( nil | string )

    要查询的索引名称


18
19
20
# File 'lib/ Mongo/search_index/view.rb', line 18

def requests_index_name
  @requested_index_name
end

实例方法详细信息

# create_many (indexes) ⇒ Array<String>

使用单个命令创建多个搜索索引。

参数:

  • 索引 ( Array<Hash> )

    要创建的索引的描述。 列表的每个元素必须是带有定义键和可选名称键的哈希。

返回:

  • ( Array<String> )

    新搜索索引的名称。

[查看源代码]

62
63
64
65
66
# File 'lib/ Mongo/search_index/view.rb', line 62

def create_many(索引)
  spec = spec_with(索引: 索引.map { |v| validate_search_index!(v) })
  结果 = 操作::创建搜索索引.new(spec).执行(next_primary, 上下文: execution_context)
  结果.first['indexesCreated'].map { |idx| idx[' name '] }
end

# create_one (definition, name: nil, type: ' 搜索') ⇒ 字符串

使用给定定义创建单个搜索索引。 如果提供了名称,则会为新索引指定该名称。

参数:

  • 定义 (哈希)

    搜索索引的定义。

  • 名称 ( nil | string ) (默认为: nil

    新搜索索引的名称。

返回:

  • ( string )

    新搜索索引的名称。

[查看源代码]

51
52
53
# File 'lib/ Mongo/search_index/view.rb', line 51

def create_one(定义, 名称: nil, 类型: ' 搜索 ')
  create_many([ { 名称: 名称, 定义: 定义, 类型: 类型 } ]).first
end

# drop_one (ID: nil, name: nil) ⇒ Mongo::Operation::Result | false

删除具有给定 ID 或名称的搜索索引。 必须指定其中之一,但不能同时指定两者。

参数:

  • id string (默认为: nil

    要删除的索引的 ID

  • 名称 string (默认为: nil

    要删除的索引的名称

返回:

[查看源代码]

76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ Mongo/search_index/view.rb', line 76

def drop_one(ID : nil, 名称: nil)
  validate_id_or_name!(id, 名称)

  spec = spec_with(index_id: id, index_name: 名称)
  op = 操作::DropSearchIndex.new(spec)

  # 根据规范:
  # 驱动程序必须抑制 NamespaceNotFound 错误
  # ``dropSearchIndex`` 助手。  删除操作应该是幂等的。
  do_drop(op, nil, execution_context)
end

#each (&block) self |枚举器

遍历搜索索引。

参数:

  • ( Proc )

    如果给定,则每个搜索索引都将屈服于区块。

返回:

  • ( self | Enumerator )

    如果给出了区块,则返回 self。 否则,将返回一个枚举器。

[查看源代码]

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ Mongo/search_index/view.rb', line 95

def ()
  @result ||= 开始
    spec = {}.点击 do |s|
      s[:id] = requests_index_id if requests_index_id
      s[:name] = requests_index_name if requests_index_name
    end

    集合.通过(read_concern: {}).聚合(
      [ { '$listSearchIndexes' => spec } ],
      aggregate_options
    )
  end

  return @result.to_enum 除非 

  @result.()
  self
end

#为空?true | false

查询搜索索引可枚举项是否为空。

返回:

  • ( true | false )

    可枚举项是否为空。

[查看源代码]

136
137
138
# File 'lib/ Mongo/search_index/view.rb', line 136

def 空?
  数数.zero?
end

# update_one (definition, ID: nil, name: nil) ⇒ Mongo::Operation::Result

使用给定 ID 或名称更新搜索索引。 必须提供其中之一,但不能同时提供。

参数:

  • 定义 (哈希)

    用于替换给定搜索索引的定义。

  • id ( nil | string ) (默认为: nil

    要更新的搜索索引的 ID

  • 名称 ( nil | string ) (默认为: nil

    要更新的搜索索引的名称

返回:

[查看源代码]

123
124
125
126
127
128
# File 'lib/ Mongo/search_index/view.rb', line 123

def update_one(定义, ID : nil, 名称: nil)
  validate_id_or_name!(id, 名称)

  spec = spec_with(index_id: id, index_name: 名称, index: 定义)
  操作::updateSearchIndex.new(spec).执行(next_primary, 上下文: execution_context)
end