模块:Mongoid::SearchIndexable::ClassMethods
- 定义于:
- lib/mongoid/search_indexable.rb
Overview
该功能的类级方法的实现。
实例方法摘要折叠
-
#auto_embed_search(text, 索引:nil,path:nil,limit:10,num_candidates:nil, 过滤器:nil,exact:false,model:nil, 管道:[]) ⇒ Array<Mongoid::Document>
使用自动嵌入执行Atlas Vector Search查询。
-
# create_search_indexes ⇒ Array<String>
请求创建所有已注册的搜索索引。
-
# remove_search_index (name: nil, ID : nil) ⇒ 对象
删除给定名称或 ID 指定的搜索索引。
-
# remove_search_indexes ⇒ 对象
请求删除所有已注册的搜索索引。
-
# search_index (name_or_defn, defn = nil) ⇒ 对象
为提供的单键或复合键添加索引定义。
-
# search_indexes (options = {}) ⇒ 对象
查询当前model集合上可用搜索索引的便捷方法。
-
#vector_search(vector, 索引: nil, path: nil, limit: 10, num_candidates: nil, 过滤器: nil, 管道: []) ⇒ Array<Mongoid::Document>
执行Atlas Vector Search查询并返回匹配的document。
-
#vector_search_index(name_or_defn, defn = nil) ⇒ 对象
添加向量搜索索引定义。
-
# wait_for_search_indexes (names, Interval: 5 ) {|SearchIndexable::Status| ... } ⇒ 对象
等待创建命名搜索索引。
实例方法详细信息
# auto_embed_search (text, 索引:nil,path:nil,limit:10,num_candidates:nil, 过滤器:nil,exact:false,model:nil, 管道:[]) ⇒ Array<Mongoid::Document>
使用自动嵌入执行Atlas Vector Search查询。Atlas在查询时根据提供的文本生成查询向量;无需预先计算的嵌入。
每个返回的 document 都有一个用其相关性分数填充的 vector_search_score 属性。与 vector_search 不同,索引文本字段会保留在返回的 document 中。
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
# File 'lib/mongoid/search_indexable.rb', line 352 def (text, index: nil, path: nil, limit: 10, num_candidates: nil, 过滤器: nil, 精确: false, 模型: nil, 管道: []) # rubocop:disable Metrics/ParameterLists solved_index, solved_path = (索引(index), 路径) = { ' index ' => solved_index, 'path' => solved_path, '查询' => { 'text' => text }, 'limit' => limit } ['numCandidates'] = num_candidates || (limit * 10) 除非 精确 ['exact'] = true if 精确 [' 过滤 '] = 筛选器 if 筛选器 ['model'] = 模型 if 模型 agg_pipeline = [ { ' $vectorSearch ' => }, { ' $addFields ' => { 'vector_search_score' => { '$meta' => 'vectorSearchScore' } } } ] agg_pipeline.concat(阵列(管道)) 集合.聚合(agg_pipeline).map { |doc| 实例化(doc) } end |
# create_search_indexes ⇒ Array<String>
请求创建所有已注册的搜索索引。 请注意,搜索索引是异步创建的,可能需要几分钟才能完全可用。
155 156 157 158 159 |
# File 'lib/mongoid/search_indexable.rb', line 155 def create_search_indexes return if search_index_specs.空? 集合.search_indexes.create_many(search_index_specs) end |
# remove_search_index (name: nil, ID : nil) ⇒对象
删除给定名称或 ID 指定的搜索索引。 必须提供名称或 ID,但不能同时提供两者。
197 198 199 200 201 202 203 204 |
# File 'lib/mongoid/search_indexable.rb', line 197 def remove_search_index(名称: nil, ID : nil) 记录器.信息( " 搜索索引: 正在 删除 ID' #{ collection . name } ' 上的 搜索 集合 ' #{ { name || ID } ' " \ " 。 ")集合。 search_indexes 。 drop_one ( name: name , ID : ID ) end |
# remove_search_indexes ⇒对象
如果这样可以仅删除搜索索引,那就太好了
请求删除所有已注册的搜索索引。 请注意,搜索索引是异步删除的,可能需要几分钟才能完全删除。
已在model上声明,但由于model可能未命名索引,因此我们不能保证知道相应索引的名称或 ID。不过,可以合理地假设,model 的意图是一对一地声明所有所需的搜索索引,因此删除所有搜索索引就足够了。如果需要删除特定索引或索引设立,请考虑将 search_indexes.each 与 remove_search_index 一起使用。
218 219 220 221 222 |
# File 'lib/mongoid/search_indexable.rb', line 218 def remove_search_indexes search_indexes.每 do |spec| remove_search_index ID : spec[' ID '] end end |
# search_index (name_or_defn, defn = nil) ⇒对象
为提供的单键或复合键添加索引定义。
237 238 239 240 241 242 243 |
# File 'lib/mongoid/search_indexable.rb', line 237 def search_index(name_or_defn, defn = nil) 名称 = name_or_defn 名称, defn = nil, 名称 if 名称.is_a?(哈希) spec = { 定义: defn }.点击 { |s| s[:name] = 名称.to_s if 名称 } search_index_specs.推动(spec) end |
# search_indexes (options = {}) ⇒对象
查询当前model集合上可用搜索索引的便捷方法。
188 189 190 |
# File 'lib/mongoid/search_indexable.rb', line 188 def search_indexes( = {}) 集合.search_indexes() end |
# vector_search (vector, 索引: nil, path: nil, limit: 10, num_candidates: nil, 过滤器: nil, 管道: []) ⇒ 数组<document>
执行Atlas Vector Search查询并返回匹配的document。每个返回的 document 都有一个用其相关性分数填充的 vector_search_score 属性。
默认下,从返回的 document 中排除向量字段(由 path: 给出),因为向量很大,检索后几乎没有用处。
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/mongoid/search_indexable.rb', line 298 def vector_search(向量, index: nil, path: nil, limit: 10, num_candidates: nil, 过滤器: nil, 管道: []) # rubocop:disable Metrics/ParameterLists solved_index, solved_path = resolve_vector_index(索引(index), 路径) num_candidates ||= limit * 10 = { ' index ' => solved_index, 'path' => solved_path, 'queryVector' => 向量, 'numCandidates' => num_candidates, 'limit' => limit } [' 过滤 '] = 筛选器 if 筛选器 agg_pipeline = [ { ' $vectorSearch ' => }, { ' $addFields ' => { 'vector_search_score' => { '$meta' => 'vectorSearchScore' } } }, { '$ 项目' => { solved_path => 0 } } ] agg_pipeline.concat(阵列(管道)) 集合.聚合(agg_pipeline).map { |doc| 实例化(doc) } end |
#vector_search_index(name_or_defn, defn = nil) ⇒ 对象
添加向量搜索索引定义。还可以在首次调用model时在model上定义一个只读 vector_search_score字段,该字段将填充 vector_search 返回的document。
259 260 261 262 263 264 265 266 267 268 269 270 |
# File 'lib/mongoid/search_indexable.rb', line 259 def vector_search_index(name_or_defn, defn = nil) 名称 = name_or_defn 名称, defn = nil, 名称 if 名称.is_a?(哈希) spec = { 类型: 'vectorSearch', 定义: defn }.点击 { |s| s[:name] = 名称.to_s if 名称 } search_index_specs.推动(spec) return if 字段.键?('vector_search_score') 字段 :vector_search_score, 类型: Float attr_readonly :vector_search_score end |
# wait_for_search_indexes (names, Interval: 5 ) {|SearchIndexable::Status| ... } ⇒对象
等待创建命名搜索索引。
168 169 170 171 172 173 174 175 176 |
# File 'lib/mongoid/search_indexable.rb', line 168 def wait_for_search_indexes(名称, interval: 5) 循环 do 状态 = 状态.new(get_indexes(名称)) 产量 状态 if block_given? 中断 if 状态.准备好了吗? 睡眠 interval end end |