类:Mongo::SearchIndex::View

继承:
对象
  • 对象
显示全部
扩展方式:
可转发
包括:
Enumerable、Collection::Helpers、Retryable
定义于:
lib/ Mongo/search_index/view.rb

Overview

表示搜索索引视图的类。

实例属性摘要折叠

实例方法摘要折叠

Collection::Helpers 中包含的方法

#do_drop

Retryable 中包含的方法

#read_worker#select_server#with_overload_retry#write_worker

构造函数详情

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

创建新的搜索索引视图。

参数:

  • 集合 ( Collection )

    集合。

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

    配置视图行为的选项。

选项哈希 ( options ):

  • :id string

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

  • :name string

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

  • :aggregate 哈希

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

引发:

  • ( ArgumentError )


36
37
38
39
40
41
42
43
44
45
# File 'lib/ Mongo/search_index/view.rb', line 36

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 哈希(只读)

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

返回:

  • (哈希)

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



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

def aggregate_options
  @aggregate_options
end

# 集合Mongo::Collection (只读)

返回此视图所属的集合。

返回:



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

def 集合
  @collection
end

# requests_index_id nil |字符串(只读)

返回要查询的索引ID 。

返回:

  • ( nil | string )

    要查询的索引ID



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

def requests_index_id
  @requested_index_id
end

# requests_index_name nil |字符串(只读)

返回要查询的索引名称。

返回:

  • ( nil | string )

    要查询的索引名称



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

def requests_index_name
  @requested_index_name
end

实例方法详细信息

# create_many (indexes) ⇒ Array<String>

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

参数:

  • 索引 ( Array<Hash> )

    要创建的索引的说明。列表的每个元素必须是具有定义键、可选名称键和可选类型键的哈希。类型键必须是“搜索”或“ vectorSearch ”之一。默认为“搜索”。

返回:

  • ( Array<String> )

    新搜索索引的名称。



71
72
73
74
75
76
77
78
79
# File 'lib/ Mongo/search_index/view.rb', line 71

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

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

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

参数:

  • 定义 (哈希)

    搜索索引的定义。

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

    新搜索索引的名称。

  • 类型 string (默认为: '搜索')

    搜索索引的类型。可能的值为“搜索”和“vectorSearch”。默认为“搜索”。

返回:

  • ( string )

    新搜索索引的名称。



56
57
58
59
60
61
# File 'lib/ Mongo/search_index/view.rb', line 56

def create_one(定义, 名称: nil, 类型: ' 搜索 ')
  spec = { 定义: 定义, 类型: 类型 }.点击 do |sp|
    sp[:name] = 名称 除非 名称.nil?
  end
  create_many([ spec ]).first
end

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

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

参数:

  • id string (默认为: nil

    要删除的索引的 ID

  • 名称 string (默认为: nil

    要删除的索引的名称

返回:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ Mongo/search_index/view.rb', line 89

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

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

  追踪器.trace_operation(op, 上下文, op_name: 'dropSearchIndex') do
    # 根据规范:
    # 驱动程序必须抑制 NamespaceNotFound 错误
    # ``dropSearchIndex`` 助手。  删除操作应该是幂等的。
    do_drop(op, nil, 上下文)
  end
end

#each (&block) self |枚举器

遍历搜索索引。

参数:

  • ( Proc )

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

返回:

  • ( self | Enumerator )

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



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/ Mongo/search_index/view.rb', line 111

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 )

    可枚举项是否为空。



156
157
158
# File 'lib/ Mongo/search_index/view.rb', line 156

def 空?
  数数.zero?
end

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

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

参数:

  • 定义 (哈希)

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

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

    要更新的搜索索引的 ID

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

    要更新的搜索索引的名称

返回:



139
140
141
142
143
144
145
146
147
148
# File 'lib/ Mongo/search_index/view.rb', line 139

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

  spec = spec_with(index_id: id, index_name: 名称, index: 定义)
  op = 操作::updateSearchIndex.new(spec)
  上下文 = execution_context
  追踪器.trace_operation(op, 上下文, op_name: 'updateSearchIndex') do
    op.执行(next_primary, 上下文: 上下文)
  end
end